I created my own javascript file and now I want to integrate into my module. I'm trying to do as told in this site https://developer.wordpress.org/reference/functions/wp_enqueue_script/ But it does not work, I don't understand why
P.S: I am in the main file of my module (exampe.php)
class exemple
{
public function __construct()
{
register_activation_hook(__FILE__, array('exemple_bd', 'install'));
register_uninstall_hook(__FILE__, array('exemple_bd', 'uninstall'));
include_once plugin_dir_path( __FILE__ ).'settings.php';
new Settings();
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_init', array($this, 'load_JS'));
}
public function add_admin_menu()
{
add_menu_page('monTitre', 'Monplugin', 'manage_options','xxx', array($this, 'menu_html'),'dashicons-email');
add_submenu_page('xxx', 'NouveauTitre', 'NouveauTitre', 'manage_options', 'xxx', array($this, 'menu_html'));
}
public function load_JS()
{
wp_register_script('myfunction',plugins_url( '/js/function.js', __FILE__ ));
wp_enqueue_script('myfunction');
}
You need to register them with action hook "wp_enqueue_scripts". Use "admin_enqueue_scripts" for admin scripts.
The function should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end of the site, like in the examples above...
https://developer.wordpress.org/reference/functions/wp_enqueue_script/#notes
if all the paths are good, an you have a custom theme, make sure your main theme file references wp_head()
it should be just before the closing 'head' tag like this:
<?php ...
wp_head();
?></head>