It 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>