• Resolved KirillTaylor

    (@kirilltaylor)


    Hi,

    I’ve got some 5-6 plugins which deliver certain bonus upon purchase (via woocommerce).
    1 basic class which others extend to. I’ve had an idea running them in a certain order within the basic class using a custom hook.
    My question is: is it possible to fire actions using something like this?
    add_action( 'my_custom_hook', array( $className, 'my_method'));

    (couldn’t find any documentation or code examples for this)

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m assuming that $className is not an instance of the class, but rather just the name, right? In other words, this will be called statically?

    If so, on the add_action WordPress documentation page it states the following:

    If the class is called statically the approach has to be like below as $this is not available. This also works if class is extended. Use the following:

    class MyPluginClass {
    
        public static function init() {
    
             add_action( 'save_post', array( get_called_class(), 'myplugin_save_posts' ) );
        }
    
        public static function myplugin_save_posts() {
             // do stuff here...
        }
    }
    
    MyPluginClass::init();

    This would lead me to believe that it is possible.

    Thread Starter KirillTaylor

    (@kirilltaylor)

    Thanks,

    That’s not exactly the way I had imagined it. I did solve the issue, though. Non-static class, in the constructor, different hooks for different methods within the class. Such as

    function __construct(){
         add_action('hook1', 'method1');
         add_action('hook2', 'method6');
         add_action('hook3', 'method3');
    }

    Thanks for the help anyway ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Combining multiple plugins into one’ is closed to new replies.