WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's what I have so far..

class emailer {
function notifyHeart($post_ID)  {

    $interests = get_user_meta( get_current_user_id(), 'interests', TRUE );
    $to = the_author_meta( 'user_email', get_current_user_id() );

    if(has_tag($interests[0])) {
        $email = $to;
        mail($email, "An article about Heart", 
          'A new post has been published about heart.');
        return $post_ID;

add_action('publish_post', array('emailer', 'notifyHeart'));

What I need to do now, before actually sending the email, I need something that checks if the user has an $interest and if so, send them an email. Any help with this?

share|improve this question
removing [0] from $interests[0] will remove the php error. Though i don't really understand what you're trying to do here. – Mridul Aggarwal Dec 20 '12 at 18:50
Well the [0] I need so that the correct "interest" in the area can be matched to a tag from a post. – Richard Dec 20 '12 at 18:59
What I'm trying to do is, after a post has been published, find out its tags, if the tags match a user's "interests" which I have already gathered in the database, then email that user. – Richard Dec 20 '12 at 19:00
For future reference, [0] will actually throw an error. The TRUE in get_user_meta will return only one of the tags in the database. That will mean that $interests there is a string & not array. – Mridul Aggarwal Dec 20 '12 at 19:13
up vote 1 down vote accepted
class emailer {
  static function notifyHeart($post_ID)  {
    $interests = get_user_meta(get_current_user_id(), 'interests');
    $to = get_the_author_meta( 'user_email', get_current_user_id() );
    $post = get_post($post_ID);

    foreach($interests as $interest) {
      if(has_tag($interest, $post)) {
        $email = $to;
        mail($email, "An article about Heart", 'A new post has been published about heart.');
add_action('publish_post', array('emailer', 'notifyHeart'));

Here's a reworked example. It retrieves all the tags you have in database as meta, checks each of them & if it matches the one in that post's id, it will send the mail & break out of the loop.

share|improve this answer
Thank you, but I'm receiving the following error now.. Warning: Cannot modify header information - headers already sent.. it occurs after publishing a post. – Richard Dec 20 '12 at 19:26
that error shouldn't be related to mail function. maybe due to something else. Have you recieved the email? – Mridul Aggarwal Dec 20 '12 at 19:30
Thank you, it's working now, however, it's only sending email to one user with a match, the other user on the site, that selected a different interest ins't receiving the email. – Richard Dec 20 '12 at 21:12
that's cuz you only checked for the current(logged in) user. To check for all users , you can use get_users & then loop through all the users to send them the email – Mridul Aggarwal Dec 21 '12 at 7:34

Your Answer


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.