$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Remove an action by extending class and replacing it|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Remove an action by extending class and replacing it

matteradmin9PV0评论

I'm trying to override a method (my_packages) from the WP Job Manager Paid Listings plugin by extending it's class from within a custom plugin

Here is how the source code (none-related code removed) is set up...

class WC_Paid_Listings_Orders {

  private static $instance;

  public static function get_instance() {

      return null === self::$instance ? ( self::$instance = new self ) : self::$instance;
  }

  public function __construct() {

    add_action( 'woocommerce_before_my_account', array( $this, 'my_packages' ) );

  }

  public function my_packages() {

    // This is what I want to unhook

  }

}

How would I 'unhook' my_packages? This is what I have thus far (and failing with)! I'm guessing it's to do with the instance? I've tries all sorts of iterations from examples on here but to no avail - Operating at the edge of my knowledge here I'm afraid.

class cvl_override_defaults extends WC_Paid_Listings_Orders {

   public function __construct() {

    remove_action('woocommerce_before_my_account', array( $this, 'my_packages'));

    add_action( 'woocommerce_before_my_account', array( $this, 'new_my_packages' ) );

   }

   public function new_my_packages() {

     // New output goes in here

   }

}

TIA!

I'm trying to override a method (my_packages) from the WP Job Manager Paid Listings plugin by extending it's class from within a custom plugin

Here is how the source code (none-related code removed) is set up...

class WC_Paid_Listings_Orders {

  private static $instance;

  public static function get_instance() {

      return null === self::$instance ? ( self::$instance = new self ) : self::$instance;
  }

  public function __construct() {

    add_action( 'woocommerce_before_my_account', array( $this, 'my_packages' ) );

  }

  public function my_packages() {

    // This is what I want to unhook

  }

}

How would I 'unhook' my_packages? This is what I have thus far (and failing with)! I'm guessing it's to do with the instance? I've tries all sorts of iterations from examples on here but to no avail - Operating at the edge of my knowledge here I'm afraid.

class cvl_override_defaults extends WC_Paid_Listings_Orders {

   public function __construct() {

    remove_action('woocommerce_before_my_account', array( $this, 'my_packages'));

    add_action( 'woocommerce_before_my_account', array( $this, 'new_my_packages' ) );

   }

   public function new_my_packages() {

     // New output goes in here

   }

}

TIA!

Share Improve this question asked Nov 12, 2018 at 19:46 richerimagericherimage 1014 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Problem lies in this line:

remove_action('woocommerce_before_my_account', array( $this, 'my_packages'));

This won't remove action registered by parent class, because $this is a different object in this case, so you won't remove any action at all.

So how to remove such action? Since you can't access the same $this value in your class, you'll have to iterate through all filters and remove the given one manually.

Here's a good example how to do that:

https://wordpress.stackexchange/a/239431/34172

When an action is added using a specific instance of a class (when you see $this), to remove the action you need to pass the same instance of the class to remove_action().

Since WC_Paid_Listings_Orders is a Singleton (it appears), there is only one instance of the class, and you can get that instance using the get_instance() method. You can then use that instance to remove the action.

$wc_paid_listings_orders = WC_Paid_Listings_Orders::get_instance();
remove_action( $wc_paid_listings_orders, 'my_packages' );
Post a comment

comment list (0)

  1. No comments so far