WordPress functions: global variables $post, $page, $paged and $pages, etc

WordPress has many built-in global variables, such as $post, $page, $paged and $pages. Almost all the data generated by WordPress can be found in the global variables. Reasonable use of these variables can bring a lot of convenience to WordPress development.

How to use global variables

Accessing global variables in WordPress

If you want to access a global variable in your code, the first thing to do is to declare the variable as global: Global $variable;

In addition, each global variable of WordPress can also be obtained through the corresponding function. The official recommendation of WordPress is to obtain these global variables through the function.

global $wp_ Version; / / method 1 is declared with global in advance

//Or

$wp_ version = get_ Bloginfo ('version '); / / method 2 uses the corresponding function

echo $wp_ version;

Pay attention to get_ query_ Var() only gets WP_ Query can identify public query parameters, that is, if you add a custom query parameter, directly through get_ query_ The var() function cannot be obtained. To get the value of a custom query parameter, we need to use 'query'_ Add custom query parameters to var 'filter

Get global variables in WordPress

In addition to the global variables registered in the WordPress kernel, some themes and plug-ins may also register some global variables. If you need to view all the global variables registered in the system, you can view them through the following code.

echo "

";
print_ r($GLOBALS);
Echo "< /wan>"; / / please change wan to pre when using
Database global variable $wpdb
The wpdb function is used in the theme or plug-in file of WordPress to directly declare the global variable. However, to call the core database operation function of WordPress in the root directory or non WordPress environment, it is necessary to call the WP blog in the root directory of WordPress- header.php Profile:

//Set the call directory according to the specific needs

require_once(‘./wp-blog- header.php ’);

//Declare the global variable $wpdb

global $wpdb;

//Wpdb insert data

$wpdb->insert( $wpdb->prefix . 'fanly', array( 'name' => 'Fanly', 'url' => ' nicebrian.com ' ) );

//Wpdb delete data

$wpdb->query( "DELETE FROM " . $wpdb->prefix . "fanly WHERE 'url' = ' nicebrian.com '" );

//Wpdb modify data

$wpdb->update( $wpdb->prefix . 'fanly', array( 'name' => 'Fanly', 'url' => ' nicebrian.com ' ), array( 'name' => 'nicebrian' ) );

//Wpdb get data

$data = $wpdb->get_ row( "SELECT * FROM ".$wpdb->prefix."fanly WHERE 'url'=' nicebrian.com '" );

echo $data->name;

WordPress main global variables

Global variables that can be used in loops

In the WordPress loop, global variables have been obtained in advance, and we can use them directly. These global variables contain information about the current article in the loop.

$post (WP_ Post) / / current article object

$authordata (WP_ User) / / current article author object

$currentday (string) / / release date of current article

$currentmonth (string) / / release month of current article

$page (int) / / the page of the current article to be accessed is defined by the query parameter page

$paged / / the current pagination value, home page, blog page, archive page and page to calculate pagination. The first page is 0

$pages (array) / / pagination information of the current article. Each pagination element contains -- NextPage -- label separated parts

$multipage (Boolean) / / whether the current article is a multi page article is detected according to the above $pages parameter

$more (Boolean) WordPress / / do you want to execute the -- more tag? If it is true, WordPress will not execute the more tag

$numpages (int) / / returns the total number of pages of the current article, which is related to $pages x above

Detect Boolean value of browser

The following global variable stores the detection information about the user's browser. The value is Boolean, which can be used to access the browser of the website.

  1. $is_iphone (boolean) //iPhone Safari
  2. $is_chrome (boolean) //Google Chrome
  3. $is_safari (boolean) //Safari
  4. $is_NS4 (boolean) //Netscape 4
  5. $is_opera (boolean) //Opera
  6. $is_macIE (boolean) //Mac Internet Explorer
  7. $is_winIE (boolean) //Windows Internet Explorer
  8. $is_gecko (boolean) //FireFox
  9. $is_lynx (boolean)
  10. $is_IE (boolean) //Internet Explorer
  11. $is_edge (boolean) //Microsoft Edge

Detect Boolean value of web server

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

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

The following variables store some version information in WordPress.

$wp_version (string) / / current installed WordPress version

$wp_db_version (int) / / the version of the current database

$tinymce_version (string) / / TinyMCE version

$manifest_version (string) / / cache the version of manifest

$required_php_ Version (string) / / the minimum PHP version required for the WordPress version installed on the website

$required_mysql_ Version (string) / / minimum MySQL version required for WordPress installed on the website

Other global variables

$super_admins (array) / / user ID with super administrator rights. This global variable is only registered with the site owner

$wp_query (object) //Class_ Reference/WP_ Query class instance

$wp_rewrite (object) //Class_ Reference/WP_ Rewrite class instance

$wp (object) //Class_ Reference / WP class instance

$wpdb (object) //Class_ Reference / wpdb class instance

$wp_ Locale (object) / / localization information

$wp_admin_ bar (WP_ Admin_ / / manage toolbar objects

$wp_roles (WP_ Roles) / / WordPress role object

$wp_meta_ Boxes (array) / / registered metaboxes objects, including their ID, parameters, callback function, title, etc

$wp_registered_ Sidebars (array) / / registered widget area

$wp_registered_ Widgets (array) / / registered gadgets

$wp_registered_ widget_ Controls (array) / / registered widget field

$wp_registered_ widget_ Updates (array) / / registered widget updates

Background global variables

$pagenow (string) / / used in WP admin, also refer to get_ current_ Screen() to learn about WordPress admin screen API

$post_ Type (string) / / used in WP admin, the article type of the current page

$allowedposttags (array) / / allowed article Tags

$allowedtags (array) / / allowed Tags

Background menu data of $menu (array) / / WordPress

Original article, author: brian, if reprinted, please indicate the source:https://www.nicebrian.com/29.html

Leave a Reply

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