WordPress Query to Send New Post Notification to All Users

You can use many plugins to send notification about new published post. But using many plugins may slowed down your site performance and site vulnerability may also increase due to low quality plugin security. So it is understood that if you can complete the task by adding a simple WordPress action or filter function then you should go for it. You can send notification to all of your registered users by adding a simple functions. Here is that WordPress query to send new post notification to all users:

send-new-post-notification-wordpress-featured

[html]</p>
<p>function newPostNotification($post_ID) {<br />
if( wp_is_post_revision($post_ID) ) return;<br />
global $wpdb;<br />
$get_post_info = get_post($post_ID);<br />
if ( $get_post_info-&gt;post_status == ‘publish’ &amp;&amp; $_POST[‘original_post_status’] != ‘publish’ ) {<br />
// Collect all of the emails from the database<br />
$wp_user_email = $wpdb-&gt;get_results(&quot;SELECT DISTINCT user_email FROM $wpdb-&gt;users&quot;);<br />
// Send emails to each of registered users<br />
foreach ( $wp_user_email as $email ) {<br />
// Set Your Email subject<br />
$subject = ‘&lt;span style=&quot;color: #fafafa;&quot;&gt;&lt;strong&gt;New Post has been published on MyTutorialguru.com&lt;/strong&gt;&lt;/span&gt; ‘;<br />
$message = ‘View the post now:’ . get_permalink($post_ID);<br />
// Finally Send email<br />
wp_mail($email-&gt;user_email, $subject, $message);<br />
}<br />
}<br />
}<br />
add_action(‘publish_post’, ‘newPostNotification’);</p>
<p>[/html]

Add the above function to your funtions.php file and from now on all of your registered user will get notification if you publish a post.

 

Author: mytutorialguru

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *