remove_action() not working in WordPress plugin - wordpress

I'm new to writing WordPress plugins. I'm trying to write a little plugin that modifies how the woocommerce plugin displays images on the single product page. Specifically, if there is no product image, make the div holding the image "display:none" rather than displaying a placeholder image there. The strategy I'm using is to use add_action to render my own version of woocommerce's product_image.php template and then (trying to) use remove_action to prevent the original product_image.php file from being rendered. The add_action is clearly working, as I can see the "display:none" div in Firebug. However, the remove_action isn't succeeding.
Here is my code:
$add_result = add_action( 'woocommerce_before_single_product_summary', 'eba_wc_show_product_images', 10);
function eba_wc_show_product_images() {
include( 'eba_product-image.php' );
}
$remove_result = remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 30);
echo "<hr/>result of add_result = " . $add_result . "<hr/>";
echo "<hr/>result of remove_result = " . $remove_result . "<hr/>";
The priority on the original add_action for the woocommerce_before_single_product_summary hook was 20, so I made the priority on the remove_action 30.
The two debugging statements at the end show that the add_action is returning "1", but the result of the remove_action is empty. Any help would be greatly appreciated.

I've stumbled on this question several times now and I wish somebody would've just told me that I can do this:
$priority = has_action('action_name', 'function_name');
remove_action('action_name', 'function_name', $priority);
This saves me from actually hardcoding the priority, which may be different for different environments.

Try removing the action during plugins_loaded, this should ensure that its definitely been added before you try and remove it.
add_action('plugins_loaded','alter_woo_hooks');
function alter_woo_hooks() {
$add_result = add_action( 'woocommerce_before_single_product_summary', 'eba_wc_show_product_images', 10);
$remove_result = remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 10);
echo "<hr/>result of add_result = " . $add_result . "<hr/>";
echo "<hr/>result of remove_result = " . $remove_result . "<hr/>";
}
function eba_wc_show_product_images() {
include( 'eba_product-image.php' );
}

one other way that worked for me is that we fire exact hook with earlier priority and remove other hook with later priority
add_action('action_name','alter_some_hook',0);
function alter_some_hook() {
$priority = has_action('action_name', 'function_name');
remove_action('action_name', 'function_name', $priority);
}

You will have to understand the code execution sequence. You Must make sure that remove_action is getting called after the add_action has been executed and the action is actually in the queue. If you remove first and then add, it will obviously not work and doesn't make sense as well.

Related

Wordpress : add_action not working in plugin or functions.php

I've got two functions:
add_action( 'woocommerce_order_status_completed' , 'after_order_complete' , 10);
function after_order_complete( $order_id )
{
error_log("woocommerce_order_status_completed", 0);
}
add_action( 'wp_login', 'after_login', 10, 2);
function after_login( $user_login, $user ) {
error_log("wp_login", 0);
}
that I can't seem to get called, tried in a plugin and functions.php but they never fire and no errors are logged.
How is it I debug this to find out why they fail or does anyone know how to get these to work?
In the same code I have another add_action but this time using user_register and that works as expected from plugin or functions.php
Edit:
I have the wp_login figured out, action does get called but only when first visiting the back end, site is front end only for users so used membership system hook instead.

remove_action fails on woocommerce_template_loop_product_thumbnail

I wrote a simple plugin that performs some logic on thumbnails in WooCommerce. This plugin worked great for about a year, until the client switched to a new theme. Now the plugin is no longer working and I've narrowed the problem to remove_action() failing.
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
if (! remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10) ) {
echo 'FAILED to remove action<br/>';
}
/**
* WooCommerce Loop Product Thumbs
**/
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
}
}
I don't know if this is because of the order plugins are loaded, or if I'm making the call incorrectly due to a change in WooCommerce. I've read some topics that indicate the remove_action() should follow my custom add_action() so I reversed the order accordingly. It doesn't work either way - remove_action() is always returning FALSE.
I've been banging my head against a wall all day trying to figure this out. Can anyone give me a clue on a sure-fire way to execute the remove, or a way to debug it?
Thanks.
Try changing the priority flag of [add/remove]_action() to something higher, like 90.
It's a little hard to guess without seeing what the theme is actually doing!
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 90);
if (! remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 90) ) {
echo 'FAILED to remove action<br/>';
}

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Disable caching in WordPress Feeds generation

i'm working on a plugin that adds another parameter to feeds, i want to add a numberOfItems on the url and the feed returns that number of articles. I dont want to use the builtin option from WP Admin because the feed will be added on other websites with different number of items, it's a little bit complicated, the point is i need this implementation. i've added something like
function _my_custom_option( $option )
{
global $wp_query, $wp_rewrite;;
remove_filter( 'pre_option_posts_per_rss', '_my_custom_option' );
//$number = get_query_var('number');
$number = $wp_query->query_vars['numberOfItems'];
if(isset($number))
$option = $number;
else
$option = 10;
//$wp_rewrite->flush_rules();
add_filter( 'pre_option_posts_per_rss', '_my_custom_option' );
return $option;
}
add_filter( 'pre_option_posts_per_rss', '_my_custom_option' );
It seems that the feed is cached somehow and doesnt generate with the number of items, because when i add paged=2, it works. But if i change on page=2 numberOfItems with another number, it doesnt change. Plus, i added some junk text in wp-includes >feed-rss2.php just to verify if it cached or not. And it doesnt show after the first 2-3 page refreshes.
To be honest, i'm stuck, i don't know how to approach this, i've looked on the wordpress code and i dont see where the caching is done.
What about this?
function do_not_cache_feeds(&$feed) {
$feed->enable_cache(false);
}
add_action( 'wp_feed_options', 'do_not_cache_feeds' );

Wordpress remove shortcode from content

Is it possible to remove the gallery shortcode from the content before when the_content() is executed? I searched codex and found remove_shortcode( $tag ) but they show no examples.
I tried adding to functions
function remove_gallery($content) {
$content .= remove_shortcode('[gallery]');
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
It doesnt work..
Update:
I was able to unregister the shortcode using the code below, but it also removes the content
function remove_gallery($content) {
return remove_shortcode('gallery', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);
I know this is a relative old question, but the strip_shortcodes function does work!
global $post;
echo strip_shortcodes($post->post_content);
Easiest way if you ask me..
Strange. remove_shortcode (codex link) doesn't take a second argument.
You're returning either the true or false return of the remove_shortcode function, not the content with the shortcode removed.
Try either something like this in that second version of your function up there:
remove_shortcode('gallery');
return $content;
Or just put
remove_shortcode('gallery');
In your functions.php file. The previous poster suggested including the [ ]'s, which I guess is wrong.
I think you should use sub string replacement like this:
function remove_gallery($content) {
return str_replace('[gallery]', '', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);
Bear in mind, this method does not come with good performance.
update: You can unregister the shotcode in function.php by adding code:
remove_shortcode('[gallery]');
An old question, but after some digging and a combination of answers this worked for me:
<?php $content = get_the_content();
echo strip_shortcodes($content);?>
I was only inserting galleries, which I wanted to remove and display separately. Obviously if you want to remove a specific shortcode this might not be the solution for you.

Resources