Support » Theme: Twenty Twenty » Child theme index.js

  • Hi all,

    I am using a child theme, I am trying to stop the Parent assets/js/index.js working and get the one to work in my child theme, same structure assets/js/index.js.

    I have done a bit of digging, but nothing works for me, just have loads of conflicts. I am using this snippet in functions.php

    /* Stop Parent Theme index.js and use Child Theme index.js */
    
    // enqueue your required script file
    add_action('wp_enqueue_scripts', 'your_child_theme_js_file_override');
    function your_child_theme_js_file_override(){
        wp_enqueue_script( 'child_theme_script_handle', get_stylesheet_directory_uri() . '/assets/js/index.js', array('jquery' ) );
    }
    
    // dequeue your required script file
    function your_child_theme_js_file_dequeue() {
       wp_dequeue_script( 'parent_theme_script_handle' );
    }
    add_action( 'wp_print_scripts', 'your_child_theme_js_file_dequeue', 100 );

    With the above implemented you can see in the google code inspect that it is still reading the twenty twenty index.js

Viewing 4 replies - 1 through 4 (of 4 total)
  • could you try this?
    You should actually use the parent themes script handle. At least I am not aware of any constant 'parent_them_script_handle'.

    Could you try the following code instead? Cannot try for myself atm, sorry.

    /* Stop Parent Theme index.js and use Child Theme index.js */
    
    // enqueue your required script file
    add_action('wp_enqueue_scripts', 'your_child_theme_js_file_override');
    function your_child_theme_js_file_override(){
        wp_enqueue_script( 'child_theme_script_handle', get_template_directory_uri() . '/assets/js/index.js', array('jquery' ) );
    }
    
    // dequeue your required script file
    function your_child_theme_js_file_dequeue() {
       wp_dequeue_script( 'twentytwenty-js' );
    }
    add_action( 'wp_print_scripts', 'your_child_theme_js_file_dequeue', 100 );
    

    Hi RaQai

    Thank you for the quick reply, that has stopped the conflicts!! Perfect! It does however show both index.js in the source code.

    Thian

    • This reply was modified 2 months, 4 weeks ago by ThianBrodie.

    works completely fine on a fresh installation of wordpress using a blank child theme for twenty twenty.

    Maybe check your plugins and caching settings.

    raQai

    (@raqai)

    My bad, I didn’t check the actually used sources.

    Use get_stylesheet_directory_uri instead of get_template_directory_uri.

    see get_template_directory_uri pointing to parent theme not child theme

    • This reply was modified 4 weeks ago by raQai.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.