0

I'm building a woocommerce plugin( using the WP Plugin Boilerplate) that adds its own actions. My questions are:-

  1. When deactivating/ deleting the plugin does wordpress handle removing the actions created and added or do you have to implement this yourself?
  2. When creating and registering actions is it a good idea to do so in the construct() of a class or should I use the loader->add_action()? For E.g.

Based on the Boilerplate I have the following

private function define_admin_hooks() {

        $plugin_admin = new Wc_Shipping_Admin( $this->get_plugin_name(), $this->get_version() );

        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
        $this->loader->add_action( 'woocommerce_shipping_init', $this, 'shipping_init_method');

in the shipping_init_method

public function shipping_init_method() {
      require_once . '/admin/partials/shipping-init-methods.php';
    require_once '/includes/class-wc-shipping-create-bill.php';

      $this->_shipping_method = new Wc_Shipping_Method();
// instead of adding action when class above is created do this. Then in deactivator method remove action
            $this->loader->add_action('woocommerce_update_options_shipping_' . $this->_shipping_method->id, $this->_shipping_method->method, 'process_admin_options');
      $this->loader->add_action('woocommerce_order_status_processing', $this->_shipping_method, 'createbill', 10, 1 );

// this is a class to add to custom action which is called when method from action above is executed
            $this->_bill_method = new Create();
            $this->loader->add_action('wc_createb', $this->_bill_method, 'init', 1, 2 );
  }
  1. with relation to question 2....is it wise to do so if I need to have the actions registered and removed on each instantiated instance to deal with each unique case.? For .E.g.

I only need the following

// this is a class to add to custom action which is called when method from action above is executed
            $this->_bill_method = new Create();
            $this->loader->add_action('wc_createb', $this->_bill_method, 'init', 1, 2 );
  }

When $this->loader->add_action('woocommerce_order_status_processing', $this->_shipping_method, 'createbill', 10, 1 ); is called. Should I instantiate the class and add the action inside the createbill method or like how I did above when initialising the plugin?

  • for the 1. : action and filter are added at every page load then if the plugin is destactivated, hooks are no more executed and you don't need to remove them. – mmm Nov 9 '17 at 17:26
  • 1
    for the 2. : can you edit your question to show a example ? – mmm Nov 9 '17 at 17:27
  • @mmm Thanks. Hope this edits help. Though based on your first answer to question one maybe I should add the add_action on class instantiation – Kendall Nov 9 '17 at 19:04
  • @mmm its a matter of "best practices" – Kendall Nov 9 '17 at 19:07
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.