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.

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
add comment

2 Answers

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 . '>' . 
                PHP_EOL;
            // send mail
            wp_mail( 
                $to,
                $subject, 
                $message,
                $headers
            );
        }

        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
add comment

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
1  
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
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.