WordPress is an open source PHP based CMS. CMS stands for Content management system. It provied the flexibility to users to build dynamic websites, portals, online store, blog etc.
Inside this article we will see complete information of wordpress global variables from basics.
WordPress Global Variables are pre-defined variables found throughout the WordPress codebase that provide access to critical information and functionality.
These variables are accessible from any location in the WordPress installation, including plugins, themes, and core files.
Learn More –
- How to use WordPress Global Database Object?
- Learn WordPress CRUD Tutorial with wpdb Object
- Learn WordPress Built-in Functions & Custom Queries
- Create a Basic Widget Plugin in WordPress Tutorial
Let’s get started.
How WordPress is very much flexible PHP CMS to work?
WordPress has several features I think a book can be written on it’s features only. This CMS is handled by Technical, non technical users, developers etc. The level of customization is too good. We can customize almost everything in wordpress.
The development in wordpress like plugin development, theme, child theme, widget etc is pretty handy to maintain. I am writing few of features from a list –
- It is very simple.
- Amazing the maintenance of theme. Managing themes is powerful.
- User Management is too good with set of user roles. Also custom role maintenance.
- Easy to manage all medias over posts, pages etc.
- Well managed publishing tools like – Publish, Draft, Schedule.
- Working with wordpress – Default provides PHP based security functions.
- 10000+ available plugins makes the CMS too strong to work and manage every section.
- SEO based and user friendly urls of application.
- Official Documentation is too good. It is very clear to see and implement.
What is a variable and type of scopes?
In simple terms, variable is a container in which we store values. Values can be of any type like it will be string value, integer, decimal point etc. So that’s why we use data type to declare variables .In programming languages variables play very important roles in memory management system.
A variable scope means range of value of a variables can be used. PHP variables can be any of the type of scope
- Local variables
- Global variables
- Static
- Function parameters
So, also WordPress have several variables to use.
What is a Global variable?
A simple variable whose scope is only for a specific function. Any function which have some variables and defined inside any block is termed as local variable or simple variable. Let’s see a simple code snippet of that.
function myBlock(){ $x = 100; // I am a local variable echo $x; // It prints value 100 }
But in case if we want to use this variable $x in some other functions then it will not be available any more. It seems to be a new variable for that function. Here comes the concept of global variable.
function add(){ $number1 = 100; $number2 = 100; $GLOBALS['sum'] = $number1 + $number2; // stored sum in global variable } function otherFunction(){ echo $sum; // It prints the value 200 }
Global variable can be used anywhere in the application.
Understand Global variable Concept in WordPress
In wordpress global variables are available for various reasons and for different different work. While working with wordpress like for any level of customizations this is recommended not to modify any of the core files or global variables. This will affect the behaviour of working wordpress.
To use a global variable in our code, we first need to globalize the variable with the keywords global.
Syntax –
global $variable;
OR
$GLOBALS[‘variable_name’]
Types of Global Variables in WordPress
We have several types of global variables available category wise in wordpress.
- Site Level
- Admin Level
- Browser Detection
- Web Server Detection
- Version Variables
Some of the globals are not in any of the above category but we will discuss those also.
Site Level Globals
- $post
- $page
- $authordata
- $user_ID
global $post OR $GLOBALS[‘post’]
This global is used to get the post data into an object format. I think to understand this global in a proper way – We need to open a post into browser.
Any Post into browser –
Next, to check the working on global $post object – What we have to do ? Back to single.php or singular.php of your activated theme. In my case I have 2020 theme activated. I have opened singular.php page.
In that PHP file, I have added this piece of code –
<?php get_header(); global $post; echo "<pre>"; print_r($post); // OR either we can use $GLOBALS['post'] ?>
We can see the output as –
global $page OR $GLOBALS[‘page’]
It Returns the page ID of current post – viewed page.
global $user_ID OR $GLOBALS[‘user_ID’]
It Returns the userID of logged in User. We can use this anywhere in the application.
<?php get_header(); global $user_ID; echo "<pre>"; print_r($user_ID); // OR either we can use $GLOBALS['user_ID'] ?>
Admin Level Globals
- $pagenow
- $post_type
- $menu
These global variables used by admin panel of wordpress setup.
global $pagenow – When you click on any menu. The current page information what we open is stored in $pagenow variable. Simply we can use this $pagenow global variable to get data. It returns “string” value.
global $post_type – In wordpress every any thing we store like pags, post, medias, custom post they all stored into a single table but post type is different. So global $post_type is used to get the current working post type for the data. It returns “string” value.
global $menu – This global variable returns all menus with the index values what we have created in wordpress theme. It returns all menus in “array” format.
Browser Detection Globals
These globals are very useful when we want to detect the browser. Here we have some available globals which is used for browser detection are –
- $is_iphone – iPhone Safari
- $is_chrome – Google Chrome
- $is_safari – Safari
- $is_NS4 – Netscape 4
- $is_opera – Opera
- $is_macIE – Mac Internet Explorer
- $is_winIE – Windows Internet Explorer
- $is_gecko – FireFox
- $is_lynx
- $is_IE – Internet Explorer
- $is_edge – Microsoft Edge
These all variables return boolean value. If it matches it returns TRUE value else return value will be FALSE.
if($is_chrome){ // come here I will Chrome }else{ // I am different browser }
Web Server Globals
These globals help to detect the web server type. Here we have the list of globals available on the basis of web server type –
- $is_apache – Apache HTTP Server
- $is_IIS – Internet Information Services (IIS)
- $is_iis7 – Internet Information Services (IIS) v7.x
- $is_nginx – Nginx web server
It returns boolean value – Either TRUE or FALSE.
if($is_apache){ // Yes, I am Apache }else{ // Sorry, I am different web server }
Version Related Globals
We have several other globals created to check versions. Like WordPress version , database version etc. Find the list of available version global with their return types.
- $wp_version – The installed version of WordPress (String)
- $wp_db_version – The version number of the database (Integer)
- $tinymce_version – The installed version of TinyMCE (String)
- $manifest_version – The cache manifest version (String)
- $required_php_version – The version of PHP this install of WordPress requires (String)
- $required_mysql_version – The version of MySQL this install of WordPress requires (String)
global $wp_version; echo $wp_version; // It prints the application using wordpress version
WordPress Global Object – Database Operations
To perform any database operations, WordPress provides a class called wpdb which is present in the directory at core libraries wordpress folder /wp-includes and file is /wp-includes/wp-db.php
We can create an object of this wpdb class and perform all database operations but wordpress auto creates an object of this class while wordpress loads.
global $wpdb;
function myFunction() { global $wpdb; //database operations - code }
Available Methods – Find / Search data
These are those methods which we use to select data from database table. Methods like to select a single value, a complete row, column values and all results data set.
- $wpdb->get_var
- $wpdb->get_row
- $wpdb->get_col
- $wpdb->get_results
If you want to know all these methods and their usage in detailed click here to go.
Available Methods – Insert, Update & Delete Operations
This is also a list of data but this time instead of select we use to insert data into table, update any data or delete any specific record.
- $wpdb->insert
- $wpdb->update
- $wpdb->delete
For more information about all these methods click here to go.
We hope this article helped you to learn about WordPress Global Variables in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.