1. Code
  2. WordPress

An Introduction to WordPress Global Variables

Read Time:3 minsLanguages:
This post is part of a series called WordPress Global Variables.
A Practical Use of WordPress Global Variables

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:    

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:

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:

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:

global variablesglobal variablesglobal variables

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:

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.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.