1

The WP systems sends a lot of mails to authors. There also plugins who use email notifications for authors.

But, let say I have a user, who is managed by two persons (because it's a company, an entity that is more then one person) - I want all the emails to be sent also to a second Email address, that I will enter in the profile page of that WP user.

What is the best way to achieve this? I know how to add a field to the profile page, but I don't know how to send every notification that the user gets to the second email address. please help :)

10
  • Have you tried using comma separated values for the email? Just a thought...
    – mor7ifer
    Feb 17, 2012 at 13:00
  • tried it, just for the elimination process. it didn't worked. Feb 17, 2012 at 14:49
  • What exactly didn't work out?
    – kaiser
    Feb 19, 2012 at 14:51
  • @kaiser, try it yourself - it would output an error saying it is not a valid email address. Feb 20, 2012 at 16:18
  • Yea, sure - building a setup to test this. Did you actually var_dump the output to make shure you ain't got the comma or white spaces?
    – kaiser
    Feb 20, 2012 at 16:31

2 Answers 2

1

Not a WP specific solution, but you can usually set-up an alias/forwarder in your email system that sends a copy to both people.

e.g.

[email protected]

[email protected]

etc.

3
  • yes. but's that isn't always possible. let say, when the host is lousy and won't let me manage my emails properly... Feb 23, 2012 at 21:03
  • 1
    If you have a lousy host it's probably time to move hosts, not route around their lousiness ;-). This is certainly easier than a code based solution (for non technical users).
    – Refiner
    Feb 27, 2012 at 22:36
  • Yes, I know. but just for the challange. what would you do. in the end i've changed hosting and forward the Email as you suggested, and as I also thought would be the best solution. But what if? Mar 2, 2012 at 10:56
2

A suggestion, albeit a sorta hack, I'd like to make is to use a mailing list. You can add an infinite number of emails to a mailing list.

An alternative you could do is to use the publish_{$posttype} hook to send email notificiations through wp_mail. The wp_mail function's $to parameter takes either a string or an array so you could pass in multiple email addresses.

EDIT:

function notify_users_of_new_post($post_id) {
    // Get list of subscribers and their secondary email address stored in wp_usermeta
    $user_query = get_users('blog_id=1&orderby=nicename&role=subscriber&fields=all_with_meta');

    $bName = get_bloginfo('name');
    $permalink = get_permalink($post_id);
    foreach($user_query as $user) {
        $email = $user->email;

        if (isset($user->secondary_email) AND !empty($user->secondary_email)) {
            $email = array( $user->email, $user->secondary_email );
        }

        wp_mail($email, sprintf('New Blog Entry on %s', $bName), sprintf('A new entry has been published to %s. View it clicking here: %s', $bName, $permalink));
    }
}
add_action('publish_post', 'notify_users_of_new_post');

I attempted a sample code block (above). A suggestion if you plan on using this on a production site, use a cron job if you have a lot of registered users, otherwise I'm pretty sure this will cause a timeout in PHP.

1
  • Attempted a codeblock. Though I'm unfamiliar with these functions get_users (confused by Codex's query $args) and wp_mail by simply not having used it before.
    – Zack
    Feb 18, 2012 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.