Notify users whenever a new post is published based on their preference

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.

Leave a Comment