Using WordPress ‘apache_mod_loaded()’ PHP function

The apache_mod_loaded() WordPress PHP function determines if a specified Apache module exists in the configuration.

Usage

Here’s a simple example of using apache_mod_loaded(). Let’s check if the mod_rewrite module is loaded:

if( apache_mod_loaded('mod_rewrite', false) ) {
    echo "mod_rewrite is loaded!";
} else {
    echo "mod_rewrite is not loaded.";
}

If mod_rewrite is loaded, this will output “mod_rewrite is loaded!”. If it’s not, it will output “mod_rewrite is not loaded.”.

Parameters

  • $mod (string, required) – The name of the Apache module you want to check, like ‘mod_rewrite’.
  • $default_value (boolean, optional) – What to return if the module isn’t found. Default is false.

More information

See WordPress Developer Resources: apache_mod_loaded()

This function is not deprecated, and it’s available in all versions of WordPress that support Apache modules.

Examples

Check if mod_deflate is loaded

if( apache_mod_loaded('mod_deflate', false) ) {
    echo "mod_deflate is available!";
}

This code checks if mod_deflate is available. If it is, it prints “mod_deflate is available!”.

Use a default value

$result = apache_mod_loaded('unknown_module', true);
echo $result ? 'Module is loaded.' : 'Module is not loaded.';

Here, the function will return true even if ‘unknown_module’ doesn’t exist, because we’ve set $default_value to true.

Check for mod_ssl

if( apache_mod_loaded('mod_ssl', false) ) {
    echo "SSL support is available!";
}

This prints “SSL support is available!” if the mod_ssl module is loaded.

Check for mod_php

if( apache_mod_loaded('mod_php', false) ) {
    echo "PHP support is available!";
}

This checks if PHP is supported by checking for the mod_php module.

Check for multiple modules

$modules = array('mod_rewrite', 'mod_deflate', 'mod_ssl');

foreach ($modules as $module) {
    if( apache_mod_loaded($module, false) ) {
        echo "$module is available!";
    } else {
        echo "$module is not available.";
    }
}

This checks for several modules (mod_rewrite, mod_deflate, and mod_ssl) and prints a message for each one, indicating whether it’s available or not.