Make My Custom Plugin Load Last

I am creating a custom plugin to override default functionality of 3rd party plugins used on my multi-site network. The problem is some of the plugins are activated at the site level instead of the network level and due to the order of plugin execution, my network activated custom plugin has already finished running its overrides before site level plugins have run, rendering them useless.

Does anyone know how to make a custom plugin run last (even after site level plugins) to ensure you have the last say?

    • adam_fisher

      Thanks Tyler. I modified the function they mentioned to be the last plugin loaded instead of the first. The problem is, my plugin (network activated) does not show up in the list of "active_plugins" options array. I think this is because it is not activated at the site level but the network level.

      I don't think I can just blindly add to the end of the "active_plugins" array because it would mean my plugin loads twice (once at network level and again at site level). This is close but I'm still unable to make my plugin run after other site level plugins.

      How do you guys do it with your plugins? Most of them are made for network activation and you override site level plugins?

      function load_this_plugin_last() {
          // ensure path to this file is via main wp plugin path
          $wp_path_to_this_file = preg_replace('/(.*)plugins\/(.*)$/', WP_PLUGIN_DIR."/$2", __FILE__);
          $this_plugin = plugin_basename(trim($wp_path_to_this_file));
          $active_plugins = get_option('active_plugins');
          $this_plugin_key = array_search($this_plugin, $active_plugins);
      
          if (in_array($this_plugin) && end($active_plugins) != $this_plugin ) {
              array_splice($active_plugins, $this_plugin_key, 1);
              array_push($active_plugins, $this_plugin);
              update_option('active_plugins', $active_plugins);
          }
      }
      add_action("activated_plugin", "load_this_plugin_last");
      add_action("deactivated_plugin", "load_this_plugin_last");
  • adam_fisher

    The specific example I am thinking of is that I am using WordPress Social Login on my main website (activated on the main site only) and I have my custom plugin activated at the network level. I want to override the shortcode they expose [wordpress_social_login] using the same shortcode name.

    I am sure there will be other scenarios where this type of thing may pop up and there is no priority order for it (as is the case with shortcodes).

    • adam_fisher

      Thanks for the idea Ari but the problem is a network activated plugin runs before plugins activated at the site level. So as a rough example, when I call remove_shortcode('wordpress_social_login', 'my_social_login_function'); the add_shortcode has not even happened by the WP Social Login plugin yet. So by the time I call remove and then add, my code will be in place but then the WP Social Login plugin will run after because it was activated at the site level and just overwrite the add_shortcode that mine wrote with the same key and its own function.

      Surely there is a way to make a plugin load last after site level activated plugins have run?

  • aristath

    Thanks for the idea Ari but the problem is a network activated plugin runs before plugins activated at the site level.

    True... so you can simply wrap everything you want in a parent function and add it later using add_action().

    Example:

    function my_custom_queue() {
      remove_shortcode('wordpress_social_login', 'my_social_login_function');
    }
    add_action( 'wp', 'my_custom_queue' );

    I'm adding it on the 'wp' hok, but you could add it wherever you want using the one of the hooks here: http://codex.wordpress.org/Plugin_API/Action_Reference

    This is a simple but effective and widely used way of ordering actions, filters and functions. Using the proper WordPress hook and a wrapper function we can prioritize loading of our custom code however we want. :wink:

    I hope that helps!

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.