0

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.
Lbh
  • 67
  • 1
  • 7
  • 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](https://stackoverflow.com/help/minimal-reproducible-example)) so others can reproduce the issue and help you debug it. – cabrerahector Nov 13 '20 at 13:20

1 Answers1

0

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' );
DubVader
  • 1,032
  • 1
  • 6
  • 8
  • Tried that too, sadly without any effect. What I think is my code is a mess, and i misunderstand some wordpress logics. More of that, the plugin i'm trying to make is ajax oriented. Thank you for helping anyway – Lbh Nov 13 '20 at 16:39