In this three part series, we're going to be taking a look at WordPress global variables in order to gain a better understanding of them such that we can apply them to our day-to-day development.
Throughout the series, we will be going through a set of hands-on exercises that demonstrate the power of the global variables to show and to tweak the inner workings of WordPress.
What Are WordPress Global Variables?
A WordPress global variable is a variable that holds information generated by the application. These global variables can be accessed during the execution of the application and during the page life cycle.
These variables hold various pieces of information such as if the user is logged in, the browser in which the user is visiting the website, and so on. There are variables that hold the details of the web server, and variables that hold the details of the current post. The values of the global variables can be accessed to understand the inner workings of WordPress in order to begin introducing new functionality in themes and plugins.
How Do We Access Global Variables?
In our first example, we will be using the $post
global variable that hold various details about the current post data.
Every global variable can be accessed by globalizing the variable as follows:
<?php global $post; ?>
By using the global
keyword, we are declaring that we are accessing the $post
global variable. Once the variable has been globalized, the various values available to be accessed can be retrieved using the following source code:
<?php global $post; print_r ($post );
The $post
global variable contains various data about the most recently loaded post. Go ahead and insert the above snippet into page.php
of the twentyfourteen theme (though I do recommend backing up the theme prior to making these changes).
Paste the above snippet right after the get_template_part( ‘content’, ‘page’ )
which fetches the content and displays it on the page. This is what we should have until now on the page.php
:
<div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php // Start the Loop. while ( have_posts() ) : the_post(); //Include the page content template. get_template_part( 'content', 'page' ); // Paste Snippet Here global $post; print_r( $post ); // End Snippet // If comments are open or we have at least one comment if ( comments_open() || get_comments_number() ) { comments_template(); } endwhile; ?> </div><!-- #content --> </div> <!-- #primary -->
Now that we have the above code to expose the data of the global $post
variable, save the file and go ahead and open any page after confirming that the current theme is twentyfourteen. If not, go ahead and make it the current theme.
Now observe the details printed by WordPress, we should see something like this :
As you can see we now have various details of the latest post printed in a human readable format from the $post
global variable. We can access each of the individual values by invoking them as follows:
<?php echo $post->post_date; // To Print the Date of the Post echo $post->post_status; // To print the status of the Post
We have merely scratched the surface as there are other exciting global variables that can be utilized; however, but remember that the global variables are to be accessed for displaying the values. If we tamper with the values they contain then we risk causing unpredictable behavior .
What's Next?
In this session we took a look at global variables and a basic example of one of the more common variables that exists in WordPress.
In the follow up tutorial we will see how to use the author global variable to learn more about the WordPress global variables.
Comments