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

I have put together a custom registration form on my WordPress site and one of the items on the registration form is an ability to select an interest.

Essentially I have the following code from the registration form...

 update_usermeta( $user_id, 'interests', $_POST['interests'] );

I would need a code that does something like, if "interestes" matches "tag" of a new post, then send an email to the user.

Anything like that possible? Thank you in advance!

share|improve this question
1) How many tags are you dealing with? 2) Does the user have access to /wp-admin/profile.php? 3) How is the user entering the interests, as a comma separated value? 4) If 3 is true, this brings a bigger problem that is searching for partial matches. 5) Depending on 1 and 2, a list of real tags could be presented in the Profile page for the user to select. – brasofilo Dec 18 '12 at 22:18
up vote 2 down vote accepted

See this example on the question 19259.

The function was init on publish a post, change the hook to your post type or use on default post - Hook publish_post. Before you send check in the function your custom user meta field, like get_user_meta( $user_id, 'interests', TRUE ); and ready to send the information about new post.

Maybe liek this solution, but untested!

    add_action( 'publish_post', 'fb_send_mail_about_new_post' );
    function fb_send_mail_about_new_post( $post_id = FALSE ) {

        // check user meta data, if interest
        $interests = FALSE;
        $interests = get_user_meta( get_current_user_id(), 'interests', TRUE );
        if ( ! $interests )
            return NULL;

        if ( $post_id ) {
            // get data from current post
            $post_data = get_post( $post_id );
            //var_dump($post_data);exit; <-- see this for all content or use the id for get the revisons

            // get mail from author
            $user_mail = get_userdata( $post_data -> post_author );

            // email addresses
            $to = get_user_meta( get_current_user_id(), 'email', TRUE );
            // email subject
            $subject = get_option( 'blogname' ) . ': ' . $post_data -> post_title;
            // message content
            $message = $post_data->post_content . ' ' . PHP_EOL . 
                get_author_name( $post_data->post_author ) . ' ' . PHP_EOL . 
                get_permalink( $post_id );
            // create header data
            $headers = 'From: ' . 
                get_author_name( $post_data -> post_author ) . 
                ' (' . get_option( 'blogname' ) . ')' . 
                ' <' . $user_mail -> user_email . '>' . 
            // send mail

        return $post_id;

A other alternative way is to use a plugin and leave many time, like this free plugin Inform about Content. The plugin add two fields to the user profile for update of post and comment via mail. You can enhance with a custom plugin to the register site for new users to fill up this fields.

share|improve this answer

Why reinvent the wheel? You should be able to hook into wp_update_user and then add the users email/remove it via a plugin like subscribe2 based on the value of that field.

If you want a more custom solution you could create your own plugin and do something like:

    class emailer {
  function send($post_ID)  {
    $friends = 'bob@example.org,susie@example.org';
    mail($friends,"sally's blog updated",'I just put something on my blog: http://blog.example.com');
    return $post_ID;
$myEmailClass = new emailer();
add_action('publish_post', array($myEmailClass, 'send'));

Which is copy and pasted from the Plugin API

This would have to be modified with your if statement and a for-each but other than that should work. I would recommend just going with the first option though.

share|improve this answer
I briefly looked into "subscribe2" however, I wanted a notification based on user preferences. In other words, if they selected a particular tag that they're interested, then whenever a post with that tag appears, they will be notified. Not sure if "subscribe2" has anything like that. – Richard Dec 18 '12 at 21:34
@Richard, BandonRandon's code is part of the solution. The value of $friends has to be built based on the post tags and a search for matches inside all users meta. – brasofilo Dec 18 '12 at 22:20
use function wp_mail() as wp function, not mail(). It is easier to use for different installs and server-settings. – bueltge Dec 19 '12 at 12:21

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.