0

Im trying to trigger a configuration everytime I create a new subsite on multisite environment, like so:

add_action('init', 'mgh_set_events_option', 99); 
function mgh_set_events_option(){
  $mgh_is_set_options = get_option('mgh_is_set_options');
    if(!$mgh_is_set_options){
        print_r('setting options');
        update_option( 'mgh_is_set_options', true );
    }
}

The problem is that the 'init' action does not trigger in some sites from our multisite instance. There is any principle of why this happens?

6
  • Is this code in a plugin? Is that plugin active on all sites? Jan 19 at 16:32
  • @JacobPeattie Hi, not a plugin, only a snippet on functions.php
    – ingntovar
    Jan 19 at 16:42
  • I think the problem is less what you're doing, and more where you're doing it
    – Tom J Nowell
    Jan 19 at 17:16
  • 1
    If it's in a theme's functions.php, it'll only run on sites where that theme is active. You can ensure that it's active on all your sites by putting that code snippet into a plugin that's active on every site (or in a Must-Use plugin).
    – Pat J
    Jan 19 at 17:17
  • 1
    @PatJ you should leave that as an answer not a comment, Jacobs answer is also in the right direction
    – Tom J Nowell
    Jan 19 at 17:17

2 Answers 2

1

If your code is in a theme's functions.php, it'll only run on sites where that theme is active. You can ensure that it's active on all your sites by putting that code snippet into a plugin that's active on every site (ie, Network Activated). Alternately, you can use a Must-Use plugin to ensure that it runs on every site in your Multisite network.

1
  • Thanks! My theme was deactivated, that's why the init wasn't triggering!
    – ingntovar
    Jan 19 at 18:05
2

The init hook will run whenever WordPress is loaded. So if you have a plugin or theme that uses the init hook activated on multiple sites it will work on all sites that the plugin or theme is active on.

However, the hook will only run for the site that is loaded. If you load a page on Site A your callback will run and update the option on Site A, but it will not update the option on Site B until a page on Site B is loaded and the hook is run for that site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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