-1

Is there a filter available that would let you specify that a module is active, without you having to use the admin area? I've found filters that let you hide modules, but so far nothing for activating modules.

Essentially, I want to be able to define it as always on so that A) a client can't accidentally disable it, B) it saves me from having to sync DB settings across different environments.

2

Third party plugins (yeah, including jetpack) are off topic on WPSE, but well, here you go: Jetpack has a filter jetpack_get_available_modules, which lets you edit the array of active modules. You can disable a module by unsetting it from the array, or enable it by adding it. Here's how to enable a single module, 'sharedaddy':

function wpse248480_filter_jetpack( $modules, $min_version, $max_version ) {
    if (!(in_array('sharedaddy',$modules))) $modules[] = 'sharedaddy';
    return $modules;
    }
add_filter( 'jetpack_get_available_modules', 'wpse248480_filter_jetpack', 20, 3 );

UPDATE

The above only filters the available modules. To actually (de)activate them programmatically use (source):

function wpse248480_activate_jetpack () {
  Jetpack::activate_module('sharedaddy');
  // Jetpack::deactivate_module('sharedaddy');
  }
add_action('after_setup_theme','wpse248480_activate_jetpack');
  • No dice I'm afraid; that filters the modules that are available to be activated, not the modules that are actually activated. – Dre Dec 6 '16 at 19:44
  • Can you explain "third party"? Jetpack is not a third party. It is maintained by the Automattic github.com/Automattic. – prosti Dec 6 '16 at 22:27
  • @prosti WPSE is a gathering place for WP core developers. Anything not in core is considered third party. Jetpack en Woocommerce have their own fora, where you are more likely to find people who know about these particular plugins. – cjbj Dec 7 '16 at 8:47
  • Nice @cjbj, it is still OK to post a question with plugin-jetpack tag and woocommerce tag, right. I get the feeling because these tags exist WPSE welcomes these questions. +1 for the "fora" word. – prosti Dec 7 '16 at 9:02
  • @prosti There are always border cases between core and plugins. Or you can post the relevant code from the plugin, to make a question on topic here. It's not about your being welcome. It's about the community here being able to help you. – cjbj Dec 7 '16 at 9:06
0

There is an official filter, jetpack_active_modules, for this now. It will force enable/disable modules as needed.

Here is an example that will disable everything, and uncommenting a line will enable that module. In your case, you could simply make sure the string describing your desired module is present in the array before returning it.

function wpse248480_jetpack_active_modules( $active ) {
    $active = array(
        // 'after-the-deadline',
        // 'carousel',
        // 'comment-likes',
        // 'comments',
        // 'contact-form',
        // 'custom-content-types',
        // 'custom-css',
        // 'enhanced-distribution',
        // 'google-analytics',
        // 'gravatar-hovercards',
        // 'infinite-scroll',
        // 'json-api',
        // 'latex',
        // 'lazy-images',
        // 'likes',
        // 'manage',
        // 'markdown',
        // 'masterbar',
        // 'minileven',
        // 'module-extras',
        // 'module-headings',
        // 'module-info',
        // 'monitor',
        // 'notes',
        // 'photon',
        // 'post-by-email',
        // 'protect',
        // 'publicize',
        // 'pwa',
        // 'related-posts',
        // 'search',
        // 'seo-tools',
        // 'sharedaddy',
        // 'shortcodes',
        // 'shortlinks',
        // 'sitemaps',
        // 'sso',
        // 'stats',
        // 'subscriptions',
        // 'theme-tools',
        // 'tiled-gallery',
        // 'vaultpress',
        // 'verification-tools',
        // 'videopress',
        // 'widget-visibility',
        // 'widgets',
        // 'wordads',
        // 'wpgroho.js',
    );

    return $active;
}

add_filter( 'jetpack_active_modules', 'wpse248480_jetpack_active_modules' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.