admin管理员组

文章数量:1311533

// only run on dashboard page
if ( 'index.php' != $hook ) {
    return;
}

There is a plugin on the Wordpress 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>");
});
// only run on dashboard page
if ( 'index.php' != $hook ) {
    return;
}

There is a plugin on the Wordpress 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>");
});
Share Improve this question edited Jan 13, 2021 at 8:25 intense2020 asked Jan 13, 2021 at 8:14 intense2020intense2020 233 bronze badges 3
  • Can you please include the full function that you took these lines from, this will give better context and make the question easy to answer. – Q Studio Commented Jan 13, 2021 at 8:22
  • Ofcourse. Just a minute. – intense2020 Commented Jan 13, 2021 at 8:24
  • Added an answer below. – Q Studio Commented Jan 13, 2021 at 9:04
Add a comment  | 

1 Answer 1

Reset to default 3

Ok - so, firstly you should know that it is a variable and not a function - in php this is indicated by the dollar symbol before the name: $variable as opposed to function()

Second, you should note that $hook is passed as a parameter to the function block - like so:

function_name( $hook ){

    // this makes the variable $hook available inside the function - not globally.
    echo $hook;

}

So, $hook is actually passed to the function from the "hook" admin_enqueue_scripts which you can read on the codex page describes the value as $hook as:

Parameters

$hook_suffix
(string) The current admin page.

So, the answer is that $hook is not a global variable, nor a function ( essentially all WordPress functions are global, but that is another point ) - it is a local variable passed into the scope of the function hooked to the action.

本文标签: phpIs hook a global variable in WordPress