• I want to pass variables from single.php to sidebar.php

    When the sidebar is called in the single.php code, it’s written like this:

    get_sidebar();

    are the () for passing variables? how do I write it?
    get_sidebar($parameter);
    like this?
    How do I call it in sidebar.php ?

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello Hatul666,

    The get_sidebar(); only accepts one parameter and that is the name parameter which can be found here on the codex.

    Here is an example:

    Create a new file within your root folder and name it sidebar-left.php. Enter the HTML/CSS/PHP that you need to display the sidebar the way you need it to look. Then on the pages you want to display this sidebar you will simply call get_sidebar(‘left’); The ‘left’ corresponds with the -left.php on the file name.

    Hopefully that helps you out…if not I am happy to help further.

    Thread Starter Hatul666

    (@hatul666)

    Thank you ??

    but still, I’m not sure how to pass vars from single.php to sidebar.php

    Hello Hatul666,

    You can pass variables between template files by using PHP globals:
    https://php.net/manual/en/language.variables.scope.php

    In your single.php file set a variable

    $GLOBALS['some_name'] = $some_var;

    And in your sidebar.php

    $some_var = $GLOBALS['some_name'];

    Just be careful, the sidebar is called from multiple templates so there will be cases where the $GLOBALS[‘some_name’] is not set. You might want to check before doing anything fancy with the variable.

    Good Luck!

    Don’t use globals in WordPress. They may not work predictably. The last time this was discussed, one suggestion was to use transients.

    Globals are perfect for this task, and WordPress itself uses them all the time. Not sure why you are saying that, mr. Esmi. Transients seem like a rather WordPress-specific way of storing (cached) data, not at all what this question asker wants.

    You can, for instance, also define globals in your functions.php.

    A global in PHP is nothing other than a variable that has been defined in the default scope and which can be accessed in other PHP files as well as in functions and classes and the like, by simply stating a globals declaration for that variable.

    global $myvariable;

    That is all there is to it. It has nothing to do with WordPress proper.

    Here are some globals you cannot use:

    global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;

    Note that template files are included in a way that makes variables declared in e.g. header.php not accessible in index.php, but they are the other way around (define in index.php, do a global declare in header.php).

    If you use the $GLOBALS[‘name’] like Simalam suggests, it should always work. WordPress has (or really should) have no way whatsoever to disrupt any globals you use or declare other than when you use or define a variable that WordPress already uses itself.

    There are some more globals you cannot use, like $page, $numpages, $more, that can result from the query being set up. But if things start to act weird, you know what’s up.

    To find all the currently defined symbols in the global scope, do:

    print_r(array_keys($GLOBALS));

    Just because they are defined doesn’t mean WordPress necessarily uses them, they may simply not have been unset after they were no longer required.

    Just use common sense and all will work out fine..

    The only time something fishy started happening to me is not because I used a global, but because I had aliased a symbol to something that already existed (like $a =& $GLOBALS[‘wp’]) and then re-used that symbol (variable name) while it was still bound to that other variable :P.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘pass variables in wordpress’ is closed to new replies.