wp_check_browser_version()WP 3.2.0

Check if the user needs a browser update

No Hooks.

Return

Array|false. Array of browser data on success, false on failure.

Usage

wp_check_browser_version();

Examples

0

#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 );
});
0

#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.

wp_check_browser_version() code WP 6.7.2

function wp_check_browser_version() {
	if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
		return false;
	}

	$key = md5( $_SERVER['HTTP_USER_AGENT'] );

	$response = get_site_transient( 'browser_' . $key );

	if ( false === $response ) {
		$url     = 'http://api.wordpress.org/core/browse-happy/1.1/';
		$options = array(
			'body'       => array( 'useragent' => $_SERVER['HTTP_USER_AGENT'] ),
			'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
		);

		if ( wp_http_supports( array( 'ssl' ) ) ) {
			$url = set_url_scheme( $url, 'https' );
		}

		$response = wp_remote_post( $url, $options );

		if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
			return false;
		}

		/**
		 * Response should be an array with:
		 *  'platform' - string - A user-friendly platform name, if it can be determined
		 *  'name' - string - A user-friendly browser name
		 *  'version' - string - The version of the browser the user is using
		 *  'current_version' - string - The most recent version of the browser
		 *  'upgrade' - boolean - Whether the browser needs an upgrade
		 *  'insecure' - boolean - Whether the browser is deemed insecure
		 *  'update_url' - string - The url to visit to upgrade
		 *  'img_src' - string - An image representing the browser
		 *  'img_src_ssl' - string - An image (over SSL) representing the browser
		 */
		$response = json_decode( wp_remote_retrieve_body( $response ), true );

		if ( ! is_array( $response ) ) {
			return false;
		}

		set_site_transient( 'browser_' . $key, $response, WEEK_IN_SECONDS );
	}

	return $response;
}