// only run on dashboard page
if ( 'index.php' != $hook ) {
return;
}
There is a plugin on the WordPress.org repository that checks PHP current version. I happened to read its code and came across the above code.
the comment tells everything that only runs on the Dashboard page.
But is $hook a global WP function? I am unable to make a sense of this. Please help me or in case if any suggested readings.
Display-php-version.php →
function dpv_enqueue_script( $hook ) {
// only run on dashboard page
if ( 'index.php' != $hook ) {
return;
}
// enqueue script to show PHP version
wp_enqueue_script( 'dpv_script', plugin_dir_url( __FILE__ ) . 'dpv.js' );
// pass the PHP version to JavaScript
wp_localize_script( 'dpv_script', 'dpvObj', array(
'phpVersion' => phpversion()
) );
}
add_action( 'admin_enqueue_scripts', 'dpv_enqueue_script' );
Code on dpv.js →
jQuery(document).ready(function ($) {
$("#wp-version-message").after("<p>Running PHP version: <b style="color:green;">" + dpvObj.phpVersion + "</b></p>");
});