Understanding wordpress global variables

In this tutorial, we will share about to understanding wordpress global variables with an examples. WordPress-specific global variables are used throughout WordPress code for various reasons. Almost all data that WordPress generates can be found in a global variable.

To access a global variable in your code, you first need to globalize the variable using global $variable;

Inside the Loop variables Global Variables

  • $post (WP_Post): The post object for the current post. Object described in WP_Post Class Reference.
  • $posts: Used by some core functions, not to be mistaken for $query->$posts.
  • $authordata (WP_User): The author object for the current post. Object described in WP_User Class Reference.
  • $currentday (string): Day that the current post was published.
  • $currentmonth (string): Month that the curent post was published.
  • $page (int): The page of the current post being viewed. Specified by the query var page.
  • $pages (array): The content of the pages of the current post. Each page elements contains part of the content separated by the <!--nextpage--> tag.
  • $multipage (boolean): Flag to know if the current post has multiple pages or not. Returns true if the post has multiple pages, related to $pages.
  • $more (boolean): Flag to know if WordPress should enforce the <!--more--> tag for the current post. WordPress will not enforce the more tag if true.
  • $numpages (int): Returns the number of pages in the post, related to $pages.

Browser Detection Booleans Global Variables

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

Web Server Detection Booleans Global Variables

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

  • $wp_version (string): The installed version of WordPress
  • $wp_db_version (int): The version number of the database
  • $tinymce_version (string): The installed version of TinyMCE
  • $manifest_version (string): The cache manifest version
  • $required_php_version (string): The version of PHP this install of WordPress requires
  • $required_mysql_version (string): The version of MySQL this install of WordPress requires

MiscellaneousGlobal Variables

  • $super_admins (array): An array of user IDs that should be granted super admin privileges (multisite). This global is only set by the site owner (e.g., in wp-config.php), and contains an array of IDs of users who should have super admin privileges. If set it will override the list of super admins in the database.
  • $wp_query (object): The global instance of the WP_Query class.
  • $wp_rewrite (object): The global instance of the WP_Rewrite class.
  • $wp (object): The global instance of the WP environment setup class.
  • $wpdb (object): The global instance of the wpdb class.
  • $wp_locale (object): The global instance of the WP_Locale class.
  • $wp_admin_bar (object): The global instance of the WP_Admin_Bar class.
  • $wp_roles (object): The global instance of the WP_Roles class.
  • $wp_meta_boxes (array): Object containing all registered metaboxes, including their id’s, args, callback functions and title for all post types including custom.
  • $wp_registered_sidebars (array)
  • $wp_registered_widgets (array)
  • $wp_registered_widget_controls (array)
  • $wp_registered_widget_updates (array)

Admin Global Variables

  • $pagenow (string): Used in wp-admin.
    See also get_current_screen() for the WordPress Admin Screen API.
  • $post_type (string): Used in wp-admin
  • $allowedposttags (array)
  • $allowedtags (array)
  • $menu (array)

Reference:  https://developer.wordpress.org/apis/handbook/global-variables/

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts:

Prem Tiwari

An engineer by profession and a passionate blogger by heart. Founder of FreeWebMentor.com (A Programming blog for beginners), Tech Speaker at various forums. A part from this he is an open source enthusiast, WordPress Lover, Blogger, SEO, and Growth Strategic.