• Hello,
    I am trying to add hooks to one of our plugins that if it is activated another plugin on the site will automatically be activated. What is the best way to do this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there!

    Please refer to the given link, this should help you out.

    https://adambrown.info/p/wp_hooks

    Thanks

    Moderator t-p

    (@t-p)

    Plugin Handbook – Hooks

    Thread Starter tuf08694

    (@tuf08694)

    so far this is what I have and seem to not be working

    new TU_ActivateGravForms();
    
    class TU_ActivateGravForms {
    
      
    
        public function __construct() {
            // Add our methods below to the hooks
            add_action('activated_plugin', array($this, 'activate'), 10, 2);
        }
    
       
    
        // Activating any plugins
        public function activate($Plugin, $Network) {
            if (FALSE !== stripos($Plugin, 'gravityformspolls')) {
                $Blog = get_bloginfo('url');
    
            //Add gravity forms hook when gravityformspolls is activated
                     function register_activation_hook($file, $function) {
                 $file =plugin_basename($file);
                    add_action('activate_gravityforms' . $file, $function);
                     }
                }
    
    }}
    • This reply was modified 6 years ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    Despite seeming logical, because of the order things load when activating a plugin, hooking “activated_plugin” is not how to run plugin code on activation. You call register_activation_hook() directly inside of TU_ActivateGravForms::__construct() or external to your class, but within the plugin. The passed callable is the class method that will be called when your plugin is activated. Refer to the Codex entry.

    Within that registered class method, to activate another plugin, call activate_plugin(). I don’t really know if activate_plugin() will work in this context since we are in the process of activating another plugin, but it’s the first thing to try. If it fails, you may need to schedule an event in the near future that activates the plugin.

    BTW, when posting code in these forums, please demarcate code with backticks or use the “code” button of the editor. Otherwise the forum’s parser corrupts your code, making it difficult for others to test your code themselves. I fixed your previous code just because I can ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘hooks to activate a plugin’ is closed to new replies.