I have a child theme and I am able to add the script I want to use to replace some theme functions and replace the some of the buttons.
But I cannot remove the old button so they are both showing up on top of each other. How can I remove the parent js script?
here is my function.php
for the child theme
function replace_scroll(){
// Remove the Default JavaScript
wp_dequeue_script( 'dp-js' );
// Add your own script
$js_url = get_bloginfo('stylesheet_directory') . '/js';
wp_enqueue_script('dp',"$js_url/dp1.scripts.js");
}
add_action( 'wp_enqueue_scripts', 'replace_scroll' );
In a typical wordpress flow, plugins are loaded before the theme, and in case of child theme, child theme is loaded before the parent theme, specifically,
functions.php
file of a child theme is included before the functions.php of a parent. So,wp_enqueue_scripts
actions are stacked, and later execution in the that order, unless the priority of actions is defined diferently that default value ( 10 ). The problem here is that you are trying to dequeue script that has not yet been enqueued.To remove the script added by parent, raise the priority of
wp_enqueue_scripts
action of child theme to value higher than value defined in parent theme action.To replace the script, for example, you want to make some changes and leave the existing dependencies and localization there are two ways.
Use
wp_enqueue_script
with the samehandle
name, due the fact that if you try to register or enqueue an already registered handle, the new parameters will be ignored, useful if the script is later localized by parent to keep the localized data.Use global
$wp_scripts
object to modify only required properties, for example, change onlysrc
property of already enqueued or registered script.The fact that child theme is loaded first is quite practical since the use of child theme is not to replace, but more as addition to the main theme.
A few issues here to begin with
You should be using
get_stylesheet_directory_uri()
for child themes andget_template_directory_uri()
for parent themes instead of theget_bloginfo()
functions. Latter is slower and uses the first two functionsScripts 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
Testing with the parent theme Twenty Fourteen that has this in its
functions.php
file:The action
wp_enqueue_scripts
does not have a declared priority, so it's using the default10
. To make the dequeue work we have to add a lower priority in our child themefunctions.php
file:I want to add that we need to use
get_stylesheet_directory_uri()
when adding the replacement script becauseget_template_directory_uri()
returns the parent theme directory.See the WordPress docs 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
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.
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.