0

I need to initialize some variables in various templates (index, single, page) such as banner image of that page. But then use that value in the 'get_header()' template. A good example of this is the og_image that's usually in the header.php set of meta tags inside the head of the HTML. This image is the visual moniker for any given template page. So the best place to get this info is in the context of the main loop of that template. However the og_image tag itself is not inside that template (such as single.php) but inside the header.php.

Setting a variable to "global" inside single.php doesn't help because these are not just straight includes as in regular Php. They are somehow more wordpress specific.

The other option could be to do some black magic inside functions.php, but for something this straightforward, I would prefer not to overdo some function. Is there an easier way or best practice to share variables' values across headed and footer and sidebar?

2
  • black magic? you can just declare the var in functions.php and thats it
    – David Lee
    Commented Feb 9, 2017 at 22:43
  • I can declare them as global in functions.php, but I am not planning to set their value in functions.php. The setting of these values is done inside index.php, and then when get_header is called inside index.php much further down, I want the value to be used inside header.php. I tried setting a global function as in the answer below by JVD, but that does NOT work.
    – PKHunter
    Commented Feb 11, 2017 at 3:58

2 Answers 2

1

Using Custom Fields:
You can use custom fields for each page, if you need something more specific (maybe a product where you need an specific image for each page), taking the og values for example:

1.- Go to the edit page and check the custom fields checkbox so it can be seen below the editor

enter image description here

2.- Enter a new custom field

enter image description here

enter image description here

it should look like this, the name is your $key

enter image description here

3.- Save or Update the page.

4.- Add this code to your functions.php

function opengraph_tags() {

            /* we set the URL */
            $og_url = get_permalink();
            /* we set the site name */
            $og_site_name = get_bloginfo();
            /* we set the image */
            /* we check for a featured image */
            global $post;
            $og_img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
            $key = 'description';
            /* MY CUSTOM VALUE I WANT TO PUT FROM THE CONTENT TO THE HEADER */
            $og_description = get_post_meta($post->ID, $key , true);

        ?>
        <meta property="og:description" content="<?php echo $og_description; ?>"/>
        <meta property="og:type" content="website" />
        <meta property="og:url" content="<?php echo $og_url ?>"/>
        <meta property="og:site_name" content="<?php echo $og_site_name ?>"/>
        <meta property="og:image" content="<?php echo $og_img_src[0]; ?>"/>
        <?php
    }

add_action('wp_head', 'opengraph_tags', 5);

you can see i am grabbing the custom field with $key which can be anything you want, and also you can grab all the custom values you want, provided that you created them, you can set default values there too with a simple if(empty($myValue)), if you inspect the header you will see something like this:

enter image description here

Now if you want to use other values in header.php or even footer.php just set the custom field and use this code to grab the value:

<?php 
global $post;
$key = 'description';
/* MY CUSTOM VALUE I WANT TO PUT FROM THE CONTENT TO THE HEADER */
$og_description = get_post_meta($post->ID, $key , true);
?>
3
  • Thank you, this is helpful. Unfortunately this requires wp_head() function to be activated in my header.php, which I absolutely abhor as many plugins insert their rubbish and I don't have the time to figure out the dequeue script for each one of them. Also, this relates only to inserting things in the header for my specific use case. I'm interested in a more "global" way of having variables across the entire theme scope, so that any file can access them. Putting them in functions.php does NOT work.
    – PKHunter
    Commented Feb 11, 2017 at 9:30
  • The use of wp_head for the action its just an example, the last block of code can be put wherever you want, even with a custom hook only for your purpose, there is no need to go into each theme file and put code there to modify the header, everything can be done in functions.php, so you need the var to be available globally only there, since you can get anything there.
    – David Lee
    Commented Feb 11, 2017 at 17:56
  • This is very helpful David. Much appreciated.
    – PKHunter
    Commented Feb 11, 2017 at 19:40
1

There is no black magic it is incredibly easy.

function globalvariabletest() {
    global $testvar;
    $testvar = 'hello world';
}
add_action( 'after_theme_setup', 'globalvariabletest' );
2
  • Just to clarify: I don't want to set the value of this variable here. I can declare it here, sure, but the value is to be set it in index.php or single.php. Then their respective get_header() function calls, ie the header.php, should refer to the value set in each file.
    – PKHunter
    Commented Feb 11, 2017 at 3:59
  • 1
    so just declare it here and then set the value wherever you want to.
    – JVD
    Commented Feb 12, 2017 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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