Using WordPress ‘newblogname’ PHP filter

The ‘newblogname’ filter allows you to modify the new site name during the WordPress multisite registration process.

This name will be used as either the subdomain or the subdirectory path for the new site, based on the network settings.

Usage

$new_blog_name = apply_filters( 'newblogname', $blogname );

Parameters

  • $blogname (string) – The new site name to be filtered.

Examples

Append a custom string to new site names

add_filter( 'newblogname', 'my_custom_newblogname' );

function my_custom_newblogname( $blogname ) {
    return $blogname . '-mysite';
}

In this example, we’re appending -mysite to all new site names. If the original name is example, it will be changed to example-mysite.

Force all site names to lowercase

add_filter( 'newblogname', 'my_lowercase_newblogname' );

function my_lowercase_newblogname( $blogname ) {
    return strtolower( $blogname );
}

Here, we ensure that all new site names are lowercase. If the original name is ExampleSite, it will be changed to examplesite.

Replace spaces with dashes in site names

add_filter( 'newblogname', 'my_replace_spaces_newblogname' );

function my_replace_spaces_newblogname( $blogname ) {
    return str_replace( ' ', '-', $blogname );
}

In this scenario, we replace spaces in new site names with dashes. If the original name is example site, it will be changed to example-site.

Add a prefix to new site names

add_filter( 'newblogname', 'my_prefix_newblogname' );

function my_prefix_newblogname( $blogname ) {
    return 'my-prefix-' . $blogname;
}

This example adds a prefix my-prefix- to all new site names. If the original name is example, it will be changed to my-prefix-example.

Remove numbers from new site names

add_filter( 'newblogname', 'my_remove_numbers_newblogname' );

function my_remove_numbers_newblogname( $blogname ) {
    return preg_replace( '/\d+/', '', $blogname );
}

In this case, we remove any numbers from new site names. If the original name is example123, it will be changed to example.

Change site name to uppercase during registration

function uppercase_new_blog_name( $blogname ) {
    return strtoupper( $blogname );
}
add_filter( 'newblogname', 'uppercase_new_blog_name' );

This example uses the strtoupper() function to change the site name to uppercase during registration.

Replace spaces in site name with hyphens during registration

function replace_spaces_with_hyphens( $blogname ) {
    return str_replace( ' ', '-', $blogname );
}
add_filter( 'newblogname', 'replace_spaces_with_hyphens' );

This example uses the str_replace() function to replace spaces in the site name with hyphens during registration.

Limit the maximum length of the site name during registration

function limit_new_blog_name_length( $blogname ) {
    $max_length = 20;
    if ( strlen( $blogname ) > $max_length ) {
        $blogname = substr( $blogname, 0, $max_length );
    }
    return $blogname;
}
add_filter( 'newblogname', 'limit_new_blog_name_length' );

In this example, the substr() function is used to limit the maximum length of the site name to 20 characters during registration.

Remove all special characters from the site name during registration

function remove_special_characters_from_new_blog_name( $blogname ) {
    $blogname = preg_replace( '/[^A-Za-z0-9\-]/', '', $blogname );
    return $blogname;
}
add_filter( 'newblogname', 'remove_special_characters_from_new_blog_name' );

This example uses a regular expression with the preg_replace() function to remove all special characters from the site name during registration.