register_activation_hook in mu-plugin not triggering

I have a class setup to run some database scripts on my mu-plugin.

function activate_mjmc_core() {
 require_once plugin_dir_path( __FILE__ ) . 'includes/class-mjmc-core-activator.php';
 Mjmc_Core_Activator::activate();
 Mjmc_Core_Activator::mjmc_database_version();
 Mjmc_Core_Activator::mjmc_companies_table();
 Mjmc_Core_Activator::mjmc_locations_table();
 Mjmc_Core_Activator::mjmc_companies_insert();
}

register_activation_hook( __FILE__, 'activate_mjmc_core' );

Now these work just fine when i use this as a normal plugin where i have to activate it.

But when i use this in my mu-plugins, all the other plugin functions work, it just doenst run the database functions listed above on activate. Is there something else i need to do on these database functions? or run this on another wordpress hook?

thanks

2 Answers
2

MU plugins don’t ‘activate’, or ‘deactivate’. They’re just present or not present. So you’ll need a different approach. One option would be to just perform your activation functions on init, and store a record in the database to say whether or not it’s already been done:

function activate_mjmc_core() {
    if ( '1' === get_option( 'mjmc_activated' ) ) {
        return;
    }

    require_once plugin_dir_path( __FILE__ ) . 'includes/class-mjmc-core-activator.php';

    Mjmc_Core_Activator::activate();
    Mjmc_Core_Activator::mjmc_database_version();
    Mjmc_Core_Activator::mjmc_companies_table();
    Mjmc_Core_Activator::mjmc_locations_table();
    Mjmc_Core_Activator::mjmc_companies_insert();

    update_option( 'mjmc_activated', '1' );
}
add_action( 'init', 'activate_mjmc_core' );

Leave a Comment