How to remove_action inside class [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite













This question already has an answer here:



  • remove_action or remove_filter with external classes?

    5 answers



i have class like this




if ( ! class_exists( 'My_class' ) )
class My_class
public function __construct()
add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


public function change_title()
echo 'The title';




return new My_class();


I trying to remove_action change_title by using




remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );
// or
remove_action( 'woocommerce_before_shop_loop_item_title', 10 );


But it does not work because I don't really understand PHP OOP.
How can I do that, is it possible?



And can i remove it via a plugin?



Thanks







share|improve this question














marked as duplicate by Jacob Peattie, TheDeadMedic, shanebp, fuxia♦ Aug 25 at 10:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    up vote
    2
    down vote

    favorite













    This question already has an answer here:



    • remove_action or remove_filter with external classes?

      5 answers



    i have class like this




    if ( ! class_exists( 'My_class' ) )
    class My_class
    public function __construct()
    add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


    public function change_title()
    echo 'The title';




    return new My_class();


    I trying to remove_action change_title by using




    remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );
    // or
    remove_action( 'woocommerce_before_shop_loop_item_title', 10 );


    But it does not work because I don't really understand PHP OOP.
    How can I do that, is it possible?



    And can i remove it via a plugin?



    Thanks







    share|improve this question














    marked as duplicate by Jacob Peattie, TheDeadMedic, shanebp, fuxia♦ Aug 25 at 10:32


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite












      This question already has an answer here:



      • remove_action or remove_filter with external classes?

        5 answers



      i have class like this




      if ( ! class_exists( 'My_class' ) )
      class My_class
      public function __construct()
      add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


      public function change_title()
      echo 'The title';




      return new My_class();


      I trying to remove_action change_title by using




      remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );
      // or
      remove_action( 'woocommerce_before_shop_loop_item_title', 10 );


      But it does not work because I don't really understand PHP OOP.
      How can I do that, is it possible?



      And can i remove it via a plugin?



      Thanks







      share|improve this question















      This question already has an answer here:



      • remove_action or remove_filter with external classes?

        5 answers



      i have class like this




      if ( ! class_exists( 'My_class' ) )
      class My_class
      public function __construct()
      add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


      public function change_title()
      echo 'The title';




      return new My_class();


      I trying to remove_action change_title by using




      remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );
      // or
      remove_action( 'woocommerce_before_shop_loop_item_title', 10 );


      But it does not work because I don't really understand PHP OOP.
      How can I do that, is it possible?



      And can i remove it via a plugin?



      Thanks





      This question already has an answer here:



      • remove_action or remove_filter with external classes?

        5 answers









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 17 at 7:40

























      asked Aug 17 at 4:37









      ttn_

      276




      276




      marked as duplicate by Jacob Peattie, TheDeadMedic, shanebp, fuxia♦ Aug 25 at 10:32


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jacob Peattie, TheDeadMedic, shanebp, fuxia♦ Aug 25 at 10:32


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          It's not possible to remove it with remove_action() the way you've written it.



          When you hooked it here:



          add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


          The $this means that the function that's hooked is on this specific instance of My_class. When using remove_action() you need to pass the same instance of the class:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( $instance, 'change_title' ), 10 );


          (where $instance is the instance of the class).



          The problem is that the instance of the class is not available anywhere else because you've instantiated into nothing:



          return new My_class();


          To remove the action you need to put the class instance into a variable and then use that variable to remove it later:



          $my_class = new My_class();


          Then in your other code:



          global $my_class;

          remove_action( 'woocommerce_before_shop_loop_item_title', array( $my_class, 'change_title' ), 10 );


          Another option is that if change_title is static then you don't need a specific instance of the class to add and remove it from hooks.



          So make the function static:



          public static function change_title() 
          echo 'The title';



          Then to hook a static method you do this:



          add_action( 'woocommerce_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          Which means you can remove it like this:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          But whether your class/functions can or should be static is a larger architectural question that would depend on exactly what you're doing.






          share|improve this answer




















          • Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
            – ttn_
            Aug 17 at 7:38







          • 1




            @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
            – kero
            Aug 17 at 8:28

















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          It's not possible to remove it with remove_action() the way you've written it.



          When you hooked it here:



          add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


          The $this means that the function that's hooked is on this specific instance of My_class. When using remove_action() you need to pass the same instance of the class:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( $instance, 'change_title' ), 10 );


          (where $instance is the instance of the class).



          The problem is that the instance of the class is not available anywhere else because you've instantiated into nothing:



          return new My_class();


          To remove the action you need to put the class instance into a variable and then use that variable to remove it later:



          $my_class = new My_class();


          Then in your other code:



          global $my_class;

          remove_action( 'woocommerce_before_shop_loop_item_title', array( $my_class, 'change_title' ), 10 );


          Another option is that if change_title is static then you don't need a specific instance of the class to add and remove it from hooks.



          So make the function static:



          public static function change_title() 
          echo 'The title';



          Then to hook a static method you do this:



          add_action( 'woocommerce_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          Which means you can remove it like this:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          But whether your class/functions can or should be static is a larger architectural question that would depend on exactly what you're doing.






          share|improve this answer




















          • Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
            – ttn_
            Aug 17 at 7:38







          • 1




            @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
            – kero
            Aug 17 at 8:28














          up vote
          6
          down vote



          accepted










          It's not possible to remove it with remove_action() the way you've written it.



          When you hooked it here:



          add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


          The $this means that the function that's hooked is on this specific instance of My_class. When using remove_action() you need to pass the same instance of the class:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( $instance, 'change_title' ), 10 );


          (where $instance is the instance of the class).



          The problem is that the instance of the class is not available anywhere else because you've instantiated into nothing:



          return new My_class();


          To remove the action you need to put the class instance into a variable and then use that variable to remove it later:



          $my_class = new My_class();


          Then in your other code:



          global $my_class;

          remove_action( 'woocommerce_before_shop_loop_item_title', array( $my_class, 'change_title' ), 10 );


          Another option is that if change_title is static then you don't need a specific instance of the class to add and remove it from hooks.



          So make the function static:



          public static function change_title() 
          echo 'The title';



          Then to hook a static method you do this:



          add_action( 'woocommerce_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          Which means you can remove it like this:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          But whether your class/functions can or should be static is a larger architectural question that would depend on exactly what you're doing.






          share|improve this answer




















          • Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
            – ttn_
            Aug 17 at 7:38







          • 1




            @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
            – kero
            Aug 17 at 8:28












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          It's not possible to remove it with remove_action() the way you've written it.



          When you hooked it here:



          add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


          The $this means that the function that's hooked is on this specific instance of My_class. When using remove_action() you need to pass the same instance of the class:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( $instance, 'change_title' ), 10 );


          (where $instance is the instance of the class).



          The problem is that the instance of the class is not available anywhere else because you've instantiated into nothing:



          return new My_class();


          To remove the action you need to put the class instance into a variable and then use that variable to remove it later:



          $my_class = new My_class();


          Then in your other code:



          global $my_class;

          remove_action( 'woocommerce_before_shop_loop_item_title', array( $my_class, 'change_title' ), 10 );


          Another option is that if change_title is static then you don't need a specific instance of the class to add and remove it from hooks.



          So make the function static:



          public static function change_title() 
          echo 'The title';



          Then to hook a static method you do this:



          add_action( 'woocommerce_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          Which means you can remove it like this:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          But whether your class/functions can or should be static is a larger architectural question that would depend on exactly what you're doing.






          share|improve this answer












          It's not possible to remove it with remove_action() the way you've written it.



          When you hooked it here:



          add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );


          The $this means that the function that's hooked is on this specific instance of My_class. When using remove_action() you need to pass the same instance of the class:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( $instance, 'change_title' ), 10 );


          (where $instance is the instance of the class).



          The problem is that the instance of the class is not available anywhere else because you've instantiated into nothing:



          return new My_class();


          To remove the action you need to put the class instance into a variable and then use that variable to remove it later:



          $my_class = new My_class();


          Then in your other code:



          global $my_class;

          remove_action( 'woocommerce_before_shop_loop_item_title', array( $my_class, 'change_title' ), 10 );


          Another option is that if change_title is static then you don't need a specific instance of the class to add and remove it from hooks.



          So make the function static:



          public static function change_title() 
          echo 'The title';



          Then to hook a static method you do this:



          add_action( 'woocommerce_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          Which means you can remove it like this:



          remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );


          But whether your class/functions can or should be static is a larger architectural question that would depend on exactly what you're doing.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 17 at 5:24









          Jacob Peattie

          12.3k41525




          12.3k41525











          • Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
            – ttn_
            Aug 17 at 7:38







          • 1




            @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
            – kero
            Aug 17 at 8:28
















          • Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
            – ttn_
            Aug 17 at 7:38







          • 1




            @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
            – kero
            Aug 17 at 8:28















          Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
          – ttn_
          Aug 17 at 7:38





          Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
          – ttn_
          Aug 17 at 7:38





          1




          1




          @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
          – kero
          Aug 17 at 8:28




          @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
          – kero
          Aug 17 at 8:28


          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Why am i infinitely getting the same tweet with the Twitter Search API?

          Carbon dioxide