• I have an issue where I’m trying override a function in an active plugin. I don’t want to hack the plugin, so I’m trying to override the plugin function in the theme functions.php file.

    The plugin function puts out a few lines of javascript and is being run off the ‘wp_enqueue_scripts’ hook.

    Now, I’d like to override this javascript in my theme functions.php file. I was under the impression that the ‘init’ hook and ‘plugins_loaded’ hooks should be used to fire off a theme function when you want to be sure the plugins have already loaded.

    However, this doesn’t seem to be the case. If I use either ‘init’ or ‘plugins_loaded’ as a trigger to run my function in functions.php, the override doesn’t work.

    BUT, the override DOES work, when I use the ‘wp_enqueue_scripts’ hook in functions.php instead of init and plugins_loaded.

    I’m confused about the order of precedence here and why ‘init’ and ‘plugins_loaded’ won’t work in my theme to override the plugin function.

    Thanks for any help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s a matter of which hooks fire in what order. The callback that runs last has the final say. plugins_loaded fires quite early. init fires once WP is loaded. wp_enqueue_scripts fires very late, so hooking earlier means your callback doesn’t have the last say.

    When you add your callback to wp_enqueue_scripts, use a large priority argument number to help ensure your callback runs last.

    https://codex.www.ads-software.com/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request

    That will show you the basic order of how the actions are fired. As you’ve sene, the wp_enqueue_scripts action does run after init and ‘plugins_loaded, so you have use that action and set your priority set to a higher number than the plugin uses so you can make changes after the plugin does it’s thing.

    Thread Starter the_enn

    (@the_enn)

    Ok, guys, thanks for explaining – much appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Override plugin via functions.php question’ is closed to new replies.