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've just looked into wordpress code and found this definition of add_action:

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

Now why are we wasting one CPU cycle for just calling another function that we can simply do ourselves and then we won't have to remember two different function names for the same thing.

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Actions do things. Filters modify things. You do stuff in an action, whereas if you apply a filter, you do not expect any events or actions to occur, other than the modification of the value you're filtering. e.g. It's not okay to send an email or save a DB value in a filter, but it is okay in an action. There may be rare moments when you need to violate this convention but it is a useful and necessary one.

By replacing all add_action and do_action calls with their filter counterparts, you remove the distinction and force the knowledge of wether names like 'the_content' etc are actions or filters out of the world, and into your memory, encouraging mistakes.

So instead of making your code harder to read, realise that if your code is slow, it isn't because you didn't shave an instruction or two here and there, it's either your slow server, or your algorithmic structure of your code and the nature of the data being handled. You would get far more from refactoring your processes, upgrading hardware, or changing your paradigms.

To put it into perspective, when WordPress runs, PHP executes millions of instructions. Swapping actions for filters would barely make a dent into the top 1 thousandth of a percentile.

http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster

I have tried on a fresh Wordpress installation. The script halts before it ends with a "Bus Error" on my laptop, but the number of opcodes was already at more than 2.3 millions. Enough said.

If you really want to try, look at how the guy used the VLD and test out your theory for yourself.

share|improve this answer
    
So it means there is not actually any difference except the one that is analogous to calling a person with two different names.... just that one we call him at home and one we call him at office :) –  Gagan Apr 12 '12 at 9:55
    
The wrapping of filters by the action functions probably incurs a penalty of 4 or 5 opcodes, which is trivial on the scale of things, any benefits would easily be wiped out by using a ternary operator ( ? : ) on a large array ( see blogpost on the blog I linked to, his blog's well worth a browse through ). TLDR There is a difference, but it's so tiny it's meaningless –  Tom J Nowell Apr 12 '12 at 10:12
    
Thanks buddy for clearing this out, I never considered the fact that many people work on the same code and it becomes a bigger problem if someone just keeps writing do_action() for filters instead of actions. So I'll use add_filters in cases when I'll be sure I'm the only one going through the code till its used, otherwise I'll write add_action() :) –  Gagan Apr 12 '12 at 10:20
    
Also will test the method you mentioned in the post at weekend, and will post the results. –  Gagan Apr 12 '12 at 10:22
    
I recommend you stick with convention, using apply_filters all the time would be a micro-optimisation. You gain a performance penalty, but not from PHP opcodes, it's a mental performance penalty, which is worse. It's also misleading if you make a custom action/filter and revisit later, is it an action or a filter? You've no way of knowing –  Tom J Nowell Apr 12 '12 at 10:34

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.