Get and Use Global Variables in WordPress

Like other PHP systems, WordPress defines a number of global variables that make data easier to access. Almost all of the data generated by WordPress can be found somewhere in those globals, so knowing what is available can help you understand WordPress more deeply and build stronger theme or plugin logic.

Default global variables in WordPress

Global variables available inside the Loop

Inside the WordPress Loop, several globals have already been prepared for the current post, so they can be used directly.

$post (WP_Post) current post object
$authordata (WP_User) current post author object
$currentday (string) current post date
$currentmonth (string) current post month
$page (int) current page number for a paged post
$pages (array) current post pagination parts split by <!--nextpage-->
$multipage (boolean) whether the current post is split into multiple pages

Browser-detection boolean globals

The following globals store browser-detection information and can be used to detect the visitor’s browser type.

$is_iphone (boolean) iPhone Safari
$is_chrome (boolean) Google Chrome
$is_safari (boolean) Safari
$is_NS4 (boolean) Netscape 4
$is_opera (boolean) Opera
$is_macIE (boolean) Mac Internet Explorer
$is_winIE (boolean) Windows Internet Explorer
$is_gecko (boolean) Gecko-based browser

Server-detection boolean globals

WordPress also exposes globals about the current web server environment.

$is_apache (boolean) Apache HTTP Server
$is_IIS (boolean) Microsoft Internet Information Services (IIS)
$is_iis7 (boolean) Microsoft Internet Information Services (IIS) v7.x
$is_nginx (boolean) Nginx web server

Version globals

These variables store version information used by WordPress itself.

$wp_version (string) current WordPress version
$wp_db_version (int) current database version
$tinymce_version (string) TinyMCE version
$manifest_version (string) cache manifest version
$required_php_version (string) minimum PHP version required by this WordPress installation
$required_mysql_version (string) minimum MySQL version required by this WordPress installation

Other global variables

$super_admins (array) IDs of super admins
$wp_query (object) WP_Query instance
$wp_rewrite (object) WP_Rewrite instance
$wp (object) WP instance
$wpdb (object) wpdb instance
$wp_locale (object) locale information

Admin globals

$pagenow (string) current admin page filename
$post_type (string) current post type in wp-admin
$allowedposttags (array) allowed post tags
$allowedtags (array) allowed tags
$menu (array) admin menu data

Access global variables in WordPress

You can access WordPress globals directly with the global keyword. In many cases, WordPress also provides a function that returns the same information, and the official documentation usually prefers the function-based approach.

global $wp_version;

// Or:
$wp_version = get_bloginfo( 'version' );
echo $wp_version;

Inspect all global variables in WordPress

In addition to the globals registered by WordPress core, themes and plugins may register their own globals as well. If you need to inspect everything currently registered in the system, you can output the entire $GLOBALS array.

echo "<pre>";
print_r( $GLOBALS );
echo "</pre>";

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *