0
votes

context Trying to learn plugins creation in Wordpress, i want to register/enqueue some scripts.

if i do this the proper way :

function my_script_enqueuer() {

 wp_register_script( "my-script", plugins_url("plugin/js/my-script.js"), array('jquery'), 0.2, true );
 wp_enqueue_script( 'my-script' );
}
add_action( 'wp_enqueue_scripts', 'my_script_enqueuer' );

It does not work at all.

If i remove the function :

wp_register_script( "my-script", plugins_url("plugin/js/my-script.js"), array('jquery'), 0.2, true );
wp_enqueue_script( 'my-script' );

It works fine. Why ? How can i make the proper way working ?

Some infos

  • Plugin is of course activated in Wordpress
  • wp_head() and wp_footer() are at their respective places
  • Function is in an include php file.
1
We'll need to see more more code to understand why WordPress isn't enqueuing your plugin's script. Please update your question and include a minimal version of the plugin (see How to create a Minimal, Reproducible Example) so others can reproduce the issue and help you debug it. - cabrerahector

1 Answers

0
votes

You might try the following. Notice I consolidated the enqueue and the register functions. The enqueue function will register the script for you, so you don't need both. That would be an unlikely reason for this not working, but you really only need to use register function if you're doing so globally and enqueuing in different functions.

function my_script_enqueuer() {

  wp_enqueue_script( 'my-script', plugins_url('js/my-script.js', __FILE__), array('jquery'), 0.2, true );

}

add_action( 'wp_enqueue_scripts', 'my_script_enqueuer' );