Global Variables of WordPress

We often use functions to retrieve some data on a page and are unaware that this data can be taken from global variables. For example, the name of the author on the author page. And sometimes we just don't know how to get the data we need, where they are. This article will tell about how to get all the important data of the current page from the variables of PHP, which are already defined in WordPress. Such data only remains to take.

I should note that if there is an equivalent option to get the data through a function, it's better to get it through a function. Because it can get rid of bugs in the future - when you update in future versions, the name of the global variable may change. And it is unlikely that the name of the function will change. Besides, changed functions are not deleted but get moved to the forbidden section, but still continue to work. Here is an example:

// The current user can be accessed via the global variable
global $current_user;

// or through a function, which is preferable
$current_user = wp_get_current_user();

What are global variables?

These are PHP variables that are "visible" everywhere.

To access a global variable, you have to prefix it with the global keyword:

global $cat;
echo $cat;

Or you can use the super-global array $GLOBALS:

<?php echo $GLOBALS['cat']; ?>

Global variables are ordinary variables that are defined in the global scope (main part) of PHP. To understand what a global scope is, imagine the very first php file, usually index.php - this is the global code area (global scope). If you define a variable in such a file, it will automatically become global. But if you create a function within such a file and define a variable inside of that function, it will be local -- the scope of such a variable does not go beyond the function. The same applies to classes and their methods (functions).

And what if another file is included in the "global" file? Suppose we include another file wp-load.php - require_once 'wp-load.php; to the main file index.php. The code of this connected file also becomes global, and the variables directly defined in this file automatically becomes global.

Let's look at an example. Suppose we insert the code below into the functions.php theme file, which in turn is included in the global scope, and any variables specified directly in it become global.

$var = 'Hello world!'; // this variable becomes global

