0

I'm trying to override some custom js files in my child theme, and I have managed to get almost all scripts working, except for one script - the 'child-listing' script. All scripts load from child theme, however I can also see parent theme scripts loading as well - is this correct behaviour? If I use wp_deregister_script to remove all traces of the parent scripts, the child scripts don't work for some reason.

Here is my code:

add_action( 'wp_enqueue_scripts', 'my_remove_scripts', 10 );

function my_remove_scripts(){
    wp_dequeue_script( 'google-map' );
    wp_dequeue_script( 'parent-maps');
    wp_dequeue_script( 'parent-listing');
    wp_dequeue_script( 'parent-custom');
}

add_action('wp_enqueue_scripts', 'my_add_scripts', 15 );

function my_add_scripts(){

    wp_enqueue_script('child-maps', get_stylesheet_directory_uri() . '/js/maps.js', array('jquery'), THEME_VERSION, true);
    wp_enqueue_script('child-listing', get_stylesheet_directory_uri() . '/js/listing.js', array('jquery', 'plupload', 'jquery-ui-sortable'), THEME_VERSION, true);
    wp_enqueue_script('child-custom', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), THEME_VERSION, true);

    $map_api_key = theme_option('map_api_key');
    if (is_ssl()) {
    wp_enqueue_script('google-map-child', 'https://maps-api-ssl.google.com/maps/api/js?libraries=places'. '&key='.esc_html($map_api_key).'', array('jquery'), '1.0', false);
  } else {
      wp_enqueue_script('google-map-child', 'http://maps.googleapis.com/maps/api/js?libraries=places' . '&key='.esc_html($map_api_key).'', array('jquery'), '1.0', false);
  }
}

My assumption was that if you dequeue parent script then enqueue your own child script, all reference to the parent scripts would be removed...? What would be the reason why my child theme js scripts are not working? The dependencies all seem to be default ones that are loaded with wordpress.

I'm quite new to all this, so any help would greatly be appreciated! :)

  • wp_dequeue_script() won't do anything if the parent theme enqueues them later, such as inside a template. You'll need to deregister them. Regarding the child theme scripts not working, there just isn't enough information here to say why that would be. – Jacob Peattie Aug 6 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.