Overriding parent theme javascript from child theme Overriding parent theme javascript from child theme php php

Overriding parent theme javascript from child theme


By simply registering the script and using the same handle that is being used in the parent theme, it takes precedence. Thus when the parent theme gets around to enqueueing its own version, the handle is already allocated, and it enqueues the override instead.

function custom_script_fix(){    // use same handle as parent theme to override ('jquery-custom')    wp_register_script('jquery-custom', get_stylesheet_directory_uri() .'/includes/js/custom.js', false, '1.0');}add_action( 'wp_enqueue_scripts', 'custom_script_fix' );

The description on wp_enqueue_script() specifies it won't replace the registered version. You may need to set a lower $priority on add_action() to ensure your override script is registered first.