Multi Admin Emails in WordPress

Tech Articles | October 4, 2024 | Blog, Coding, Hosting, Wordpress

Sometimes in WordPress, you need to be able to add more than the allowed admin email address.

For me, it’s because different emails do different things, but it could equally be for you and your client to get notifications of admin events. I achieve this using a little snippet loaded within my Utility Plugin.

It allows me to add a comma-separated list in the WordPress admin area. You can see what it looks like below:

Additional admin emails input field in WordPress admin area for entering multiple email addresses.

You can also do the same using the snippet below, which automatically appends these additional emails to the To field.

// 1. Add a custom field to the General Settings page
function add_additional_admin_email_field() {
    add_settings_field(
        'additional_admin_emails', // Field ID
        'Additional Admin Emails', // Field title
        'render_additional_admin_email_field', // Callback to render the field
        'general', // Settings page (General Settings)
        'default', // Section
        []
    );

    register_setting('general', 'additional_admin_emails', [
        'type' => 'string',
        'sanitize_callback' => 'sanitize_textarea_field',
        'default' => '',
    ]);
}
add_action('admin_init', 'add_additional_admin_email_field');

// 2. Render the text area in the General Settings page
function render_additional_admin_email_field() {
    $emails = get_option('additional_admin_emails', '');
    echo '<textarea name="additional_admin_emails" id="additional_admin_emails" class="large-text" rows="3" placeholder="Enter emails separated by commas">'
        . esc_textarea($emails)
        . '</textarea>';
    echo '<p class="description">Add additional admin email addresses here, separated by commas.</p>';
}

// 3. Add additional admin emails to outgoing emails
function modify_admin_emails($args) {
    $additional_emails = get_option('additional_admin_emails', '');

    // If there are additional emails, append them to the 'to' field
    if (!empty($additional_emails)) {
        $additional_emails = array_map('trim', explode(',', $additional_emails)); // Split and trim emails
        $args['to'] .= ',' . implode(',', $additional_emails);
    }

    return $args;
}
add_filter('wp_mail', 'modify_admin_emails');
PHP

Support the Author

buy me a coffee
Really Useful Plugin Logo
Wpvideobank temp
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best