CLONER Error - Only lowercase letters (az) and numbers are allowed

If you get this error message: 
Only lowercase letters (az) and numbers are allowed.  
when trying to clone a WordPress Multisite Network site, here's what is going on and how to work around this.

This is almost always caused when you use invalid characters in the site name field. However, sometimes you want to use dashes in your site name, and this article will explain how to allow that. We will soon incorporate this update to the Cloner itself so that this should not be an issue for those who want to use dashes in site names. 

The NS Cloner uses WordPress' built in site name validation function called wpmu_validate_blog_signup() before it allows the site to be created and cloned. This prevents malformed site names that can cause broken sites and bad data in your database. What's strange is that although the normal Network Add Site tool also says that only letters and numbers are allowed:



However, despite that warning, it does in fact allow dashes. The problem is that if we remove the wpmu_validate_blog_signup() check supplied by WordPress from the NS Cloner, it could create many problems with site names that get cloned resulting in additional frustration and support cases.

In order to allow dashes in your site names (before the feature is added to the NS Cloner) you can do the following:
  1. Copy the code below into a new php file and call it something like ns-cloner-filter-dashes.php
  2. Upload the ns-cloner-filter-dashes.php file to the /wp-content/mu-plugins directory in your WordPress Multisite installation (create that directory first if it doesn't already exist).
That's it! You're done. If you aren't familiar with the mu-plugins directory it stands for "Must Use" Plugins that WordPress always loads for every site. This will ensure that the filter loads each time you clone and allows it to bypass the wpmu_validate_blog_signup() error when there are dashes in your site names. Here's the code for  ns-cloner-filter-dashes.php to put in your mu-plugins directory:
<?php

/**
* Filter to allow hyphens on blognames to bypass wpmu_validate_blog_signup()
*
* From: https://wordpress.org/support/topic/how-to-allow-in-site-urls
*
* @author Jon Breitenbucher
* @param array $result
* @return array $result
*
* Original Author: Jon Breitenbucher
* Adapted by Never Settle (http://neversettle.it) for use with the NS Cloner
*
*/
function ns_allow_blognames_with_hyphens( $result ) {
  $olderrors = $result[ 'errors' ];

  // If Site Name ('blogname') is long enough,
  // and we have no error object, just return.
  if ( ! is_object( $olderrors ) ) {
    return $result;
  }

  // Build a new WP_Error Object to hold new errors.
  $errors = new WP_Error();

  // Look for a 'blogname' $code error in this loop.
  foreach ( $olderrors->errors as $code => $error ) {
  if ( $code == 'blogname' ) {
    // Sort the 'blogname' error $value with this loop.
    foreach ( $error as $key => $value ) {
        // Switch each action based on the $error $value
        // and our slected options.
        switch ( $value ) {
            case __( 'Only lowercase letters (a-z) and numbers are allowed.' ):
                $ok_chars = '-';

                $pattern = '/^[a-z0-9]+([' . $ok_chars . ']?[a-z0-9]+)*$/';
                preg_match( $pattern, $result[ 'blogname' ], $match );

                    if ( $result[ 'blogname' ] != $match[ 0 ] ) {
                        // Build a new error to add to the $errors object
                        // Allow Lowercase Letters
                        $ok_chars = __( 'Only the following Characters are allowed: lowercase letters (a-z), numbers (0-9) and hyphen (-).' );

                            // Add the new error to the $errors object
                            $errors->add( 'blogname', $ok_chars );
                    }

                break; case __( 'Site names can only contain lowercase letters (a-z) and numbers.' ): $ok_chars = '-';

                $pattern = '/^[a-z0-9]+([' . $ok_chars . ']?[a-z0-9]+)*$/';
                preg_match( $pattern, $result[ 'blogname' ], $match );

                    if ( $result[ 'blogname' ] != $match[ 0 ] ) {
                        // Build a new error to add to the $errors object
                        // Allow Lowercase Letters
                        $ok_chars = __( 'Only the following Characters are allowed: lowercase letters (a-z), numbers (0-9) and hyphen (-).' );

                            // Add the new error to the $errors object
                            $errors->add( 'blogname', $ok_chars );
                    } break;
            case "That name is not allowed.":
                // Do Nothing, just break
                break;

            case "Site name must be at least 4 characters.":
                // Do Nothing, just break
                break;

            case "Sorry, site names may not contain the character “_”!":
                // Do Nothing, just break
                break;

            case "Sorry, you may not use that site name.":
                // Do Nothing, just break
                break;

            case "Sorry, site names must have letters too!":
                // Do Nothing, just break
                break;

            case "Sorry, that site is reserved!":
                // Do Nothing, just break
                break;
        default:
          $errors->add( 'blogname', $value );
      } // end switch ($value)
    } // end foreach ($error as $key => $value)
  } else {
    // Add other errors to $error object from the nested arrays.
        foreach ( $error as $key => $value ) {
            $errors->add( $code, $value );
        }
    } // end if ($code == 'blogname')
  } // end foreach ($olderrors->errors as $code => $error)

  // Unset old errors object in $result
  unset( $result[ 'errors' ] );
  // Set new errors object in $result
  $result[ 'errors' ] = $errors;

  return $result;
} // end function ns_allow_blognames_with_hyphens

add_filter('wpmu_validate_blog_signup', 'ns_allow_blognames_with_hyphens');

?>


Feedback and Knowledge Base