admin管理员组

文章数量:1205753

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'skfans_wp_enqueue_script()' not found or invalid function name in C:\xampp\htdocs\plugin\wp-includes\class-wp-hook.php on line 306:

add_action('wp_enqueue_scripts','skfans_wp_enqueue_script()'); 
    function skfans_wp_enqueue_script() {
        wp_enqueue_style('skfans_dev_plugin', plugin_dir_url(__FILE__)."assets/css/style.css");
        wp_enqueue_script('skfans_dev_script',plugin_dir_url(__FILE__)."assets/js/custom.js",true);
    }

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'skfans_wp_enqueue_script()' not found or invalid function name in C:\xampp\htdocs\plugin\wp-includes\class-wp-hook.php on line 306:

add_action('wp_enqueue_scripts','skfans_wp_enqueue_script()'); 
    function skfans_wp_enqueue_script() {
        wp_enqueue_style('skfans_dev_plugin', plugin_dir_url(__FILE__)."assets/css/style.css");
        wp_enqueue_script('skfans_dev_script',plugin_dir_url(__FILE__)."assets/js/custom.js",true);
    }
Share Improve this question edited Mar 8, 2022 at 14:59 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 13, 2022 at 9:30 Ammar AwanAmmar Awan 1 3
  • 2 Callbacks are without () – Buttered_Toast Commented Feb 13, 2022 at 9:37
  • new to plugin development, could you please tell me where is the callbacks here? – Ammar Awan Commented Feb 13, 2022 at 9:39
  • You can check the wordpress documentation about add_action to understand how to work with them – Buttered_Toast Commented Feb 13, 2022 at 9:48
Add a comment  | 

1 Answer 1

Reset to default 2

The text in the second parameter is not code to run, it is the name of a function. That is why it does not and cannot work. The warning is PHP telling you that no function with that name exists. Or more specifically that is it an invalid function name.

A function with () in its name is not possible. For your code to work you would need a function named skfans_wp_enqueue_script() but that's impossible because it would mean your function looks like this:

function skfans_wp_enqueue_script()() {

This would generate syntax errors and crash immediately.

To clarify the difference, this is what you believed it meant:

add_action('action/event name', 'run this PHP code inside these quotes when action/event happens');

What it actually means:

add_action('action/event name', 'A thing to call when the action/event happens' );

e.g.

add_action( 'save_post', 'wpdocs_my_save_post' );
function wpdocs_my_save_post() {
    // .....

Further reading:

  • https://www.php.net/manual/en/language.types.callable.php
  • https://developer.wordpress.org/reference/functions/add_action/

本文标签: