My Example:

function example() {
  if ($checked) echo 'true';
add_action('wp_head', 'example');

now, if not $checked, will wordpress do an empty add_action or will avoid it? if do, how I can just add action if $checked is true?

1 Answer 1


The function will run, but nothing will happen. There is a small overhead, but you shouldn't worry about it, the possible savings are minimal.

Depending on when you have $checked available, you could also do

function example() {
    echo 'true';
if ($checked) add_action('wp_head', 'example');
  • Thanks, will I need to Declare $checked 2 times? one inside function and second outside it?
    – Jlil
    Oct 8, 2017 at 21:00
  • 1
    No, just outside. If add_action is only executed if($checked), the function will never run unless $checked equals to true. Whether you do it inside or outside the function depends mostly on whether you can determine the value of $checked at template loading time where you don't know which post is requested. If you check it inside example(), get_the_ID() etc will work correctly.
    – janh
    Oct 8, 2017 at 21:33

Your Answer

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

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