Use TGM Plugin Activation Conditionally

If you follow official guide, you will always include TGM Plugin Activation file. Most of the times you won’t need this additional 94 KB. In this case, you can use conditional loading very simply.

First, make function for registration as suggested and hook it to specific TGM Plugin Activation action. Then, create a new function (or put code inside existing one) that should be loaded before init with priority 10 (you can even hook it to init with higher priority).

In this example, we require plugin Posts 2 Posts and TGM Plugin Activation file is placed inside child inc directory. You only need to check if your requested plugin is loaded, for example, Posts 2 Posts uses function _p2p_load() in its main file.

So the code is:

function md_tgmpa_init() {
  // Load TGM Plugin Activation
  if ( ! function_exists( '_p2p_load' ) ) {
    require_once dirname( __FILE__ ) . '/inc/class-tgm-plugin-activation.php';
add_action( 'init', 'md_tgmpa_init', 7 );

Leave a Reply

Comment Guidelines:
  • Your email address will not be published
  • Fields marked with * are required
  • If you want your picture next to comment, open an account at Gravatar
  • If you want to know if someone replied to your comment, check “Notify me of followup comments via e-mail”
  • You can use these HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>