Skip to content

Instantly share code, notes, and snippets.

@adczk
Created December 1, 2023 10:36
Show Gist options
  • Select an option

  • Save adczk/30736ec67478ee287d3a7ab11bd870eb to your computer and use it in GitHub Desktop.

Select an option

Save adczk/30736ec67478ee287d3a7ab11bd870eb to your computer and use it in GitHub Desktop.

Revisions

  1. adczk created this gist Dec 1, 2023.
    39 changes: 39 additions & 0 deletions block-user-registration-by-mail-domain.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?php

    /**
    * Plugin Name: Block WordPress user account registration by e-mail domain
    * Plugin URI: https://gist.github.com/adczk
    * Description: Block WordPress user account registration by e-mail domain
    * Author: adczk
    *
    * Author URI: https://gist.github.com/adczk
    * License: GPLv2 or later
    *
    * Use as MU plugin; config in code comments
    *
    */

    function wpmu_stop_registration_by_domain( $errors, $sanitized_user_login, $user_email ) {

    // below you list email domains to be blocked
    // comma-separated
    $blocked_domains = array(
    'emaildomain.com',
    'otherdomain.net',
    );

    // custom error message
    $error_msg = "You're not allowed to register!";

    foreach ( $blocked_domains as $key=>$value ) {

    if ( strpos( $user_email, $value ) !== false ) {
    $errors->add( 'banned_domain_error', $error_msg );
    }

    }

    return $errors;
    }

    add_filter( 'registration_errors', 'wpmu_stop_registration_by_domain', 10, 3 );