Robert Granlund
asked on
Remove an action from a Class Function
Wordpress remove a function from a class.
I want to remove a function from a class and in turn add a function. The adding the function works 100% but what I want to remove does not.
What I want to remove from the following function is:
// add custom fields to checkout page
add_action( 'woocommerce_after_order_n otes', array( $this, 'woo_os_checkout_fields' ) );
Here is the original Class:
Here is my code.
otes', array( $this, 'woo_os_checkout_fields' ) );
I want to remove a function from a class and in turn add a function. The adding the function works 100% but what I want to remove does not.
What I want to remove from the following function is:
// add custom fields to checkout page
add_action( 'woocommerce_after_order_n
Here is the original Class:
<?php
public function add_hooks() {
class Woo_Os_Public {
// checkout process
add_action( 'woocommerce_checkout_process', array( $this, 'woo_os_checkout_field_process' ) );
// add custom fields to checkout page
add_action( 'woocommerce_after_order_notes', array( $this, 'woo_os_checkout_fields' ) );
// save checkout custom meta
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'woo_os_checkout_field_update_order_meta' ), 10, 1 );
// add action to add an extra detail on order confirmation page
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'woo_os_woocommerce_order_details_after_order_table' ),10, 1 );
//add action to add custom detail in woocommerce email for customer and admin.
add_action( 'woocommerce_email_after_order_table',array( $this, 'woo_os_woocommerce_email_after_order_table' ), 10, 4 );
// display Agreement PDF ( V 1.0.4 )
add_action( 'init', array( $this, 'woo_os_agreement_pdf_view' ), 100 );
}
}
Here is my code.
$woo_os_public = new Woo_Os_Public();
// This filter adjusts where on the page the sign pad appears
class Wizard_class extends Woo_Os_Public {
public function add_hooks() {
remove_action( 'woocommerce_after_order_notes', array( $this, 'woo_os_checkout_fields' ) );
// add custom fields to checkout page
add_action('woocommerce_checkout_after_customer_details', array($this, 'woo_os_checkout_fields'));
}
}
$wizard_public = new Wizard_class;
$wizard_public->add_hooks();
What I want to remove from the original Class/method is : remove_action( 'woocommerce_after_order_n
If you're just trying to clear all filters that match that specific tag (maybe because you know there's only one and you don't want to pass around an object instance), you can also use remove_all_filters():
https://codex.wordpress.org/Function_Reference/remove_all_filters
https://codex.wordpress.org/Function_Reference/remove_all_filters
remove_all_filters('woocommerce_after_order_notes');
ASKER
Thank you for your response, that helps me clarify my question:
I want to add "woo_os_checkout_fields" to "woocommerce_checkout_afte r_customer _details" not to "woocommerce_after_order_n otes".
So I guess what I am asking is How do I first remove it from the original plugin and then add the new action with my plugin?
I can't clear them all because there are others that are hooking to 'woocommerce_after_order_n otes'
I want to add "woo_os_checkout_fields" to "woocommerce_checkout_afte
So I guess what I am asking is How do I first remove it from the original plugin and then add the new action with my plugin?
I can't clear them all because there are others that are hooking to 'woocommerce_after_order_n
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm not sure I follow "If you don't have a reference to the original class instance," The function is in the class Woo_Os_Public { Class. I left out that code because the page is really long. The function woo_os_checkout_fields basically builds a form field. Is that what you are asking? What Class is "woo_os_checkout_fields" in?
Oh wait - I just saw that you have this line:
$woo_os_public = new Woo_Os_Public();
...which might be the original class instance.
So to understand this, let's say you have these two classes:
Then you actually use those two classes:
You have two different class instances here, $woo_os_public and $wizard_public.
In $wizard_public's class, you're trying to remove a previously-added action using the $this variable:
remove_action( 'action_xyz', array( $this, 'handle_xyz' ) );
However, in your Wizard class, $this refers back to $wizard_public, and $wizard_public wasn't the class instance that added the action, so the removal isn't going to work, because you're trying to remove something that doesn't exist. Rather, the class instance that added the action was $woo_os_public. So for that remove_action to work properly, you have to pass $woo_os_public to the remove_action call (and you have to use global to bring the variable into the current scope):
global $woo_os_public;
remove_action( 'action_xyz', array( $woo_os_public, 'handle_xyz' ) );
Now, what I was saying before was that I didn't see the line of code where you had actually created the instance of Woo_Os_Public:
$woo_os_public = new Woo_Os_Public();
...so I didn't know if your code would be able to access that variable or not. But since you DO have it, then I'm assuming that's the correct name and just fixing the remove_action statement as I showed above will likely work.
$woo_os_public = new Woo_Os_Public();
...which might be the original class instance.
So to understand this, let's say you have these two classes:
class Woo_Os_Public
{
public function add_hooks()
{
add_action( 'action_xyz', array( $this, 'handle_xyz' ) );
}
public function handle_xyz()
{
...some code here...
}
}
class Wizard_class extends Woo_Os_Public
{
public function add_hooks()
{
remove_action( 'action_xyz', array( $this, 'handle_xyz' ) );
}
}
Then you actually use those two classes:
$woo_os_public = new Woo_Os_Public();
$woo_os_public->add_hooks();
$wizard_public = new Wizard_class();
$wizard_public->add_hooks();
You have two different class instances here, $woo_os_public and $wizard_public.
In $wizard_public's class, you're trying to remove a previously-added action using the $this variable:
remove_action( 'action_xyz', array( $this, 'handle_xyz' ) );
However, in your Wizard class, $this refers back to $wizard_public, and $wizard_public wasn't the class instance that added the action, so the removal isn't going to work, because you're trying to remove something that doesn't exist. Rather, the class instance that added the action was $woo_os_public. So for that remove_action to work properly, you have to pass $woo_os_public to the remove_action call (and you have to use global to bring the variable into the current scope):
global $woo_os_public;
remove_action( 'action_xyz', array( $woo_os_public, 'handle_xyz' ) );
Now, what I was saying before was that I didn't see the line of code where you had actually created the instance of Woo_Os_Public:
$woo_os_public = new Woo_Os_Public();
...so I didn't know if your code would be able to access that variable or not. But since you DO have it, then I'm assuming that's the correct name and just fixing the remove_action statement as I showed above will likely work.
ASKER
This code does not remove "add_action( 'woocommerce_after_order_n otes', array( $this, 'woo_os_checkout_fields' ) );"
Am I missing something simple? It does not remove it. However, it does add it before my code runs.
<?php
$woo_os_public = new Woo_Os_Public();
$woo_os_public->add_hooks();
// This filter adjusts where on the page the sign pad appears
class Wizard_class extends Woo_Os_Public {
public function add_hooks() {
global $woo_os_public;
remove_action( 'woocommerce_after_order_notes', array( $woo_os_public, 'woo_os_checkout_fields' ) );
// add custom fields to checkout page
add_action('woocommerce_checkout_after_customer_details', array($this, 'woo_os_checkout_fields'));
}
}
$wizard_public = new Wizard_class;
$wizard_public->add_hooks();
Am I missing something simple? It does not remove it. However, it does add it before my code runs.
Did you try the previous code mentioned in #42526922?
ASKER
I tried the following but it adds it before
$woo_os_public = new Woo_Os_Public();
$woo_os_public->add_hooks();
global $wp_filter ;
if (isset($wp_filter[$tag]))
{
// Find all callbacks for the given filter/tag
foreach ($wp_filter[$tag] as $filterIDX => $callbacks)
{
foreach ($callbacks as $callback_name => $callback_definition)
{
// Check to see if it's callback matches our method name
if (strpos($callback_name, $class_method) !== false)
{
// It does, remove it from the filters list
unset($wp_filter[$tag][$filterIDX][$callback_name]) ;
}
}
}
}
// This filter adjusts where on the page the sign pad appears
class Wizard_class extends Woo_Os_Public {
public function add_hooks() {
global $woo_os_public;
remove_action( 'woocommerce_after_order_notes', array( $woo_os_public, 'woo_os_checkout_fields' ) );
// add custom fields to checkout page
add_action('woocommerce_checkout_after_customer_details', array($this, 'woo_os_checkout_fields'));
}
}
$wizard_public = new Wizard_class;
$wizard_public->add_hooks();
What do you mean "but it adds it before" ?
ASKER
The Class adds the the form field where it would normally add it and then adds mine where I want it.
After the $wizard_public->add_hooks( ), can you log the contents of $wp_filter to a file and paste it here?
file_put_contents("wp_filt er.log",pr int_r($wp_ filter,tru e));
file_put_contents("wp_filt
1. You've got the wrong action / hook name.
2. You're calling remove too early - the action might not have been added yet.
3. You have a bad reference - e.g. you're passing array($this, "woo_os_checkout_fields") as the callback, but $this refers to the extended "Wizard_class" instance that you've just created, so if there's some other class instance that has the added action, then you need to remove THAT instance's callback, not $this one.