Getting and Using Global Variables in WordPress

Like other PHP systems, WordPress defines some global variables to facilitate data access. Basically, all data generated by WordPress can be found in global variables. Understanding which global variables are available in WordPress can help us better understand WordPress data, thereby gaining a deeper understanding of WordPress and laying a solid foundation for WordPress theme development.

Default Global Variables in WordPress

Global Variables Available in the Loop

In the WordPress Loop, global variables are often pre-fetched, and we can use them directly. These global variables contain information about the current post in the Loop.

$post (WP_Post) The current post object
$authordata (WP_User) The current post author object
$currentday (string) The publishing day of the current post
$currentmonth (string) The publishing month of the current post
$page (int) The current page of the post being accessed, defined by the page query parameter
$pages (array) Paging information for the current post, with each element containing a part separated by the <!--nextpage--> tag
$multipage (boolean) Whether the current post is a multi-page post, detected based on the $pages parameter above
$more (boolean) Whether WordPress executes the <!--more--> tag's mark; if true, WordPress will not execute the more tag
$numpages (int) Returns the total number of pages for the current post, related to the $pages above

Detecting Browser Boolean Values

The following global variables store detection information about the user’s browser as boolean values, which can be used to identify the browser through which the user is accessing the site.

$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) Firefox
$is_lynx (boolean) Lynx
$is_IE (boolean) Internet Explorer
$is_edge (boolean) Microsoft Edge

Detecting Server Boolean Values

The following global variables store information about the website server, which can be used to determine the type of server running the site.

$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 Variables

The following variables store version information within WordPress.

$wp_version (string) Currently installed 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 for the installed WordPress version
$required_mysql_version (string) Minimum MySQL version required for the installed WordPress version

Other Global Variables

$super_admins (array) User IDs possessing super administrator privileges; this global variable is only relevant for site owners
$wp_query (object) WP_Query class instance
$wp_rewrite (object) WP_Rewrite class instance
$wp (object) WP class instance
$wpdb (object) wpdb class instance
$wp_locale (object) Localization information
$wp_admin_bar (WP_Admin_Bar) Admin Bar object
$wp_roles (WP_Roles) WordPress roles object
$wp_meta_boxes (array) Registered metaboxes objects, including their IDs, parameters, callback functions, titles, etc.
$wp_registered_sidebars (array) Registered sidebar areas
$wp_registered_widgets (array) Registered widgets
$wp_registered_widget_controls (array) Registered widget fields
$wp_registered_widget_updates (array) Registered widget updates

Backend Global Variables

$pagenow (string) Used in wp-admin, also refer to get_current_screen() for understanding the WordPress Admin Screen API
$post_type (string) Used in wp-admin, the post type of the current page
$allowedposttags (array) Allowed post tags
$allowedtags (array) Allowed tags
$menu (array) WordPress backend menu data

Accessing Global Variables in WordPress

We can use global variables directly via the method below. Each WordPress global variable can also be accessed through its corresponding function; WordPress officially recommends using functions to retrieve these global variables.

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

Getting All Global Variables in WordPress

Besides global variables registered by the WordPress core, some themes and plugins might also register global variables. If you need to see all global variables already registered in the system, you can check them via the following code.

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

Related Posts

Leave a Reply

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