Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I downloaded various themes on net and I found on functions.php they write something like this

Example to remove inline comment style.

function demo_remove_recent_comments_style() {
    add_filter( 'show_recent_comments_widget_style', '__return_false' );
}
add_action( 'widgets_init', 'demo_remove_recent_comments_style' );

Question

Why we not write only this without a function()

add_filter( 'show_recent_comments_widget_style', '__return_false' );

Let me know

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

It is not always required technically but it is good practice to follow for multiple reasons:

  1. Code is more organized by keeping hook-related stuff together
  2. The more precise conditions when it runs - the better for performance
  3. It is easier to unhook function, that performs multiple add_, then unhook each add_ individually.
  4. The order of adding functions matters when not specifying priority.
  5. The child theme (if used) is processed before parent theme and it can lead to confusing stuff with hooks if add_ and remove_ are performed inline.

Of course in some specific examples (yours does fit) it does seem overly verbose to write out all of that just to manipulate single item on single hook.

Myself I use couple of my own (and somewhat messy functions) - add_filter_return() and add_action_with_arguments() to lower amount of wrapping when working with hooks.

share|improve this answer
    
Nice answer and make me more clear. Thanks Rast –  haha Jan 28 '11 at 6:38
add comment

Without more context, it's difficult to know exactly why, but generally, this might offer a marginal improvement to site performance.

As written, the add_action function limits how often the add_filter function is fired. It will only execute when the widgets_init hook is encountered before the show_recent_comments_widget_style hook. On the same note, the show_recent_comments_widget_style hook can be used in the code without executing the add_filter function.

Without the function(), the code would fire each time the show_recent_comments_widget_style hook was encountered, whether or not the widgets_init function came first.

share|improve this answer
    
It's will hurt our speed performance? –  haha Jan 28 '11 at 6:20
    
Regardless of which way it is coded, I would imagine that you will not notice an impact on performance. However, throughout your theme, the stricter the conditions, the better your site will perform. –  joelhaus Jan 28 '11 at 7:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.