Add the site's name as prefix when adding new users? Multisite

I have a multisite network, where the admins of each site are able to add users to their site. My problem is that if they select the same username it will not work.

So I am thinking if it is possible to save the Username field with a prefix. Say the admin adds a user called johndoe to a site called mysite then the user should be saved as mysite-johndoe. Would this be possible to achieve?

I am talking about the Users->Add New User in the backend.

Hope there is a way to do this! Either with PHP or jQuery?

Topic multisite-user-management multisite customization Wordpress

Category Web


I have noticed that the code our friend shares has an error. Add the prefix twice.

I share other code with this error resolved.

<?php


add_filter( 'pre_user_login', 'sneakily_add_prefix_to_username' );

function sneakily_add_prefix_to_username( $username ) {
    if ( ! current_user_can( 'manage_network' ) ) {
        $blog_id = get_current_blog_id();
        $blog_details = get_blog_details( $blog_id );
        $sanitized_path = str_replace( '/', '', get_blog_details()->path );
        //error_log($sanitized_path);
        if ( $sanitized_path != '') {
            if(false === stripos($username, $sanitized_path)) {
                return $sanitized_path . '-' . $username;
            } else {
                return $username;
            }
        } else {
            $domain_parts = explode( '.', $blog_details->domain );
            if ( is_array( $domain_parts ) ) {
                $sanitized_subdomain = sanitize_user( $domain_parts[0], true );
                //error_log($sanitized_subdomain);
                if ( $sanitized_subdomain != '' ) {
                    if(false === stripos($username, $sanitized_subdomain)) {
                        return $sanitized_subdomain . '-' . $username;
                    } else {
                        return $username;
                    }
                }
            }
        }
    }
    return $username;
}

I am not sure that they would like that prefix, maybe they would prefer to come up with a new username that is not used. But here, I made it how you wanted. It takes either the path of the site or the first part of the subdomain, so it works with both types of multisite. It affects users created by users that are not super administrators.

add_filter( 'pre_user_login', 'sneakily_add_prefix_to_username' );

function sneakily_add_prefix_to_username( $username ) {
    if ( ! current_user_can( 'manage_network' ) ) {
        $blog_id = get_current_blog_id();
        $blog_details = get_blog_details( $blog_id );
        $sanitized_path = sanitize_user( $blog_details->path, true );
        if ( $sanitized_path != '' ) {
            return $sanitized_path . '-' . $username;
        } else {
            $domain_parts = explode( '.', $blog_details->domain );
            if ( is_array( $domain_parts ) ) {
                $sanitized_subdomain = sanitize_user( $domain_parts[0], true );
                if ( $sanitized_subdomain != '' ) {
                    return $sanitized_subdomain . '-' . $username;
                }
            }
        }
    }
    return $username;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.