Better use two dedicated methods hooked with add_action() to different actions, or can it be twice the same method?


This is merely a question about what’s better in terms on performance, and general (WordPress) PHP Development.

I deregister and dequeue a fewe scripts (and styles) from a WordPress website, in a Custom PHP Function, which I add_action like so add_action( 'wp_print_scripts', 'my_custom_method', PHP_INT_MAX );

In my my_custom_method I gather some plugin options, global post, and more data, then I run wp_dequeue_script and wp_deregister_script iterating over each script I set to be removed (plugin options), same for styles.

This works fine for all scripts and styles I wanted to remove so far.
However, there is one script added to get_footer (by a third party plugin), so when hooking into wp_print_scripts this one is not removed.
It is removed when I hook my method (obviously) into get_footer, but in this case, styles aren’t removed.

So I just ended up hooking my method to both wp_print_scripts and get_footer, which works.

My question is, is it OK to just add_action() the same method twice (once to wp_print_scripts and once to get_footer), or would it be more performant if I reduce the method hooked to get_footer to just wp_deregister_script the one script added in get_footer?

, Beda Schmid 1 year 2020-07-13T01:10:24-05:00 0 Answers 56 views 0

Leave an answer