Plugin and theme script load order

For scritps and using the wp_register_script() function, you can define the dependeny of jQuery in the $deps argument, which is documented as: $deps: Array of the handles of all the registered scripts that this script depends on, that is, the scripts that must be loaded before this script. These scripts will automatically be enqueued when … Read more

Loading scripts with wp_register_script and wp_enqueue_script

There are probably various ways to do this – personally I use the following for loading some small scripts only on the “new” and “edit” pages (datepicker etc.): function load_my_admin_scripts( $hook ) { if (( $hook == ‘post.php’ || $hook == ‘post-new.php’ )) { wp_enqueue_script( ‘my-datepicker’, // name / handle of the script SCRIPTS . ‘script.js’, … Read more

How can I load a javascript file that is type=”module” the WordPress way?

Here is the final code I now use to add type=”module” to my scripts. wp_register_script(‘my-script’, get_theme_file_uri(‘/js/script.js’), [‘axios’], ‘1.0’); wp_enqueue_script(‘my-script’); add_filter(“script_loader_tag”, “add_module_to_my_script”, 10, 3); function add_module_to_my_script($tag, $handle, $src) { if (“my-script” === $handle) { $tag = ‘<script type=”module” src=”‘ . esc_url($src) . ‘”></script>’; } return $tag; }

getting a js file for one page

You’re registering/enqueueing your script wrong. You should register/enqueue in your theme’s functions.php file instead of inside the header/page. Also, you need to use your theme’s directory … which will be along the lines of mydomain.com/wp-content/themes/BLANK-Theme/js/SliderViewer-1.2.js. Use this code in functions.php: function my_scripts_enqueue_method() { wp_enqueue_script( ‘SliderViewer’, get_template_directory_uri() . ‘/js/SliderViewer-1.2.js’ ); } add_action( ‘wp_enqueue_scripts’, ‘my_scripts_enqueue_method’ ); This … Read more

Including style.css in Child Theme

get_stylesheet_uri will return the current theme’s stylesheet– the child stylesheet if it is a child theme. While not entirely clear from the Codex entry for that function, it is clear from the entry for get_stylesheet_directory_uri, which is used by get_stylesheet_uri. What should be happening is that the child stylesheet is being enqueued twice under different … Read more