How to write a plugin to add users to a mail list

I run a multi-site blog with about 300 sites.

I have an idea that I want to write a plugin that in order to add all administrators of blogs to a MailMan mail-list. I just want administrators though, I don’t want to add editors and subscribers to this email list.

Read More

I want to capture the event of being assigned an administrator of a blog. Then I’ll send an email to the subscribe email address of my mail list to automatically add the person.

So I was wondering which hook should I use ? I think I want to send an email every time a person is set as an administrator. I don’t care if they are removed as an administrator.

Related posts

Leave a Reply

1 comment

  1. You would hook into profile_update and user_register. First check to see if it’s a new user and whether she is an admin/editor.

    Then send the mail. Same story with updating the user: see if the role has changed, and whether the new role is admin or editor.

    <?php
    add_action( 'profile_update', 'wpse33949_profile_update' );
    add_action( 'user_register', 'wpse33949_profile_update' );
    function wpse33949_profile_update( $user_id, $old_user_data = false )
    {
        // get the updated user data
        $userdata = get_userdata( $user_id );
    
        // whatever you need to send here
        $message = "Sign up for my email!";
        $subject = "Mailing List";
    
        if( ! $old_user_data && in_array( $userdata->user_role, array( 'editor', 'administrator' ) ) )
        {
            // we're inserting a new user...
            wp_mail( $userdata->user_email, $subject, $message );
        }
        elseif( in_array( $userdata->user_role, array( 'editor', 'administrator' ) ) && $userdata->user_role != $old_user_data->user_role )
        {
            // We're updating the role of an existing user, make sure they're 
            // becoming an admin or editor, then send the message.
            wp_mail( $userdata->user_email, $subject, $message );
        }
    }
    

    The above has not been tested. Copy and paste with caution. Just meant to get you started.