1

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.com/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.

Screenshot of console, page-sr2.php

Note: Yes I am calling logit( $variable, '$variable: '); in both page-sr2.php and in the function page_sr2_pgp inside of functions.php. I am doing that because I don't know which location is correct, and I didn't believe it would cause a problem.

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.