Post Pic

How to: Automatically notify your members on new posts

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

Sep 28 2011 15:10

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…

Nov 15 2011 15:21

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!

Nov 18 2011 21:46

Great post!

How do I do this if I only want to send emails when a new post in certain page ->: news

Thanks

Jan 23 2012 05:57

Awesome post! Is there a way to send the notification via BCC (blind copy)?

Jan 29 2012 22:57

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

Mar 16 2012 16:53

Did anyone figure out how to send the notification via BCC?

Apr 09 2012 05:08

Nice tips. thx! :-D

Apr 18 2012 19:36

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.

Sep 22 2012 15:08

Hi,
I am brand new to wordpress, and I have no clue where the themes folder resides..

thanks
gary

Dec 19 2012 19:43

How to send an automatic e-mails to the users/members when i publish an new post?. I am really confused kindly help me.

Jan 14 2013 19:51

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?

Feb 05 2013 14:09

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!

Feb 10 2013 20:12

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’;

Mar 07 2013 20:00

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?

Mar 08 2013 17:22

I’m also getting some errors, this is not working bro!.

Mar 17 2013 01:28

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 ;)

Mar 22 2013 14:30

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”);

Mar 25 2013 17:34

@Deneys

table_name is we name of Users database, correct?
Teo

Apr 16 2013 10:46

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:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox