What are the advantages of using wp_enqueue_script()

Aside from dependency management, what advantage does wp_enqueue_script() have over adding scripts manually? I currently enqueue 5 scripts and am thinking of minimising HTTP requests by using an inline <script> tag instead. For the purposes of this discussion let’s forget HTTP/2. Ref: https://developer.wordpress.org/reference/functions/wp_enqueue_script/ 2 Answers 2 How about API? Let’s Check this example. $id = … Read more

wp_enqueue_scripts hook is not being called

I added the following code to my functions.php: if(!function_exists(‘bi_frontend_scripts’)) { function bi_frontend_scripts() { wp_enqueue_script(‘jquery’); // I originally wanted to do: wp_enqueue_script(‘jQuery.bxSlider’, get_bloginfo(‘template_url’).’/scripts/jquery.bxslider/jquery.bxslider.min.js’, array(‘jquery’)); } } add_action(‘wp_enqueue_scripts’, ‘bi_frontend_scripts’); But apparently neither of the scripts is being enqueued. I didn’t get what my problem was, so I added this script to find out if the hook is … Read more

is_page() function doesnt working

I’m trying to add these custom CSS and JS (written inside a custom plugin) only add to a specific page. this is my code <?php function randomAlphaNumeric($length) { $pool = array_merge(range(0,9), range(‘a’, ‘z’),range(‘A’, ‘Z’)); $key=”; for($i=0; $i < $length; $i++) { $key .= $pool[mt_rand(0, count($pool) – 1)]; } return $key; } add_action(‘init’, ‘register_script’); function register_script(){ … Read more

wp_enqueue_script order – external vs inline js

I have a few scripts loading with my theme: // loading script.js <script type=”text/javascript” src=”https://wordpress.stackexchange.com/questions/27397/script.js”></script> // doing something using script.js <script type=”text/javascript”> script-var: <?php echo get_option(‘script1-var’);?> </script> They work well, but when I do wp_enqueue_script instead of <script src=””> the script is loading AFTER in-line js content, so: // enqueuing script.js wp_enqueue_script(‘script-js’, get_template_directory_uri() .”/scripts/script.js”); // … Read more

How to load script-related styles automatically?

Scenario: It is common that a JS dependency is bundled with a style file to work properly (just think about your favorite slideshow jQuery plugin). AFAIK, in this situation, the script and style have to be included in separate wp_enqueue_script and wp_enqueue_style calls. I am thinking about how to automate this process. A solution I … Read more

Loading scripts to the Post Edit page only

I have added a few meta boxes for the posts and pages, so I want to load a js script only on the Create/Edit post and page. How can I do that? Currently I’m using following but that loads the scripts in all pages in the admin. function my_meta_boxes() { add_meta_box(“post_meta”, “Meta”, “post_meta”, “post”, “normal”, … Read more