
Do you ever wished to be able to automatically send an email to your registered users and notify them of a new post on your WordPress blog? If yes, just read this recipe and lear how to do it easily!

Do you ever wished to be able to automatically send an email to your registered users and notify them of a new post on your WordPress blog? If yes, just read this recipe and lear how to do it easily!
Just paste the following code on your functions.php file:
function email_members($post_ID) {
global $wpdb;
$usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;");
$users = implode(",", $usersarray);
mail($users, "New WordPress recipe online!", 'A new recipe have been published on http://www.wprecipes.com');
return $post_ID;
}
add_action('publish_post', 'email_members');
Once saved, all registered users will receive an email when a new post is published.
This recipe is inspired from an example found in WordPress Codex.
19 Responses
Hi,
There are multiple functions.php in a wordpress installation. Which functions.php should we place this code….
If you can please let me know the exact location, then it would be great…
Vicky!,
You need to put this code in your “functions.php” file located at your theme folder.
/wp-content/themes/[your theme folder]/functions.php
Cheerz!
Great post!
How do I do this if I only want to send emails when a new post in certain page ->: news
Thanks
Awesome post! Is there a way to send the notification via BCC (blind copy)?
Hi,
I placed the code into functions.php (themes/mytheme) but when i publish a post, it doesn’t send any email to users.
Can you help me?
Thank you in advance
Did anyone figure out how to send the notification via BCC?
Nice tips. thx!
WordPress keeps bombing when the “add_action( )” call is left hanging outside of a function. Where does that call go? Doesn’t it have to be a routine that is called to publish an article? Not working for me.
Hi,
I am brand new to wordpress, and I have no clue where the themes folder resides..
thanks
gary
How to send an automatic e-mails to the users/members when i publish an new post?. I am really confused kindly help me.
I added the mail function in my functions.php file:
I got this error message:
Catchable fatal error: Object of class stdClass could not be converted to string in /home/users/m/mdevat/www/wpmdv/wp-content/themes/mdvtheme/functions.php on line 5
Any idea why?
I get the same error as Michel de Vathaire (above):
Catchable fatal error: Object of class stdClass could not be converted to string in…
That appears every time I try to create a new post. Saving as draft works, NOT publishing.
Thanks for your help!
Since this doesnt recognise code, the method in the previous post has a great link from the 12th line before the end. This due to the variable $post_email_message containing a link itself, please place there any message you would like to say in your message.
$post_email_message .= ‘Any message here thanks’;
I’m getting the same error as others, “Catchable fatal error: Object of class stdClass could not be converted to string”. Is there any known workaround for this?
I’m also getting some errors, this is not working bro!.
Hey, thanks for the idea.
To get it to work i needed to tweak a couple of things though… This may help those who are getting errors ^_^
Instead of $wpdb->get_results use instead $wpdb->get_col
For me get_results was returning an array of associative arrays which is not what you want in the simple case. From the documentation:
“Generic, mulitple row results can be pulled from the database with get_results. [Result] can be an object, an associative array, or a numbered array.”
http://codex.wordpress.org/Class_Reference/wpdb
For this example to work it should be called as:
$wpdb->get_results($query, ARRAY_N);
But really it should be called as $wpdb->get_col because you are only fetching a single column from the DB and get_col returns a numerically indexed array unless otherwise specified. Hope this helps! ^_^
Note to the author:
It pays to check your code before you post it
Worked but had to change this
$usersarray = $wpdb->get_results(“SELECT user_email FROM $wpdb->users;”);
to
$usersarray = $wpdb->get_col(“SELECT user_email FROM table_name”);
@Deneys
table_name is we name of Users database, correct?
Teo
After i’ve published my post (and send it by mail) i was redirect in the browser to mysite.com/wp-admin/post.php -> Sorry! this link doesn’t work
thanks
E
Trackbacks: