Problem Summary:
I am using firePHP to analyze my variables in a custom function, but it does not display their contents into the console as I would expect. I also used var_dump(), it returns NULL.
Problem Details:
I am using firePHP exactly as described by @brasofilo as seen on . It is normally very helpful, but I am confused why sometimes I cannot see contents of my variables in the console.
Here is a specific example, this is in page-sr2.php:
<?php get_header(); ?>
<div id="main">
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php get_template_part('nav_menu', 'explore');?>
<?php logit( $wp_query, '$wp_query:' ); ?> /*Logit function from @brasofilo*/
<?php logit( $variable, '$variable: '); ?>
<?php var_dump($variable); ?>/* This outputs NULL on the page*/
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
<?php// get_sidebar(); ?>
<div class="clear"></div>
</div><!-- #primary -->
</div>
<?php get_footer(); ?>
</div>
In functions.php I am using a custom pre_get_posts action:
add_action('pre_get_posts', 'page_sr2_pgp');
function page_sr2_pgp( $query )
{
if( is_page('sr2') ){
return $query;
}
//global $variable; //tried this, didn't help
$variable ="something";
logit( $variable, '$variable: ');
if( isset($_GET['yr']) )
{
$query->set('meta_key', 'yr');
$query->set('meta_value', $_GET['year']);
}
// always return
return $query;
}
The result is that logit()
works for $wp_query
, but not for $variable
.
Note: Yes I am calling
logit( $variable, '$variable: ');
in bothpage-sr2.php
and in the functionpage_sr2_pgp
inside offunctions.php
. I am doing that because I don't know which location is correct, and I didn't believe it would cause a problem.
Problem Summary:
I am using firePHP to analyze my variables in a custom function, but it does not display their contents into the console as I would expect. I also used var_dump(), it returns NULL.
Problem Details:
I am using firePHP exactly as described by @brasofilo as seen on https://wordpress.stackexchange/a/71599/41479 . It is normally very helpful, but I am confused why sometimes I cannot see contents of my variables in the console.
Here is a specific example, this is in page-sr2.php:
<?php get_header(); ?>
<div id="main">
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php get_template_part('nav_menu', 'explore');?>
<?php logit( $wp_query, '$wp_query:' ); ?> /*Logit function from @brasofilo*/
<?php logit( $variable, '$variable: '); ?>
<?php var_dump($variable); ?>/* This outputs NULL on the page*/
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
<?php// get_sidebar(); ?>
<div class="clear"></div>
</div><!-- #primary -->
</div>
<?php get_footer(); ?>
</div>
In functions.php I am using a custom pre_get_posts action:
add_action('pre_get_posts', 'page_sr2_pgp');
function page_sr2_pgp( $query )
{
if( is_page('sr2') ){
return $query;
}
//global $variable; //tried this, didn't help
$variable ="something";
logit( $variable, '$variable: ');
if( isset($_GET['yr']) )
{
$query->set('meta_key', 'yr');
$query->set('meta_value', $_GET['year']);
}
// always return
return $query;
}
The result is that logit()
works for $wp_query
, but not for $variable
.
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Aug 15, 2014 at 20:56 Daniel DropikDaniel Dropik 2831 gold badge4 silver badges13 bronze badgesNote: Yes I am calling
logit( $variable, '$variable: ');
in bothpage-sr2.php
and in the functionpage_sr2_pgp
inside offunctions.php
. I am doing that because I don't know which location is correct, and I didn't believe it would cause a problem.
1 Answer
Reset to default 0It display NULL
, because it doesn't exist.
If you want to use global variables in another function than where you defined it, you have to re-declare that as global again. If you omit global keyword, it will be defined in that function scope, not in global scope.
In your functions.php
use global
keyword
add_action('pre_get_posts', 'page_sr2_pgp');
function page_sr2_pgp( $query )
{
...
...
//uncomment this line
global $variable;
$variable ="something";
logit( $variable, '$variable: ');
...
...
return $query;
}
In page-sr2.php
, re-declare as global
<?php get_header(); ?>
<div id="main">
...
...
<?php logit( $wp_query, '$wp_query:' ); ?> /*Logit function from @brasofilo*/
<?php global $variable; ?>
<?php logit( $variable, '$variable: '); ?>
...
...
</div>