wp_dequeue_script for child theme to replace script wp_dequeue_script for child theme to replace script wordpress wordpress

wp_dequeue_script for child theme to replace script


A few issues here to begin with

  • You should be using get_stylesheet_directory_uri() for child themes and get_template_directory_uri() for parent themes instead of the get_bloginfo() functions. Latter is slower and uses the first two functions

  • Scripts and styles should be deregistered AND and dequeued to remove them completely from queue

  • Priority is important. You need to hook your function later to make sure that the styles and scripts are registered before you deregister them, otherwise it won't work.

Solution:

Copy the parent js file to your child theme and open it up and make the necessary changed. Save the file

Now you need to dequeue AND deregister the parent js file and then enqueue you new child theme js file

Example

/* * Use any number above 10 for priority as the default is 10  * any number after 10 will load after */add_action( 'wp_enqueue_scripts', 'my_custom_scripts', 100 );function my_custom_scripts(){    wp_dequeue_script( 'parent-script-handle' );    wp_deregister_script( 'parent-script-handle' );    // Now the parent script is completely removed    /*     * Now enqueue you child js file, no need to register if you are not      * doing conditional loading     */    wp_enqueue_script( 'child-script-handle', get_stylesheet_directory_uri() . '/child-script.js' );    //Now we have done it correctly}


Testing with the parent theme Twenty Fourteen that has this in its functions.php file:

function twentyfourteen_scripts() {    // other enqueues    wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131209', true );}add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );

The action wp_enqueue_scripts does not have a declared priority, so it's using the default 10. To make the dequeue work we have to add a lower priority in our child theme functions.php file:

function remove_twentyfourteen_scripts(){    wp_dequeue_script( 'twentyfourteen-script' );}add_action( 'wp_enqueue_scripts', 'remove_twentyfourteen_scripts', 11 ); // <-- HERE


You need to figure out where the parent theme is generating the buttons.

If they are being generated by javascript, you can dequeue the script that is creating the buttons. Just be careful, as any other javascript in that file will be removed as well. If this is an issue, the best way to go about it is to copy the js script to your theme folder, remove the part you don't want, dequeue the original, and enqueue your altered script.

How to do this.

To dequeue a script you need to know it's handle. Here's a fantastic tutorial on how to get the handles of scripts, very handy. To sum up in case the link goes dead:

Added to your functions.php file

function wpcustom_inspect_scripts_and_styles() {    global $wp_scripts;    global $wp_styles;    // Runs through the queue scripts    foreach( $wp_scripts->queue as $handle ) :        $scripts_list .= $handle . ' | ';    endforeach;    // Runs through the queue styles    foreach( $wp_styles->queue as $handle ) :        $styles_list .= $handle . ' | ';    endforeach;    printf('Scripts: %1$s  Styles: %2$s',     $scripts_list,     $styles_list);}add_action( 'wp_print_scripts', 'wpcustom_inspect_scripts_and_styles' );

This will print a list of all the js and css being loaded to your page at the top. If your'e having trouble working out what's what, you can always use a dom inspector, such as on Chrome or firefox. On chrome- right click - inspect element - Resources- Frames- Your site url - scripts.

This will give you a list of all the scripts and their locations.

Once you've figured out which script you need, copy it to your theme and remove the part that's adding the buttons.

Then dequeue the unwanted script using the handle we found earlier, and enqueue your new one.

function wpcustom_deregister_scripts_and_styles(){    wp_deregister_script('my-script-handle');    wp_enqueue_script( 'my-new-script', get_template_directory_uri() . '/js/my-new-script.js', array(), '1.0', true );}add_action( 'wp_print_styles', 'wpcustom_deregister_scripts_and_styles', 100 );

If the buttons are being generated by php, you'll need to find the file that's generating the html in your parent theme, copy it to your child theme, and removing the offending lines.