wp_check_browser_version()
Checks if the user's browser needs to be updated.
Sends requests containing the string $_SERVER['HTTP_USER_AGENT'] to the external API service http://api.wordpress.org/core/browse-happy/1.1/ which parses the string and returns data about the user's browser and other information.
Since the string parsing occurs remotely, the execution time of the function depends on the API response time. The response may not come at all due to the lack of connection to the API or other reasons.
The result of the function execution is cached for a week (stored in transient options). Therefore, the function will only be slow on the first call for a new browser; subsequent calls will be instantaneous.
The function is located in the file wp-admin/includes/dashboard.php, which contains functions directly related to the admin page Dashboard. Therefore, before using the function, this file needs to be included so that the function is defined at the time of the call.
require_once( ABSPATH . '/wp-admin/includes/dashboard.php' );
No Hooks.
Returns
Array|false. false in case of an error during the check process. An array of browser data in case of a successful check.
Example of the returned array.
array( 'platform' => 'Windows', 'name' => 'Firefox', 'version' => '53.0', 'update_url' => 'http://www.firefox.com/', 'img_src' => 'http://s.wordpress.org/images/browsers/firefox.png', 'img_src_ssl' => 'https://wordpress.org/images/browsers/firefox.png', 'current_version' => '16', 'upgrade' => FALSE, 'insecure' => FALSE, );
- platform - the name of the visitor's OS.
- name - the name of the browser.
- version - the version of the browser.
- update_url - the link to the browser's homepage.
- img_src - the link to the browser's logo using the http protocol.
- img_src_ssl - the same but via https.
- current_version - the latest version of the browser.
- upgrade - a boolean type true or false that determines whether the current browser needs to be updated if the browser is considered outdated.
- insecure - a boolean type true or false that determines whether it is safe to use the current version of the browser.
Usage
$browser_data = wp_check_browser_version();
Examples
#1 Using in the admin panel
// Get information about the browser in the admin panel, where it is available.
add_action( 'admin_head', function (){
if ( ! function_exists( 'wp_check_browser_version' ) ) {
include_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
}
$array = wp_check_browser_version();
var_dump( $array );
});
#2 Use it on front-side of the site
// Get information about the browser in the external part of the site
// if the function is not available first include the file containing it
add_action( 'wp', function () {
if ( ! function_exists( 'wp_check_browser_version' ) ) {
require_once ABSPATH . 'wp-admin/includes/dashboard.php';
}
$array = wp_check_browser_version();
var_dump( $array );
} );
Changelog
| Since 3.2.0 | Introduced. |