Using global variable within WordPress

Within my header.php file, I need to set the body ID dynamically depending on the currently used template for whatever page someone happens to be viewing.

If someone is viewing a page that uses template A the body tag should look like this:

Read More
<body id="page1">

If someone is viewing a page that uses template B the body tag should look like this:

<body id="page2">

What would be the downside to me setting the following variable within my themes functions.php

$GLOBALS['my_current_body_id'];

Then at the beginning of each template I set the following:

$GLOBALS['my_current_body_id'] = '1';

or

$GLOBALS['my_current_body_id'] = '2';

and so on depending on how I need to style and structure the particular page.

Then in my header.php file I do the following:

<body id="page<?php echo $GLOBALS['my_current_body_id']?>">

I am wondering if there is a problem with using this type of approach that I might be overlooking.

For example, if two people hit the site at the exact same time, could there be a mix up or is the $GLOBALS[‘my_current_body_id’] specific to the user viewing the page?

Thanks.

Related posts

Leave a Reply

2 comments

  1. How about this solution:

    PHP:

    function body_tag() {
        global $post;
        $pageid = $post->ID; // Get page ID
        switch($pageid) {
            case '1':
            $bodyid = '1';
            break;
    
            case '2':
            $bodyid = '2';
            break;
    
            case '3':
            $bodyid = '3';
            break;
    
            default:
            $bodyid = '';
        }
        // Based on the page ID, set the appropriate bodyid and then return it.
    
        return $bodyid;
    }
    

    Header.php:

    <body id="page<?php echo body_tag(); ?>">
    

    The code I wrote feels a bit more simple, however…
    If user A and user B are viewing the same page, they will see the same template.
    But if user A is viewing the page with ID 4 and user B is viewing the page with ID 5, then they will see different templates.
    Are you trying to make a plugin for A/B testing?

  2. Here’s a couple methods, depending on what’s needed:

    1) To grab the ‘post_name’ of the direct parent to the Page:

    <?php
    $page = $wp_query->post;
    $parent_name = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE ID = '$page->post_parent;'");
    ?>
    

    And to display the parent Page’s name:

    <body id="<?php echo $parent_name; ?>">
    

    2) To collect it for the top parent, that is the one at the top of a Page parent<>child heirarchy, we can modify the code from jpepper’s link:

    <?php
    $current_page = $post->ID;
    $parent = 1;
    
    while($parent) {
    $page_query = $wpdb->get_row("SELECT post_name, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    $parent = $current_page = $page_query->post_parent;
    if(!$parent)
    $parent_name = $page_query->post_name;
    }
    ?>
    

    Then use the same method as above to display the name.
    For better way you can simply use it a function in functions.php and use it in header.php file.

    Source : https://wordpress.org/support/topic/dynamic-id-for-ltbodygt-and-subpages