TGMPA / TGM-Plugin-Activation

TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install, update and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference bundled plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet.
http://tgmpluginactivation.com/
GNU General Public License v2.0
1.76k stars 430 forks source link

Override TGMPA plugins on parent theme from child theme #605

Closed madastro closed 8 years ago

madastro commented 8 years ago

I have a parent theme, pre-built, which uses TGMPA and would like to override the defined plugins via a child-theme so that I don't have to edit anything on the parent in case of updates. Is this possible?

Also, I've also tried using TGMPA, for loading plugins which are used by the child-theme only, but it does not display the added required plugin list in the dashboard.

the functions.php on the child theme has this:

// Pulls the TGM class from the parent theme
include get_template_directory() . '/includes/class-tgm-plugin-activation.php';
add_action( 'tgmpa_register', 'required_plugins' );
function required_plugins() {
    $plugins = array(
        array(
          'name' => 'Wordpress SEO by Yoast',
          'slug' => 'wordpress-seo',
          'required' => false,
        ),
    );
}
jrfnl commented 8 years ago

Hi @madastro, without knowing more about how the parent theme has been set up, I can only give you solution directions. Here are a few options which come to mind:

  1. If the parent-theme has created their required_plugins() function as a pluggable function, i.e. wrapped within a if ( !function_exists() ) {} call, then you can overrule the parent function from your child-theme, by naming your function the same as theirs. The child-theme's functions.php file will be loaded before the parent's functions.php file, so you can effectively overrule any function they've made pluggable.
  2. If the parent-theme did not make the function pluggable, have a look a their code and find where and how they add their function to the tgmpa_register hook. Once they've registered it, you can then unregister it. This will effectively allow you to overrule their required/recommended plugins.

    Example in simplified pseudo-code:

    // Parent theme registers their TGMPA hook within their `after_setup_theme()` function:
    add_action( 'after_setup_theme', 'parent_after_setup_theme' );
    function parent_after_setup_theme() {
       add_action( 'tgmpa_register', 'parent_required_plugins' );
    }
    // What you could then do in your child-theme:
    
    // Register your own TGMPA hook-in:
    add_action( 'after_setup_theme', 'child_after_setup_theme' );
    function child_after_setup_theme() {
       add_action( 'tgmpa_register', 'child_required_plugins' );
    }
    
    // Unregister the function from the parent theme.
    add_action( 'after_setup_theme', 'child_after_setup_theme_tweak_parent', 11 ); // Any number higher than the prio the parent uses or higher than 10 if they don't set a prio.
    function child_after_setup_theme_tweak_parent() {
       remove_action( 'tgmpa_register', 'parent_required_plugins' );
    }

Pro-tip: make sure you include TGMPA in the child-theme just in case the parent would decide to ditch TGMPA. Also: verify the version the parent-theme uses and please ask them to update if they don't use the latest version (2.6.1 at the time of writing this).

I've also tried using TGMPA, for loading plugins which are used by the child-theme only, but it does not display the added required plugin list in the dashboard.

I'm not sure what you mean by "display the added required plugin list in the dashboard". TGMPA does not display anything on the dashboard page. However, it will add a menu item to install required/recommended plugins, but only if these plugins aren't installed already. So if you already have WPSEO installed, the page will not be added to the menu and will not display.

Does that help ? If this doesn't answer your question, please clarify and I'll try and help.

madastro commented 8 years ago

Thanks @jrfnl for the detailed response and will try what you have outlined and will give an update.

Regarding the plugin display, sorry, I meant the list under Appearance->Install Plugins, and yes I try to install/uninstall the plugins for testing.

jrfnl commented 8 years ago

Regarding the plugin display, sorry, I meant the list under Appearance->Install Plugins, and yes I try to install/uninstall the plugins for testing.

Depending on how the parent theme has configured the location for TGMPA and/or whether there are plugins installed which use TGMPA and may have a different location configured, the TGMPA admin page can be located at any location in the admin menu, not just the default Appearance->Install Plugins. I realized that this is confusing, but that is the current set-up (will be changed in v3).

You could use the Debug Bar in combination with the Actions and Filters add-on to check which functions are registered to the tgmpa_register hook and check within those functions if any of them change the menu or parent-slug items to see where the page is supposed to be in your install. See: http://tgmpluginactivation.com/configuration/#h-configuration-options

Do you see the admin notice ? and if not: did you ever dismiss it ?

madastro commented 8 years ago

@jrfnl thanks again for the detailed response.

I've tried the example code on your first reply and have not gotten it to work yet, so I'm still investigating.

The parent theme isn't pluggable so I've used the second option, below is the code so far:

// Setup child plugins
add_action( 'after_setup_theme', 'child_theme_plugins' );
function child_theme_plugins() {
  add_action( 'tgmpa_register', 'register_theme_plugins' );
}

// Remove parent plugins
add_action( 'after_setup_theme', 'remove_parent_plugins', 11 );
function remove_parent_plugins() {
  remove_action( 'tgmpa_register', 'parent_plugins' );
}

// Register plugins
function register_theme_plugins() {
  $plugins = array(
    array(
      'name' => 'FV Top Level Categories',
      'slug' => 'fv-top-level-cats',
      'required' => false,
    ),
  );
}

Once that's in, the TGMPA notice disappears and the 'Install Plugins' menu under Appearance as well, and when I comment out the add_action( 'after_setup_theme', 'remove_parent_plugins', 11 ); part, I get the notice and menu back.

I tried a couple of stuff as well, like removing the priority part (11) in the remove_parent_plugins hook. I've also switched the order of the code by moving the parent plugin hook before the child, but I get the same issue.

I've also updated TGMPA on both parent and child for testing, but I get the parent_url_slug error, so I'm still looking into the configuration.

Anyway, will comment once I've figured out a few things. Thanks again.

madastro commented 8 years ago

Alrighty, this is working for me now and have resolved the slug error on update. Issue was a typo, wherein the word "required" was spelled "requred" on the parent theme, sheesh!

Thanks @jrfnl for all the detailed help.

Cheers!

jrfnl commented 8 years ago

@madastro Glad to hear you've got it working.

One thing I noticed about the code you posted earlier: make sure when you define your plugins array to also call the tgmpa() function to pass them on. So your register_theme_plugins() function should end with this before the closing brace }:

tgmpa( $plugins );

I'd suggest you have a look at the example file and the configuration help page for more info if you still need it.