0

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?

1 Answer 1

0

So there's 4 factors:

  • Performance (Speed, resource usage)
  • Convenience
  • Code maintainability
  • Learning the API you're using

If you don't know the performance of your function then it's impossible to answer your question, but I would guess that if it doesn't do or cause any database queries, and it's not doing operations on lots of data, then it's probably fast.

It seems like what you're doing is convenient - you have the code in one place and call it from a couple of places it's needed.

Even though it's called from two places, as the code is in one place that's quite maintainable. If you want to change what it does in future you don't have two places and need to figure out where to put changes, you just change this one function and you know the effect will happen.

You could do this a different way but it sounds like you're saying that this would involve a lot more time to learn the interface you're using (i.e. WP script queueing orders) and this might not have a benefit for you.

So, on balance if it's a fast function there's no reason not to do this and it's not bad design due to maintainability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.