admin管理员组文章数量:1330564
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
?
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
Reset to default 0So 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.
本文标签:
版权声明:本文标题:performance - Better use two dedicated methods hooked with add_action() to different actions, or can it be twice the same method 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742267481a2443682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论