How to safely remove an action hook in child-theme? - wordpress

I am working on a WordPress installation called PointFinder. Items that are posted by users get deleted after some pre-defined period. My goal is to disable this behaviour. I already found the corresponding code lines and deactivated the add_action hook that triggers the periodically called function pointfinder_clean_pending_orders() in schedule-config.php, which works fine for the moment.
add_action( 'pointfinder_schedule_hooks_hourly', 'pointfinder_clean_pending_orders' ); // <--- commented out this line
function pointfinder_clean_pending_orders() {
/* code that cleans-up ... */
}
How can I achieve the deactivation in my child-theme ? If I simply add a remove_action to functions.php in my child-theme, will that work? I am not sure what will be called first, add_action in schedule-config.php or my remove_action in functions.php ?
I only have access to the productive server and no testing environment and thus I am a bit reluctant regarding experiments.

try this:
add_action('init','remove_hourly_hook');
function remove_hourly_hook() {
remove_action( 'pointfinder_schedule_hooks_hourly', 'pointfinder_clean_pending_orders' );
}
What this does is after WordPress is initialized (regardless of the order of functions.php being called), it will then remove the action.
Now, if the initial add_action is inside a its own hook, you will want to make sure your hook 'init' is changed to occur afterwards.

Related

remove_action is not working for me to remove a theme's action..?

I'm running WordPress + WooCommerce + Storefront. I'm trying to remove the "Built with Storefront & WooCommerce" credit in the footer.
I see lots of articles explaining how to do that by using remove_action as follows:
remove_action('storefront_footer', 'storefront_credit',20);
This is not working, though. I've tried it through a Snippets plugin, and I've tried it through the functions.php in my child theme.
I've also verified that nothing seems to have changed in the theme code since these articles were written, so it has the same function and hook names.
I'm not sure what I'm missing here..?? Any information on this would be greatly appreciated. Thanks!
In order to remove the action you must be sure to remove it before it's added. This is a parent theme's hook, so if you remove_action from the child theme or via code Snippets, it's already too late.
Unless... you make the remove_action trigger earlier, by hooking that to "wp" WordPress action.
Try with:
add_action( 'wp', 'bbloomer_remove_storefront_actions' );
function bbloomer_remove_storefront_actions() {
remove_action( 'storefront_footer', 'storefront_credit', 20 );
}
Another cool thing you can do to absolutely make sure you can remove an action is by using the same hook, but with a lower priority:
add_action( 'storefront_footer', 'bbloomer_remove_storefront_actions', 19 );
function bbloomer_remove_storefront_actions() {
remove_action( 'storefront_footer', 'storefront_credit', 20 );
}
In this example, I'm removing 'storefront_credit' which has priority = 20 by hooking into the same hook, with priority 19, so that I remove it before it's added.

storefront_sticky_add_to_cart refuses to be removed

Almost finished my WooCommerce Storefront child theme.
There's a block sitting under the footer on the product pages that I have zero desire to keep. Just as I've done with a few other extraneous pieces of markup, I tracked down the action and removed it:
remove_action( 'storefront_after_footer', 'storefront_sticky_single_add_to_cart');
... It is still showing in the page.
I've even tried some random things I found:
add_filter ('storefront_sticky_add_to_cart', '__return_false');
and
function cleanup_parent_filters()
{
//...
remove_action( 'storefront_after_footer', 'storefront_sticky_single_add_to_cart');
}
add_action( 'wp_loaded', 'cleanup_parent_filters');
With no results.
This is really more annoying than anything. I can remove the entire do_action( 'storefront_after_footer' ); from the template as I really just don't care to have it there, but now I need to know why this is not working as expected out of principle.
try
remove_action( 'storefront_after_footer',
'storefront_sticky_single_add_to_cart', 999 );
remove_action needs the priority value as it was used to add the action. From the Docs
Important: To remove a hook, the $function_to_remove and $priority
arguments must match when the hook was added. This goes for both
filters and actions. No warning will be given on removal failure.
And why 999? From the source code.
Disclaimer: I haven't tested this out.

Removing action added by a plugin in Wordpress

I am using Popup Maker plugin for Wordpress and I am trying to prevent it to load on a particular page using the functions.php file of a child theme.
I've located in the plugin's directory the file popup-maker.php which contains the following line at the end:
add_action( 'plugins_loaded', 'popmake_initialize', 0 );
If I remove / comment this line, the popup will not fire, so I guess this is the action I need to remove. I have read the WP codex and numerous posts but still can't get it to work.
Right now I am stuck at this (function I have added in my child theme's functions.php file):
function remove_popmaker() {
remove_action('plugins_loaded', 'popmake_initialize', 0);
}
add_action( 'init', 'remove_popmaker', 1 );
PS: I am using shopkeeper theme and Woocommerce.
All help appreciated, thanks.
you are adding a action to init which is after plugins_loaded so you cannot remove a action after it has run.
you can try the same action but you will have to do this from a plugin
remove_action( 'plugins_loaded', 'remove_popmaker', 0 );
But i suspect actions added before yours will be run after, this may be unpredictable if not you may have to code a MUplugin (google this).
There is a way I managed to do this:
add_action('wp', 'disableEmailPopup');
function disableEmailPopup () {
global $post;
if ($post->ID === 11625249) return remove_action( 'wp_enqueue_scripts', 'popmake_load_site_scripts');
}
#BMM - Without knowing the use case as to why you need to disable the entire plugin on one page its hard to give you the best answer.
For example if you simply wanted a single popup to not show on one page you can use the conditions panel to add a negative condition using the (!) button. Click it to turn it red and you will check for the opposite of a condition, so (!) Page Selected: Page xyz would prevent it from loading on that page.
Alternatively you can create your own custom conditions allowing you to add that condition to any popup.
Lastly if you wanted to unhook it just from the front end you can simply remove the rendering & script handlers
remove_action( 'wp_footer', 'popmake_render_popups', 1 );
remove_action( 'wp_head', 'popmake_script_loading_enabled' );
And if you want to prevent the queries as well
remove_action( 'wp_enqueue_scripts', 'popmake_preload_popups', 11 );
Hope that helps.

wordpress: can't get do_action to work with custom function

I'm using woocommerce and on functions.php i've hooked my function to an action.
add_action('my_calculate_shipping','calculate_shipping_2');
function calculate_shipping_2() {
echo "test";
}
Now on cart-shipping.php i use do_action('my_calculate_shipping'); and test isn't displayed. I've used function_exists and it returns false.
How do i solve this?
The reason is plugins are loaded before theme,
Ref: WordPress Code Flow
You can wrap your add_action code in some hook which is called in upper level.

WordPress Thesis theme - hook below post

i need a little help with thesis theme, i made a simple hook for single post with custom content and i want the hook to be right after the post but after the post there's a plugins like facebook etc... and my hook is below that. How to make my hook to be right after post without disabling this plugins ?
*i already tried every posible combinations thesis_hook_after_post, thesis_hook_after_post_box etc and it dont work
Thanks in advance !
Change priority value in add_action
add_action( $tag, $function_to_add, $priority, $accepted_args );
(int) (optional) Used to specify the order in which the functions
associated with a particular action are executed. Lower numbers
correspond with earlier execution, and functions with the same
priority are executed in the order in which they were added to the
action. Default: 10
Edit:-
function post_article111() {
if (is_single( )) {
echo "content goes here..";
}
}
add_action('thesis_hook_after_post_box', 'post_article111','5');

Resources