Using WordPress ‘allow_subdomain_install()’ PHP function

The allow_subdomain_install() WordPress PHP function is used to check if a multisite network allows subdomain installations.

Usage

Here’s a basic way to use the allow_subdomain_install() function. This example checks if subdomain installation is allowed and prints a message based on the result.

if (allow_subdomain_install()) {
    echo 'Subdomain installation is allowed.';
} else {
    echo 'Subdomain installation is not allowed.';
}

If subdomain installation is allowed, this will print ‘Subdomain installation is allowed.’ Else, it will print ‘Subdomain installation is not allowed.’

Parameters

  • This function does not accept any parameters.

More information

See WordPress Developer Resources: allow_subdomain_install

Examples

Checking for subdomain install in a conditional

In this example, we use the function to conditionally run some code if a subdomain install is allowed.

// Use the function in a conditional
if (allow_subdomain_install()) {
    // Run some code...
}

If subdomain installation is allowed, the code within the conditional will be executed.

Displaying a notice based on subdomain install

Here we use the function to display a different admin notice depending on whether a subdomain install is allowed or not.

// Check if subdomain install is allowed
if (allow_subdomain_install()) {
    echo 'You can create new subdomains.';
} else {
    echo 'You cannot create new subdomains.';
}

This will display a message to the user informing them of whether they can create new subdomains or not.

Enabling a feature if subdomain install is allowed

In this example, the function is used to enable a specific feature if a subdomain install is allowed.

// Check if subdomain install is allowed
if (allow_subdomain_install()) {
    // Enable some feature...
}

If a subdomain install is allowed, the feature within the conditional statement will be enabled.

Logging subdomain install status

This example uses the function to write a log message regarding the subdomain install status.

// Log the subdomain install status
error_log('Subdomain install allowed: ' . (allow_subdomain_install() ? 'Yes' : 'No'));

This will write a log message indicating whether a subdomain install is allowed or not.

Redirecting based on subdomain install status

In this last example, the function is used to redirect the user to different pages based on whether a subdomain install is allowed.

// Redirect based on subdomain install status
header('Location: ' . (allow_subdomain_install() ? '/subdomain-page' : '/regular-page'));

If a subdomain install is allowed, the user will be redirected to ‘/subdomain-page’. If not, they’ll be redirected to ‘/regular-page’.