1

I have two versions of the simplest function that hooked to the wp_head action.

One is working, andother one is not.

Can someone explain?

// Works !
function custom_description() {
     ?>
         <meta name="description" content="cococo" />
     <?php  
}

// Does not work
/*
function custom_description() {   
    return  '<meta name="description" content="cococo" />';
}
*/

add_action( 'wp_head', 'custom_description' );

2 Answers 2

1

I found answer i do not need to return value, i must echo it. So my function should look like:

function custom_description(){   
    echo  '<meta name="description" content="cococo" />';
 }

This works !

1

You can easily recognize where you're supposed to return a value, and where you're supposed to do an action (like echo or some other action).

Although, internally they are very similar, but:

  1. Any hook that's called a filter hook, and used with add_filter function, expects a return.

  2. On the other hand, any hook that's called an action hook, and used with add_action function, doesn't expect a return and instead expects an action (like echo or something else).


Now, in your code, since wp_head is an action hook:

add_action( 'wp_head', 'custom_description' );

you're supposed to do an action like echo, instead of return.


You'll get more info. from the following documents:

  1. https://developer.wordpress.org/plugins/hooks/#actions-vs-filters

  2. https://developer.wordpress.org/plugins/hooks/actions/

  3. https://developer.wordpress.org/plugins/hooks/filters/

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.