javascript - Loading scripts from custom plugin in Wordpress -


i have in main plugin file (/plugins/oglasi/oglasi.php)

<?php  /*  plugin name: oglasi  plugin uri:   description: custom post type "oglasi".  version: 1.0  author: dragi  author uri:  license: gplv2  */  function james_adds_to_the_head() {     wp_register_script( 'add-bx-js', plugin_dir_url( __file__ ) . 'js/filter-oglasi.js', array('jquery'),'',true  );  }  add_action( 'wp_enqueue_scripts', 'james_adds_to_the_head' ); 

and here /plugins/oglasi/js/filter-oglasi.js have:

jquery(document).ready(function() {      console.log("main loaded");     alert("dragi"); }); 

but not working. can help?

wp_register_script() registers script enqueued later using wp_enqueue_script() function. use wp_enqueue_script('add-bx-js') load registered script. means, if want register scripts, not directly load them in pages, can register files once, , load them when need them.

use following code :

function james_adds_to_the_head() {     wp_register_script( 'add-bx-js', plugin_dir_url( __file__ ) . 'js/filter-oglasi.js', array('jquery'),'',true  );      wp_enqueue_script( 'add-bx-js' ); }  add_action( 'wp_enqueue_scripts', 'james_adds_to_the_head' ); 

or :

function james_adds_to_the_head() {     wp_enqueue_script( 'add-bx-js', plugin_dir_url( __file__ ) . 'js/filter-oglasi.js', array('jquery'),'',true  ); }  add_action( 'wp_enqueue_scripts', 'james_adds_to_the_head' ); 

more info :

https://wordpress.stackexchange.com/questions/82490/when-should-i-use-wp-register-script-with-wp-enqueue-script-vs-just-wp-enque

https://developer.wordpress.org/reference/functions/wp_register_script/

update

you have error in js file. use jquery instead of jquery follow , use ctrl+f5 force refresh browser cache. can use wp_register_script() fourth parameter - version parameter - force refresh assets.

jquery(document).ready(function() {      console.log("main loaded");     alert("dragi"); }); 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -