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.

This snippet for example will email on EVERY post:

function email_friends( $post_ID )  
{
   $friends = 'bob@example.org, susie@example.org';
   wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );

   return $post_ID;
}
add_action('publish_post', 'email_friends');

... whereas I just need add_action to run ONLY if the post is in category say "uncategorized", not any other category.

Thanks!

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Use has_category() to check if a post belongs to a certain category.

function email_friends( $post_ID )  
{
    if ( has_category( 'uncategorized', $post_ID )
    {
        $friends = 'bob@example.org, susie@example.org';
        wp_mail( 
            $friends, 
            "sally's blog updated", 
            'I just put something on my blog: ' . get_permalink( $post_ID )
        );
    }
}
add_action('publish_post', 'email_friends');

And you don't need to return anything for an action handler.

share|improve this answer
    
It worked great, thanks! :) –  Nimbuz May 29 '12 at 3:39
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.