I'm using a plugin with this action:
add_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ), 10, 2 );
I've written a plugin to do some tweaking to my site here and there. Mostly REST API customizations.
I'd like to override, subvert, ignore, remove, that filter. Right now I've got it "working" by just returning on the first line of the pre_save_review function.
I tried:
remove_action( 'pre_comment_approved', 'pre_save_review');
... but I wonder if there's some namespacing issue. I don't know a lot about PHP so I don't know how to refer to classes in other files/plugins, which I imagine is the issue.
Thanks!
You can remove all filters with a wordpress function "remove_all_filters"
remove_all_filters('pre_comment_approved');
This also has a second parameter to pass the priority number, since this one is added with priority 10 it would be this:
remove_all_filters('pre_comment_approved', 10);
Reference remove_all_filters
You'll want to use the remove_filter function for this.
See the Example in the documentation here: https://codex.wordpress.org/Function_Reference/remove_filter
If a filter has been added from within a class, for example by a
plugin, removing it will require accessing the class variable.
global $my_class;
remove_filter( 'the_content', array($my_class, 'class_filter_function') );
So maybe give the following a try;
global $this;
remove_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ) );
Related
There is not a specific answer for this exact issue so i am going to try this.
So in the parent theme of a wordpress website there is a php file named helpers-icons.php. The exact path for this file is /wp-content/themes/parent/inc/helpers/helpers-icons.php, and the content of that file is
function get_flatsome_icon($name, $size = null) {
if($size) $size = 'style="font-size:'.$size.';"';
return '<i class="'.$name.'" '.$size.'></i>';
}
This file is then inlcuded in functions.php of that parent theme.
Now i want to override a function inside and being more specific, just this line of code
return '<i class="'.$name.'" '.$size.'></i>'; to return '<span class="'.$name.'" '.$size.'></span>';
how could i do that on child theme without messing with the php files of parent?
Thanks in advance
EDIT
EDIT 2
EDIT - Different Approach
You can copy the file /inc/helpers/helpers-icons.php into your child theme (keep the folder structure - e.g. /child-theme/inc/helpers/helpers-icons.php). That way it will be called instead of the original file and you can change the function there.
(Original Answer) Removing Functions From Hooks:
You’ll need to de-hook the parent function and hook your child theme function instead by using remove_action() and remove_filter().
The one you use will depend on whether the function is attached to an action hook or filter hook in the parent theme.
To remove a function, use:
remove_action( 'init', 'parent_function' );
However, this won’t work on its own, you will need to attach this function to a hook which will fire after the hook which the parent theme function is attached to. This is because you can’t remove the action before it’s been fired.
function child_remove_parent_function() {
remove_action( 'init', 'parent_function', [priority level] );
}
add_action( 'wp_loaded', 'child_remove_parent_function' );
NOTE: If the parent_function() was loaded using a priority level, you have to unloaded by stating the same level as well. That’s why [priority level] is in square brackets.
As you may imagine, wp-loaded is a hook that comes after init, so remove_action() can really remove the parent_function() that was loaded to init.
Now you’re free to write whatever new function you’d like.
function child_function() {
// your function
}
add_action( 'init', 'child_function' );
Source and further reading: https://obsessive-coffee-disorder.com/how-to-override-parent-theme-functions-in-wordpress/
I've just started learning Wordpress and going through standard/default themes. If i have understood filters idea correctly, before we can apply them we need to add callback functions via add_filter($hook, $callback, $args). However looking at the 'twentyseventeen' theme i can't see those declaration for twentyseventeen_starter_content and it is used then with: $starter_content = apply_filters( 'twentyseventeen_starter_content', $starter_content ); (file functions.php) and twentyseventeen_front_page_sections - $num_sections = apply_filters( 'twentyseventeen_front_page_sections', 4 ); (file front-page.php). What am i missing and how does it work without setting callback functions?
apply_filters runs all the callbacks attached to it by add_filter to the same hook/tag. If there are no callbacks attached to that hook/tag it returns the second parameter (which is the value beng filtered) of the apply_filters. Therefore apply_filters( 'twentyseventeen_front_page_sections', 4 ); will return 4 if there are no add_filter('twentyseventeen_front_page_sections', 'callbackfunc');. Else it will return the result of the add_filter callback with the highest priority after going through all callbacks.
Priorities are set in the add_filter as the third parameter.
I don't know if this what you were looking for but i thought it might give you a better understanding.
In WooCommerce, there is a public static class WC_Emails, defined in /woocommerce/includes/class-wc-emails.php.
Within that class, there is a reference to a hook called woocommerce_email_customer_details. To that hook, an action called email_addresses is assigned, which is simply a PHP file that generates some HTML for billing and shipping addresses to be added to email notifications.
add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 );
I am trying to remove that action and can't seem to figure out how to do it.
Here's an example bit of code in my child theme's functions.php that does not work:
add_action( 'init', 'remove_default_addresses');
function remove_default_addresses() {
remove_action( 'woocommerce_email_customer_details',
array( 'WC_Emails', 'email_addresses' ), 20);
}
Instead of the init hook there, I have tried wp-head. Since WC_Emails is a static function, the above code is the way the WordPress Codex suggests, as far as using an array() above, instead of just the action name. Regardless, this doesn't work, either (with just the action name):
add_action( 'init', 'remove_default_addresses');
function remove_default_addresses() {
remove_action( 'woocommerce_email_customer_details', 'email_addresses', 20);
}
20 is the priority on the add_action Woo does, and I also understand that a remove_action() must have the same priority as what the original add_action() had.
No matter what I try, the billing and shipping addresses still appear on the email notifications.
Once that gets working, it will be great! However, there is an additional requirement: This needs to happen only inside code I have for the woocommerce_order_status_pending_to_processing_notification hook in Woo.
In other words, I have other code that fires and generates an email when an order status changes from Pending to Processing (and I have tried my code above with all its variations within that hook as well). I need the billing and shipping addresses to NOT show only in the custom email that is generated.
Any ideas? Thanks so much. :)
Here is the code:
add_action( 'woocommerce_email', function ( $email_class ) {
remove_action( 'woocommerce_email_customer_details', array( $email_class, 'email_addresses' ), 20, 3 );
});
Source
I've been learning WP plugin development and the tutorial that I was looking at said that Wordpress has two hooks, one being add_action and the other being add_filter. Then i searched within the core to learn more about it and i noticed that the add_action function returned an add_filter function. What is the point of using add_action if i can just use add_filter? Can someone explain this?
I found it here: /wp-includes/plugin.php
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1)
{
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
I had an action using admin_init to play with wordpress, i changed it to use add_filter instead and it worked without any problems. Isnt add_filter only used for string outputs while add_action uses hooks added throughout the wp core?
I'm trying to hook into the WooCommerce function that creates orders create_order or wc_create_order - and pull some of the variables from it. I've hooked into functions before, but they've never been this complicated. The functions are inside the WC_Checkout Class - and I can't seem to find the right tool to get over that wall.
The code inside the function is irrelevant at this point in time. I just want the function to fire. I've tried the basic add_action function.
add_action( 'create_order', 'my_function', 1, 1 );
I also tried a few more options that were a little complicated, but the resources for this were a little sketch. I'm pretty sure it's more complicated than this too:
function init() {
add_action('create_order', array(&$this, 'my_function'), 1, 1 );
}
I've seen some posts including __construct() but they weren't specific enough to work.