2

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

  • How are you creating your tables? WP provides dbDelta which can create tables if they don't exist, and even update their schemas if you decide to change a column or add a new one – Tom J Nowell Dec 12 '19 at 2:49
  • those are in the called functions and are working fine, just not being called since there is no mu-plugin activation hook. Jacob down below pointed me in the right direction – joelanco Dec 12 '19 at 3:01
3

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' );
-1

You need to switch to siite for run this activation hook.

function activate_mjmc_core() {
$current_site_id = get_current_blog_id();
switch_to_blog($current_site_id);
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();
restore_current_blog();
}

Hope this work. :D

register_activation_hook( FILE, 'activate_mjmc_core' );

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.