1

I want to overide the function which enqueues scripts and styles in the theme Twenty Twelve using its child functions.php.

I followed this guide but can't get it to work.

Can someone suggest a possible workaround?

For quick reference the function is here

I don't want to use wp_deregister_style/script as I want to keep my code clean.

  • 1
    I cannot see how using wp_deregister_style() would DIRTY your code. Please explain – Pieter Goosen Jan 26 '16 at 13:37
  • I didn't mean dirty exactly. Just the number of lines due dependency management. Also, once I remove the parent function and write my own function, it is clearer when I come back to it later. – Ramanan Jan 26 '16 at 20:18
0

If you want to remove the entire twentytwelve_scripts_styles function, you have to remove the action hook :

remove_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' );

However, this code have to execute after the function is being hooked by Twenty Twelve code, and before the action is fired (because after it's too late). The child theme functions.php file is loaded before the Twenty Twelve one, so you have to call remove_action from an action that fires before wp_enqueue_scripts, for example init.

add_action( 'init', 'answer_215713_override_twentytwelve_script' );
function answer_215713_override_twentytwelve_script(){
    remove_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' );
}

The tutorial that you linked says :

[...] you need to attach this function to a hook which will fire after the hook which the parent theme function is attached to. This is because you can't remove the action before it's been fired.

I think this is wrong. The opposite is true : you have to attach your function to a hook that will fires before the hook it was attached by the parent theme, or else it will be to late to cancel the action. In fact, you can't remove actions after they have been fired (it's useless).

  • Thanks. That part was so ingrained that I never realized it! Your code works. – Ramanan Jan 26 '16 at 19:59

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.