// function
function my_func(){
	echo $var;
my_func(); // call: we will get a Notice level error,
		   // because there is no $var variable in the scope of the function

// function
function my_func2(){
	global $var; // define $var globally

	// now $var is a global variable

	echo $var; // hello world!

	// let's change the variable, it's a global variable
	$var = 'Bye-bye.';
my_func2(); // print: 'hello world!'

// print variable $var outside of functions
echo $var; //will print: 'Bye-bye.'
		   //because we changed global $var in function my_func2()

This code explains the essence of global variables, how they are visible and not visible within functions.

A global variable can be created from a local scope, for example from a function:

echo $var; // this will cause an error - the variable is not defined

function my_func2(){
	global $var; // create a global variable $var

	$var = 'hello world';
my_func2(); // call the function

echo $var; // print 'Hello world'

Note: The global keyword exposes a reference to the specified variable, rather than creating a new one. The following constructions are equivalent:

global $var;

// the same as

$var = & $GLOBALS['var'];

Important: Use global variables with caution!

There are a lot of global variables and they are easy to change. For example, if you write $id = 2; at the beginning of header.php theme files, the global variable $id which contained the ID of the current entry will change and cannot be used!

Similarly, the theme or plugins can change global variables. But still, there are some "defined" variables, that every developer should be aware of, and not use them as a global variable when creating a plugin or theme. This is exactly the list you will find in this article.

I do not recommend you to use variables not mentioned in this article in your code. You should find an alternative way to get the required data instead, e.g. using the appropriate function.

Global Variables in WordPress

During page generation, WordPress gets a bunch of data and important data is stored in global variables. For example:

  • The category ID on the category page is $cat.
  • Or all the posts received on a category page - $wp_query.
  • Or the post object on the post page - $post.

So, WordPress creates a bunch of global variables that you can use in your code. Let's look at the most important ones below.

Common variables

Stores everything about URL rewrite rules. Global class instance WP_Rewrite. Installed in the wp-setings.php file before the setup_theme event.
Stores all connected and put to output scripts. Global instance WP_Scripts.
Stores all connected and rendered styles. Global instance WP_Styles.
Stores the current connection to the database. Global class instance wpdb. Allows communication with the database.
Prefix a table in the database.
All data of the admin bar.
Object of all registered metaboxes. Contains their IDs, parameters (args), callback functions and headers of all record types, including arbitrary ones.
All data about registered areas for the sidebar.
Data of all registered widgets.
Allowed HTML tags when publishing an entry. See file for list wp-includes/kses.php.
Allowed HTML tags when posting comments. See the list in the file wp-includes/kses.php. See allowed_tags()
All WordPress hooks.
An array with event names in the index and the number of times the event was triggered in the value.
The entire object cache. It's stored this way when the object cache plugin isn't installed... Global class instance. WP_Object_Cache.
All registered shortcodes and their data.

The class responsible for embedding objects. For example, when HTML code of youtube video link is created in the text of an article. The variable contains an instance of the class WP_Embed.

This class is based on the class WP_oEmbed - It contains the basic functionality of the embeds, and WP_Embed is a kind of wrapper. You can get the WP_oEmbed object through a function: _wp_oembed_get_object(). Separately, this object has no global variable.

There is no such global variable! You can get the WP_oEmbed object through a function: _wp_oembed_get_object().
Data of all taxonomies.
Data of all record types. Object array WP_Post_Type
An array of all translations. Contains MO class objects obtained from .mo files. See load_textdomain().
An array of domains for which translations have been unloaded (cancelled). An array with elements: 'domain' => true. See unload_textdomain().
An array of data about all the statuses of entries. publish, future, etc.
The data object of the current user is an instance of WP_User. Some of the data is stored in separate global variables for quick access: $user_login, $user_ID, $user_email, $user_url, $user_identity (Usually login).
All data of user roles on site.
An array of special strings that can be translated into emoticons. Translate text into smilies. :x into smiley, :| Into smiley, etc.
An array of IDs of users who have extra super-administrator rights in MU build. This global variable is set by the site owner (e.g. in the wp-config.php file). If set, the variable overwrites (overwrites) the list of super-admins in the database.
Sets the width of the site content. Used in the file functions.php.

An instance of the selected object extending the class WP_Filesystem_Base, for working with files on the server. An instance can be one of the classes: WP_Filesystem_Direct, WP_Filesystem_FTPext, WP_Filesystem_ftpsockets, WP_Filesystem_SSH2.

Variable set by the function WP_Filesystem().

An array containing the paths to the folders where the themes are located. Usually there is only one path. The variable is created with the function register_theme_directory().

Localization (translation)

Data of the current site locale (site language). Global instance класса WP_Locale.
Site locale, for example ru_RU.
Array of all days of week translated to current locale. [ __('Sunday'), __('Monday'), ... ]
Array of first letters of all days of week translated to current locale. [ __('Sunday') => __('S'), __('Monday') => __('M'), ... ]
An array of short names of all days of week translated to current locale. [ __('Sunday') => __('Sun'), __('Monday') => __('Mon'), ... ]
An array of all months translated to the current locale. [ __('January'), __('February'), ... ]
An array of abbreviated names of all months translated to the current locale. [ __('January') => __('Jan'), __('February') => __('Feb'), ... ]

Main WordPress query

IMPORTANT: Don't use the names of the variables listed below in your code, because they are used by the WordPress core. And if you do use them, you should understand what you're doing.

The main request is made by the wp() method WP::main().

Stores the current request of WordPress itself - an instance of WP class, which contains the request parameters obtained from the URL.

The variables below are set by WP::register_globals(), which is called from WP::main().


Stores result of global WP query - instance of WP_Query class. It is set by function query_posts().

$wp_the_query must always be equal to $wp_query. If someone suddenly used query_posts() instead of get_posts()) in their code, the $wp_the_query variable allows you to return the value of $wp_query to the correct state, see. wp_reset_query().

The string (parameters) of the current query.
All the posts of the main WP_Query.
The WP_Post object of the current post. If the page implies output of posts or a post.
SQL query to get all posts for the current page.
Equals 1 when the condition is triggered - is_single() || is_page().
Whether the current page is the post page. Equals 1 when the condition is triggered - is_single() || is_page().
User data get_userdata(). Set only on the author archive page is_author().

The WP::register_globals() method also extracts all variables of the main WP_Query into global variables:

// Extract updated query vars back into global namespace.
foreach ( (array) $wp_query->query_vars as $key => $value ) {
	$GLOBALS[ $key ] = $value;

See WP_Query::parse_query() and WP_Query::fill_query_vars():


Inside the WordPress loop

Inside the posts loop

You can use these global variables inside the WordPress loop. They change as you go through the items in the loop. They all contain information about the current entry (post) in the loop.

They are set by WP_Query::setup_postdata(). Or a wrapper for this method setup_postdata().

The object of the current post. On the record page it is the record itself. On the headings page it is the current entry in the loop.
The object of the author of the current post. The object is described in WP_User.
The day when the current post was published.
The month when the current post was published.
The page of the current post, when the post is paginated Using the tag. <!--nextpage-->.
The content pages of the current entry (post). Each page is paginated here using the tag <!--nextpage-->.
Determines whether the current post is paginated using the <!--nextpage-->. Contains true or false.
Number of pages if the entry is paginated with a tag <!--nextpage-->.
Whether there is a tag <!--more--> in the post content.

In the comments loop

The object of the current comment in the loop. See the description for an example of such an object get_comment()

In the Front Part (Front-end)

The path to the template file that is responsible for outputting the HTML code of the current page. For example: C:/sites/

In the Admin Panel

The data of the current admin screen. See. get_current_screen()

Determines what page we are on now - the current page of the admin panel. Stores the name of the php file that handles the current page. For example options-general.php. It takes the last element taken from $_SERVER['PHP_SELF'] and standardizes the value where needed.

The variable is set early, immediately after the event muplugins_loaded, but before the event plugins_loaded.

The variable is also set at the frontend, only there it almost always contains index.php or wp-login.php.

Record type in the admin. On the record or taxonomy page.
Array of data with the admin menu items that were added via add_menu_page(). Contains an Array of data arrays for each menu.
Array of data with the admin sub-menu items that were added via add_submenu_page(). Contains an Array of data arrays of each sub-menu.

The main menu items of the admin menu in the array. Where the array index is the menu ID, which is specified in the fourth parameter add_menu_page().

It is almost the same as $menu, only it is an index array.

	[index.php]               => dashboard
	[separator1]              => separator1
	[upload.php]              => media
	[link-manager.php]        => links
	[edit-comments.php]       => comments
	[edit.php]                => posts
	[edit.php?post_type=page] => pages
	[separator2]              => separator2
	[themes.php]              => appearance
	[plugins.php]             => plugins
	[users.php]               => users
	[tools.php]               => tools
	[options-general.php]     => settings
	[separator-last]          => separator-last
	[woocommerce]             => woocommerce

Version Variables

The current version of WordPress.
Current version of the database.
Current version of the TinyMCE editor.
Cache manifest version.
The minimum version of PHP, which is required for the current WordPress.
The minimum MySQL version required for the current WordPress.

Browser Variables

These variables contain data about the current browser the user is using.

iPhone Safari.
Google Chrome.
Netscape 4.
Mac Internet Explorer.
Windows Internet Explorer.
A global variable defined by WordPress and used to determine if the current browser is Internet Explorer.
Microsoft Edge.

Web Server Variables

These global variables contain data about which server WordPress is running on.

Apache HTTP Server.
Microsoft Internet Information Services (IIS).
Microsoft Internet Information Services (IIS) v7.x.


ID of the current blog.