setting variable in header.php but not seen in footer.php - wordpress

in wordpress ,
i set a variable in header.php
<?php
$var= 'anything'
?>
but in footer.php when I echo it
<?php
echo $var;
?>
I got no thing printed ... why !>

You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).
So just declare your variable as global:
$GLOBALS[ 'var' ] = '...';
Then:
echo $GLOBALS[ 'var' ];

I know you've already accepted the answer to this question; however, I think there's a much better approach to the variable scope problem than passing vars into the $GLOBALS array.
Take the functions.php file in your theme for example. This file is included outside the scope of the get_header() and get_footer() functions. In fact it supersedes anything else you might be doing in your theme (and I believe in the plugin scope as well--though I'd have to check that.)
If you want to set a variable that you'd like to use in your header/footer files, you should do it in your functions.php file rather than polluting $GLOBALS array. If you have more variables that you want to sure, consider using a basic Registry object with getters/setters. This way your variables will be better encapsulated in a scope you can control.
Registry
Here's a sample Registry class to get you started if:
<?php
/**
* Registry
*
* #author Made By Me
* #version v0.0.1
*/
class Registry
{
# +------------------------------------------------------------------------+
# MEMBERS
# +------------------------------------------------------------------------+
private $properties = array();
# +------------------------------------------------------------------------+
# ACCESSORS
# +------------------------------------------------------------------------+
/**
* #set mixed Objects
* #param string $index A unique index
* #param mixed $value Objects to be stored in the registry
* #return void
*/
public function __set($index, $value)
{
$this->properties[ $index ] = $value;
}
/**
* #get mixed Objects stored in the registry
* #param string $index A unique ID for the object
* #return object Returns a object used by the core application.
*/
public function __get($index)
{
return $this->properties[ $index ];
}
# +------------------------------------------------------------------------+
# CONSTRUCTOR
# +------------------------------------------------------------------------+
public function __construct()
{
}
}
Save this class in your theme somewhere, e.g. /classes/registry.class.php Include the file at the top of your functions.php file: include( get_template_directory() . '/classes/registry.class.php');
Example Usage
Storing variables:
$registry = new Registry();
$registry->my_variable_name = "hello world";
Retrieving variables:
echo '<h1>' . $registry->my_variable_name . '</h1>'
The registry will accept any variable type.
Note: I normally use SplObjectStorage as the internal datastore, but I've swapped it out for a regular ole array for this case.

I know this is a bit old question and with a solution voted but I though I should share another option and just found a better solution (that works) without using Globals
function fn_your_var_storage( $var = NULL )
{
static $internal;
if ( NULL !== $var )
{
$internal = $var;
}
return $internal;
}
// store the value
fn_your_var_storage( 'my_value' );
// retrieve value
echo fn_your_var_storage(); // print my_value

try this code
first define your initial variable
$var="something";
then use the $_GLOBALS
$_GLOBALS['myvar']=$var;
and finally use the global variable in anywhere you want
global $myvar;
define string inside the $_GLOBALS as taken as global variable name or use the $_GLOBALS['myvar'] direct into the code without using the global

In wordpress Header, any template, Footer is different functions so you have to declare any varible as a global variable then you can access it .
/** header.php **/
<?php
global $xyz;
$xyz="123456"; ?>
/** Template.php or Footer.php **/
<?php
echo $xyz; ///return 123456
?>

Related

Unit Testing with Fat-Free-Framework

Is there a way to use PHPUnit where I have a test folder with indexTest.php inside that tests routes in my index.php file?
The fat-free guide gives snippets of code for mocking route requests and POSTS. I have only managed to get such a test to work if I generate the route directly in my test file with whatever functionality in it.
What I would like is to mock a route with tokens, allow it to run from a route in index.php and through the controller and test f3 variables that should be set by running the route.
<?php
class indexTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$f3 = Base::instance();
// Don't write to STDOUT
$f3->set('QUIET', true);
$f3->route('GET /path', function(){ echo 'TEXT'; });
$this->assertNull($f3->mock('GET /path'));
$this->assertSame('TEXT', $f3->get('RESPONSE'));
$f3->route('GET /verify/#answer/#value',
function($f3, $params){
$errors = array();
$answer = $params['answer'];
$value = $params['value'];
$prefix = substr($answer, 0, 3); //pre, ans, pos
$id = (int)substr($answer, 3); //question id number (1, 2, 3, 4)
//$value is the input value from user
$result = check_id($prefix, $id, $value);
if($result !== true){
$errors[] = $result;
}
$f3->set('errors', $errors);
return $errors;
});
function check_id($prefix, $id, $value)
{
if($prefix == 'pre' || $prefix == 'pos'){
if($value <= 0 || $value > 180 || $value === NULL){
echo 'The input value of ' . $prefix . $id . ' question was out of bounds';
return 'The input value of ' . $prefix . $id . ' question was out of bounds';
}else{
return true;
}
}else if($prefix == 'ans'){
if($value < 0 || $value > 10 || $value === NULL){
echo 'The value of quiz ans' + $id + ' was out of bounds';
return 'The value of quiz ans' + $id + ' was out of bounds';
}else{
return true;
}
}else {
return 'The prefix does not match';
}
}
$this->assertNotNull($f3->mock('GET /verify/ans1/8'));
$this->assertEmpty($f3->get('RESPONSE')[0]);
$this->assertNotNull($f3->mock('GET /verify/dsk4/6'));
$this->assertSame('6', $f3->get('PARAMS.value'));
$this->assertSame('dsk4', $f3->get('PARAMS.answer'));
$this->assertEmpty($f3->get('RESPONSE')[0]);
$this->assertNotNull($f3->mock('GET /verify/pre4/250'));
$this->assertSame('The input value of pre4 question was out of bounds', $f3->get('errors')[0]);
$this->assertNotSame('pre4', $f3->get('PARAMS.answer'));
$f3->set('QUIET',FALSE); // allow test results to be shown later
$f3->clear('ERROR'); // clear any errors
}
}
I'd prefer not to declare the entire route like this, maybe I am entirely wrong and this is not possible? The above code works running vendor/bin/phpunit. Relative examples and tutorials are difficult to find on this.
Short answer
Separate your controller code from bootstrapping and routing code
Reuse the routing configuration in your environments, e.g. website, CLI and testing environment
Use Base->mock() in your tests to mock the previously defined routes
Don't execute Base->run() in the testing environment
Long answer
I'm planning for a long time to write an article about testing F3 routes but due the lack of time I will just give some points here instead:
Create a reusable file which defines the routes (e.g. a routes.php file or a INI file with route defintions)
Load the routes before running test code. This could be easily done with a custom bootstrap file for PHPUnit (--bootstrap <FILE> or use the according directive in PHPUnit's configuration).
Write the PHPUnit tests
Example
The following example is an adaption of my GitHub Gist:
bootstrap-website.php
<?php
$f3 = Base::instance();
require 'bootstrap-shared.php';
// [Custom rules only for the website here]
require 'routes.php';
$f3->run();
bootstrap-test.php
<?php
$f3 = Base::instance();
require 'bootstrap-shared.php';
// [Custom rules only for testing environment here]
$f3->set('QUIET', true);
$f3->set('APP.TEST', true);
require 'routes.php';
routes.php
<?php
/**
* #var $f3 Base
*/
$f3->route('GET /path', function(){ echo 'TEXT'; });
ExampleTest.php
class ExampleTest extends PHPUnit_Framework_TestCase {
public function test() {
// Could also be provided by a custom base TestCase.
$f3 = Base::instance();
$this->assertNull($f3->mock('GET /path'));
$this->assertSame('TEXT', $f3->get('RESPONSE'));
}
}
Some notes:
bootstrap-test.php is the custom bootstrapping file for PHPUnit
bootstrap-website.php is the bootstrapping file for the website
bootstrap-shared.php contains information shared by all environments. The file could include routing information. I separated the routing information in the example: routes.php
ExampleTest.php is a regular PHPUnit test
The $f3->set('QUIET', true); snippet should be added to the custom bootstrap file. It's also a good idea to introduce a variable showing that the application is running in a test mode, for instance $f3->set('APP.TEST', true)
F3 doesn't clean up your variables between tests/mocks. You could store the original state before running tests and then restore the state in PHPUnit's setUp() method
Instead of rendering pages it could also be sufficient to collect only the data which should be available for rendering. In this case use the introduced APP.TEST variable in your view to skip rendering
Notes for later answer updates
ini_set('error_log','./phpunit/error.log')
$f3->set('ONERROR',function(){});

Wordpress custom plugin shortcode wpdb unknown

I'm writing a custom plugin in wordpress and I'm using a shortcode for the first time.
Until now my shortcode works but now I want to execute a query in the function of that shortcode to get all the employees from a specific employee group id
[employee_group_tag group_id=2]
This is the file that has my shortcode functionality:
<?php
/*
Plugin Name: Employees
Plugin URI: xxx
Description: xxx
Version: 1.0
Author: xxx
Author URI: http://www.blabla.com
*/
global $wpdb;
function employee_shortcode_func( $atts ) {
extract( shortcode_atts( array(
'group_id' => '0',
), $atts ) );
// Alle werknemers ophalen adhv van group_id
$sql = 'SELECT * FROM wp_werknemers_employees WHERE employee_employee_group_id = ' . $group_id;
$results = $wpdb->get_results($sql);
return 'test';
}
add_shortcode( 'employee_group_tag', 'employee_shortcode_func' );
But when I run this it gets stuck on $wpdb->get_results($sql) because when I leave this out it displays my page correctly but when I want to fill it with employee details I only get the half of my page. So it "breaks"
I tried to do (which works in all my other files)
require_once("../../../wp-config.php")
But it doesn't work...
Is it possible that it's not working because its in my "main" plugin file? Because in all my other files I can use wpdb when I use the require function...
any ideas?
From the PHP Manual:
For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:
$a = 1;
include 'b.inc';
Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:
$a = 1; /* global scope */
function test()
{
echo $a; /* reference to local scope variable */
}
test();
The last example does not work, as $a is not defined inside the scope of the function.
Just like in your case, for it to work you have to use:
function employee_shortcode_func( $atts ) {
global $wpdb;
// etc
}

drupal template.php

how should i know which variables and objects can be used directly in this file.(eg:$node,$term....) thank you.
In template.php
/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_node(&$vars) {
_vdump(get_defined_vars(), 1);
}
/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_page(&$vars) {
_vdump(get_defined_vars(), 1);
}
And add dump function to custom module
/*
* Custom dump function
*
* #param $vars
* An string or array containing the data.
* #param $keys
* If true6 function will return keys of $vars array
* #return a dump of $vars as drupal message.
*/
function _vdump($var, $keys = FALSE) {
if($keys){
drupal_set_message('<pre>' . print_r(array_keys($var), 1) . '</pre>');
}
else {
drupal_set_message('<pre>' . print_r($var, 1) . '</pre>');
}
}
I'm guessing you're talking about creating/modifying a theme. You could use most of the standard Drupal globals. You can always use get_defined_vars to see if any other variables have been defined.
There is no such variables in the template.php file.
Do you think of $node, $terms, ... that you find on page.tpl.php or node.tpl.php ?
If yes, those variables are generated in the preprocess functions.
Modules can implements those hook to define new variables that you can directly use in those file, or template.php can also define some new variables.
Please have a look in the documentation about the preprocess

Warning: Cannot modify header information

Warning: Cannot modify header information - headers already sent by (output started at /home/content/51/5126851/html/wp-includes/post-template.php:54) in /home/content/51/5126851/html/wp-includes/pluggable.php on line 890
I know I need to do something to my post-template.php file, but I'm not sure what. I looked at the other answers but mine seems a little different. Here's what the relevant function looks like:
/**
* Display or retrieve the current post title with optional content.
*
* #since 0.71
*
* #param string $before Optional. Content to prepend to the title.
* #param string $after Optional. Content to append to the title.
* #param bool $echo Optional, default to true.Whether to display or return.
* #return null|string Null on no title. String if $echo parameter is false.
*/
function the_title($before = '', $after = '', $echo = true) {
$title = get_the_title();
if ( strlen($title) == 0 )
return;
$title = $before . $title . $after;
if ( $echo )
echo $title; // <-- This is line 54 of post-template.php
else
return $title;
}
My first recommendation would be to learn how to format code for SO. And figure out how to cut down your problem to the bare minimum someone needs to solve it.
My second would be to look around the line mentioned in the error. I just did, and look what I found:
if ( $echo )
echo $title;
So now you know what's outputting stuff, what can you do about it?
Well, the other part of that statement is:
else
return $title;
Now, I'm no Wordpress expert, but I'm sure that you can work out the first thing that needs changing.
You shouldn't be editing files in wp-includes without a good reason and a good understanding of what you're doing. WordPress is extendable via themes and plugins in almost all situations - you should rarely, if ever, have to hack core code to do something.

Check for role in node-type.tpl.php

Is there any way to check the role of the viewer within a drupal theme file, so as to write a conditional statement?
Thanks...
The current user is always available as a global variable, so just do:
// Make the user object available
global $user;
// Grab the user roles
$roles = $user->roles;
$user->roles will be an array of role names, keyed by role id (rid).
Edit: To be precise, the global user object is made available during early bootstrapping, in phase DRUPAL_BOOTSTRAP_SESSION, but from the point of custom coding within themes or modules, you can treat that global as always available.
This will do
global $user;
$num_roles = db_fetch_object(pager_query(db_rewrite_sql('SELECT rid from {role} ORDER BY rid Desc')))->rid; // Find how many roles are there
for($i=0; $i < $num_roles; $i++){
if(strlen($user->roles[$i]) >0){
echo $user->roles[$i];
$i = $num_roles;
}
}
Just an appendix to Henrik Opel's answer:
if you use it in a tpl.php file, then create a variable first in a preprocess_node function:
<?php
function YOURTEMPLATE_preprocess_node(&$variables) {
global $user;
$variables['current_user_roles'] = $user->roles;
}
?>
Now you can print your roles in your tpl.php:
<?php
if ($current_user_roles) {
?>
<ul class="roles">
<?php
foreach ($current_user_roles as $role) {
?><li class="roles-item"><?php print $role; ?></li><?php
}
?>
</ul>

Resources