admin管理员组文章数量:1406761
When looking through WordPress snippets/tutorials/plugins I often see add_action()
and add_filter()
being placed before the function is declared:
add_action( 'publish_post', 'email_friends' );
function email_friends( $post_ID ) {
$friends = '[email protected], [email protected]';
mail( $friends, "sally's blog updated" , 'I just put something on my blog: ' );
return $post_ID;
}
From a logic standpoint this just doesn't make sense to me. Why would you place the function after it is called in your code? This is usually how I would handle the same situation:
function email_friends( $post_ID ) {
$friends = '[email protected], [email protected]';
mail( $friends, "sally's blog updated" , 'I just put something on my blog: ' );
return $post_ID;
}
add_action( 'publish_post', 'email_friends' );
I know both scenarios work, but is there a specific advantage to one or the other? About 90% of the time I see the first scenario being used, so that leads me to believe there is a benefit to this in some way.
When looking through WordPress snippets/tutorials/plugins I often see add_action()
and add_filter()
being placed before the function is declared:
add_action( 'publish_post', 'email_friends' );
function email_friends( $post_ID ) {
$friends = '[email protected], [email protected]';
mail( $friends, "sally's blog updated" , 'I just put something on my blog: http://blog.example' );
return $post_ID;
}
From a logic standpoint this just doesn't make sense to me. Why would you place the function after it is called in your code? This is usually how I would handle the same situation:
function email_friends( $post_ID ) {
$friends = '[email protected], [email protected]';
mail( $friends, "sally's blog updated" , 'I just put something on my blog: http://blog.example' );
return $post_ID;
}
add_action( 'publish_post', 'email_friends' );
I know both scenarios work, but is there a specific advantage to one or the other? About 90% of the time I see the first scenario being used, so that leads me to believe there is a benefit to this in some way.
Share Improve this question edited May 4, 2015 at 10:37 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Sep 26, 2012 at 17:20 voodooGQvoodooGQ 4291 gold badge4 silver badges11 bronze badges4 Answers
Reset to default 17It is easier to read: When is what called? If you are debugging a hook you can immediately see if you have to read the function or not: If it is not your hook, you can skip the code.
In my themes and plugins I combine all registrations for actions, filters and shortcodes at the top and I add the hook to the PHPDoc block:
add_action( 'wp_head', 'foo' );
add_action( 'shutdown', 'bar' );
/**
* Foo you!
*
* @wp-hook wp_head
* @return void
*/
function foo()
{
print '<!-- foo -->';
}
There is no real difference actually, I for example prefer to follow first scenario, because it's neater to place calls in one place, and define functions below that. PHP parses the whole document prior to running anything, and if functions are properly defined, everything will work normally, no advantage in either scenario.
I believe the right saying here is: Whatever floats your boat :)
4 years later, but I'm sure it'll help people getting here from search.
As others have stated, there isn't a difference as php parses the entire document and executes in the correct order. So whatever you like.
I personally like the first style:
add_action(hook, bar);
function bar(){
//code here
}
I tend to think backwards. Goal oriented if you will. So I like to to read, "we're doing function bar, in hook. Okay cool, now, what does the function do?"
It sets up the context for the function better. Of course this is just my personal preference. So do what you like.
I feel like there should be some specific guidance to this in the WordPress coding standards, but it seems like there is not.
I would argue against the "whatever is fine" answers to say either:
A) consistently follow the examples available in the WP Codex and do function
followed by add_action
like this:
function wporg_custom()
{
// do something
}
add_action('init', 'wporg_custom');
or
B) if you pick a different style, pick one and do it consistently in all of the themes and plugins you build - not just "whatever floats your boat" at the moment.
本文标签: filtersaddaction()addfilter() before or after function
版权声明:本文标题:filters - add_action(), add_filter() before or after function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744800068a2625809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论