Auto-Activating and Configuring Akismet for WordPress (Standard and Multisite)

If you are running a WordPress Network (A.K.A WP Multisite), you might want to automatically activate, and configure, the Akismet plugin for every site powered by that MS instance.

Note: although the following is most commonly used in Multisite, the same code works just fine in standard WordPress.

Auto-Activating the Plugin

To do that, you can leverage WordPress Must Use Plugins (A.K.A. mu-plugins).

Here is an example mu-plugin that can live in wp-content/mu-plugins/akismet-loader.php:

<?php
function wpms_akismet_loader() {
        require_once( WP_PLUGIN_DIR . '/akismet/akismet.php' );
}

add_action( 'plugins_loaded', 'wpms_akismet_loader' );

function wpms_akismet_disable_plugin_actions( $actions, $plugin_file, $plugin_data, $context ) {
        if ( 'akismet/akismet.php' === $plugin_file ) {
                return array();
        }
        return $actions;
}

add_filter( 'plugin_action_links', 'wpms_akismet_disable_plugin_actions', 10, 4 );

The wpms_akismet_loader() function/action will load/enable the Akismet Plugin bundled with WP in wp-content/plugins/akismet/, without individual site administrators having to do so manually.

The wpms_akismet_disable_plugin_actions() function/filter will disable the actions normally found under a plugin’s name to avoid having your users enabling/disabling/editing/deleting the bundled Akismet plugin that you have auto-enabled in the previous step, leaving only the Settings link to configure Akismet.

Auto-Configuring the API Key

With only the above mu-plugin installed, each of the sites in the MS install would still have to configure Akismet with their own API key. This might be desirable, but you might also want to enable the same API key for all of the sites served by said instance.

To do so, you can leverage a special constant in the wp-config.php file that contains your WordPress install’s hardcoded settings: WPCOM_API_KEY. The constant starts with WPCOM_ because it was first used to configure such features on WordPress.com, the largest WP Multisite instance in the world.

Simply add the following anywhere in the wp-config.php file found at the root of your WP install, where 12345qwerty is the Akismet API key you want to be used everywhere:

// AKISMET API KEY
define('WPCOM_API_KEY','12345qwerty');

Note that once the constant is configured, the text field in the Akismet settings screen where one would normally enter the API key will no longer be displayed.