Using WordPress ‘get_subdirectory_reserved_names()’ PHP function

The get_subdirectory_reserved_names() WordPress PHP function retrieves a list of reserved site names on a sub-directory Multisite installation.

Usage

get_subdirectory_reserved_names();

Parameters

  • None

More information

See WordPress Developer Resources: get_subdirectory_reserved_names

Examples

Display the list of reserved site names

This example displays the list of reserved site names in a comma-separated format.

$reserved_names = get_subdirectory_reserved_names();
echo implode(', ', $reserved_names);

Check if a site name is reserved

This example checks if a given site name is reserved and displays a message accordingly.

$site_name = 'example';
$reserved_names = get_subdirectory_reserved_names();

if (in_array($site_name, $reserved_names)) {
    echo 'The site name "' . $site_name . '" is reserved.';
} else {
    echo 'The site name "' . $site_name . '" is not reserved.';
}

Allow ‘/blog’ in a Multisite network

This example modifies the list of reserved names to allow the use of ‘/blog’ in a Multisite network.

function mod_subdirectory_reserved_names($names) {
    $position = array_search('blog', $names);
    if ($position) {
        unset($names[$position]);
    }
    return $names;
}
add_filter('subdirectory_reserved_names', 'mod_subdirectory_reserved_names');

Add a custom reserved site name

This example adds a custom reserved site name to the list of reserved names.

function add_custom_reserved_name($names) {
    $names[] = 'my-custom-name';
    return $names;
}
add_filter('subdirectory_reserved_names', 'add_custom_reserved_name');

Remove a reserved site name

This example removes a reserved site name from the list of reserved names.

function remove_reserved_name($names) {
    $position = array_search('wp-json', $names);
    if ($position !== false) {
        unset($names[$position]);
    }
    return $names;
}
add_filter('subdirectory_reserved_names', 'remove_reserved_name');