简体   繁体   中英

Enqueue Javascript in plugin not registering on pagesource

I'm hoping this is the right place to ask for advice. Please do forgive my ignorance, i am new to this. I am trying to load my javascript files via a plugin, as I could not get it working within the functions.php. I have created the php file, and have the following in it:

add_action( 'wp_enqueue_scripts', 'register_scripts', 
'adding_scripts');

function register_scripts() {

    wp_register_script('app', plugins_url( 'app.js', __FILE__), '', 
1.0, true );
    wp_register_script('angular', plugins_url( 'angular.js', 
__FILE__), '', '1.6.4', true );
    wp_register_script('angular-animate', plugins_url( 'angular-
animate.js', __FILE__), 'angular', '1.6.6', true );
    wp_register_script('jquery.mobile', plugins_url( 
'jquery.mobile-1.4.5.min.js', __FILE__), 'jquery', '1.4.5', true ); 
}

function adding_scripts() {
    wp_enqueue_script('jquery');
    wp_enqueue_script('app');
    wp_enqueue_script('angular');
    wp_enqueue_script('angular-animate');
    wp_enqueue_script('jquery.mobile'); 
}

At present, when loading the page source, I'm only finding the jquery script, and the css files loaded in the functions file. I have the .js files named appropriately in the plugins folder directory, and i can activate it within wordpress admin panel with no errors (There is a header on the file).

What my endgame is to utilise the angular-animate, and the jquery.mobile for aspects that they offer for animating parts of the page.

Please let me know if there is anything else i can provide.

1) I think you are very confused with WordPress functions and hooks. Please refer WordPress documentation for understanding hooks :- https://codex.wordpress.org/Plugin_API/Hooks

2) You are not using add_action function properly. Please refer function documentation :- https://developer.wordpress.org/reference/functions/add_action/

3) You don't need to create separate functions for registering and enqueuing scripts. You can do them all together.

Saying all this, please refer below code :-

add_action( 'wp_enqueue_scripts', 'add_my_scripts');

function add_my_scripts(){

  wp_register_script('app', plugin_dir_url(). '/js-path/app.js', array('jquery') );
  wp_enqueue_script('app');
}

Let me know if anything is unclear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM