How can I turn this off for individual sites in a multi-site install?

How can I turn off CoursePress Pro for individual sites in a multi-site install?

  • aristath

    Hello there @Dennis, I hope you're well today!

    Network-activated sites in WordPress can't be de-activated on individual sites.
    However, you could hack your way to make this work...
    If you open the coursepress.php file you'll notice that the CoursePress class is only loaded if it has not already been declared to avoid any conflicts.
    So we can take advantage of that.

    Create a new php file in your wp-content/mu-plugins folder. If that folder does not already exist, manually create it.
    So let's say you create a new file there called disable-coursepress.php
    Paste the following in that file and save it:

    <?php
    
    if ( !defined('ABSPATH') ) exit;
    
    $blog_id = get_current_blog_id();
    
    $disabled_blogs = array( 5, 12, 68 );
    
    if ( in_array( $blog_id, $disabled_blogs ) ) {
    
        class CoursePress {
    
            private static $instance = null;
    
            function __construct() {}
    
            public static function instance( $instance = null ) {
                if ( !$instance || 'CoursePress' != get_class($instance) ) {
                    if ( is_null(self::$instance) ) {
                        self::$instance = new CoursePress();
                    }
                } else {
                    if ( is_null(self::$instance) ) {
                        self::$instance = $instance;
                    }
                }
                return self::$instance;
            }
    
        }
    
    }

    That code will do the following:
    1. Get the ID of the current blog (line 5)
    2. Create an array of blog IDs where we want the plugin disabled (line 7). In this example I'm saying that I want the plugin disabled on sites 5, 12, 68 but you can change these to whatever you want.
    3. Check if the current site exists in our array of disabled site (line 9). If it does, then it continues to declare an essentially empty class.

    files in the mu-plugins folder are run before all other plugins are loaded.
    So now if a visitor visits site 5, 12 or 68, the CoursePress class will be defined before the plugin is executed and thus the plugin will never load.

    P.S. I haven't tested the above code but I don't see why it wouldn't run...

    I hope that helps!

    Cheers,
    Ari.