0

I am trying to create a child theme and want to deregister the original javascript and enqueue my own. Its the original javascript of the theme but a bit different :)

The problem that I have is that in the parent theme a lot of variables are 'pushed' to JS using : wp_localize_script.

The variables that are loaded in parents functions.php are not available in my child themes javascript. And I am guessing that it is because of the load order. WordPress first loads the child functions.php then the parents. And thus, first load the javascript and then the variables. Plus when I enqueue my own javascript the script has a different name.

parent functions.php 

$translation_array = array ( .... ) ;

wp_enqueue_script('jscript_custom', get_template_directory_uri() . '/js/jscript.custom.js', array('jquery'), null, true);

wp_localize_script('jscript_custom', 'obj_jscript', $translation_array);


child theme functions.php

..
wp_dequeue_script( 'jscript_custom' );
..

wp_enqueue_script('jscript_custom_child', get_stylesheet_directory_uri() . '/js/jscript.custom.js', array('jquery'), null, true);

so the variables from the parent theme are pushed to a javascript that is no longer available. And the child theme's script cannot have the same name as the parents theme.

Any ideas on how to solve this? Thanks, J

=== edit ===

**parent theme functions.php**

function parent_enqueue_scripts() {

    wp_enqueue_script('jscript_custom', get_template_directory_uri() . '/js/jscript_custom', array('jquery'), null, true);

    $translation_array = array(

        ... huge list of variables ...

        'tags_html' => $tags_html,
        'u' => $current_user->ID,
        'ui' => $current_user->display_name,
        'ul' => $current_user->user_nicename,
        'user_rewrite' => $wp_rewrite->author_base
    );

    wp_localize_script('jscript_custom', 'obj_jscript', $translation_array);
}
add_action('wp_enqueue_scripts', 'parent_enqueue_scripts');




        ****child theme functions.php****

        function remove_parent_scripts() {
            wp_dequeue_script( 'jscript_custom' );
            }

        add_action('wp_print_scripts','remove_parent_scripts');


        function enqueue_parent_theme_style_and_js() {
            wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
            wp_enqueue_script('jscript_custom_child', get_stylesheet_directory_uri() . '/js/jscript.custom.js', array('jquery'), null, true);

            }


    add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style_and_js' );

    .
  • you can manipulate the order that your enqueue function runs by changing the add_action priority. have you tested what happens if you dequeue the original and enqueue your own with the same handle? – Milo Dec 19 '14 at 18:39
  • I played with priority, but no success. The javascript won't load if I use exactly the same handle – user1973842 Dec 19 '14 at 19:06
  • I've just tested enqueueing, localizing, dequeueing, and re-enqueueing with the same handle and it worked. Add all of the relevant code from both the parent and child themes so we can see what it looks like. – Milo Dec 19 '14 at 19:49
  • Hi Milo, I have edited my original post and posted almost complete functions in there. – user1973842 Dec 19 '14 at 21:06
0

In your child theme, set the priority on the wp_enqueue_scripts hook to run later than the parent theme, and do your dequeue/re-enqueue there. Right now, your child theme code dequeues the original script on a later hook than your enqueue code.

function enqueue_parent_theme_style_and_js() {
    wp_dequeue_script( 'jscript_custom' );
    wp_enqueue_script('jscript_custom', get_stylesheet_directory_uri() . '/js/jscript.custom.js', array('jquery'), null, true);
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style_and_js', 30 );
  • Muchos gracias :) – user1973842 Dec 19 '14 at 22:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.