Woocommerce remove an action hook - php

I am using woocommerce plugin in my wordpress site. I want to remove a function named 'woocommerce_checkout_coupon_form' which is hooked in to the action 'woocommerce_before_checkout_form'.
I tried adding the below code in my theme functions.php
add_action('init','remove_coupon_text',10);
function remove_coupon_text() {
remove_action('woocommerce_before_checkout_form','woocommerce_checkout_login_form',10);
}
But this is not working. Any idea?

Sorry it was a mistake from my side. I used the wrong function name. The function name I intended to remove was 'woocommerce_checkout_coupon_form'. The issue is fixed now.

Try this
remove_all_actions( $tag, $priority );

Try increasing the priority.
remove_action('woocommerce_before_checkout_form','woocommerce_checkout_login_form',1 - 10);
Edit: Follow the comments below and dont use this as it is, what i meant by increasing priority was to gradually increase the priority till it matches the time when function was added, when it does match you will automatically see the results else it just wont work. Thank you everyone for making it clear.

Related

Wordpress functions.php won't save using unset() PHP function

I have the unset function in my functions.php theme file and WordPress no longer allows me to save my code in the editor.
I'm using it to unset product tabs on WooCommerce as per this code (found on the WooCoomerce site - https://docs.woocommerce.com/document/editing-product-data-tabs/):
add_filter( 'woocommerce_product_tabs', 'custom_remove_product_tabs', 98 );
function custom_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
return $tabs;
}
With this code in I get the generic error of: "Something went wrong. Your change may not have been saved. Please try again. There is also a chance that you may need to manually fix and upload the file over FTP." when saving my functions.php file. If I comment out the line with the unset function is saves just fine.
It does work on the site if I save this code using FTP etc so I appreciate some may say just do that, but I like to quickly edit things inside WordPress sometimes which is no longer possible so want to understand why it's happening (and WordPress have docs on using this function themselves so it's driving me mad!). I've tried adding a standard php unset function with a generic variable to check it's not WooCommerce related but this doesn't work either.
Am I missing something? If not, is it a server setting? We're using PHP 7.4 on a litespeed server so should be up to date. I'm not sure if there is a way to see a more detailed error log from this issue so if there is any help finding it would be great.
Thanks for any help!

How can i call a plugin shortcode from within a function with theme functions.php file?

I use a plugin (Popup Builder) on my WordPress site. Whenever I create a new popup with the plugin, it creates a shortcode for that popup. I want to call on that shortcode from within the theme functions.php file. But I can't seem to get it to work properly.
The shortcode runs only if conditions are met. And in this example it's when a visitor access the site for the first time. I then check for a certain cookie and if that cookie does not exist, the popup will fire up and force the visitor to choose one option from a list of options, and then the cookie will be set with the correct value, once they do.
However I cant seem to find a solution that fires the popup at all. An I also get this notice: Notice: do_shortcode_tag was called incorrectly. Attempting to parse a shortcode without a valid callback:
function check_for_cookies() {
// Check if cookie is already set
if(isset($_COOKIE['tln_valgt_fylke'])) {
// Do this if cookie is set
$string .= 'Hi, and welcome back!' ;
return $string;
} else {
// Do this if the cookie doesn't exist
$shortcode = do_shortcode("[sg_popup id=163]");
return $shortcode;
}
}
add_action('init', 'check_for_cookies');
What am I doing wrong, and what if this is not a good way of accomplishing what I want, then what is?
This is just a guess
But, I think its a timing issue. You are using the init action to hook into another plugins shortcodes. It's possible that plugin has not yet registered it's shortcode via add_shortcode or if it has registered it, it may not have "included" the file that defines the callback for it (for whatever reason).
In fact it seems likely because:
do_shortcode_tag was called incorrectly. Attempting to parse a shortcode without a valid callback
This indicates the shortcode was called and no callback existed for it. This is not a problem with your code per say. But it says that the plugin has not yet loaded the file that contains the callback.
You could test this by hooking into an action that happens later in the execution chain, after you know all plugins have been loaded and initialized. Like even wp_head
Perhaps you could even get away with changing the priority of the hook:
add_action('init', 'check_for_cookies', 20); //default is 10
This way it's triggered at the end of init, but even then it may be too soon. The only real way to know is to look at the code for the plugin and find out when it's registering it's "stuff". An easy way to do that is add this code to the plugins shortcode callback:
try{
throw new \Exception();
}catch(\Exception $e){
die("<pre>{$e->getTraceAsString()}</pre>");
}
This will throw and then catch an exception, and then output the stacktrace. Which may show you exactly where the shortcode is being setup. You'll have to trigger the callback (obviously) for it to work. You can also use print_r(debug_backtrace()) but it's much harder to read IMO.
PS I been doing a lot of WP work lately and I just had an issue with action timing ... lol. That was why I thought of it, I spent the last 2 days refactoring code. In my case I an replacing the add/edit/profile parts of the user system on both the front and back end. It's a sort of subuser plugin. And there is a lot of competing actions related to that if you know what I mean...

Gravityforms not found message - WP Plugin hook

I'm trying to hook onto the WP Gravityforms plugin's not found message.
I've found a hook(gform_form_not_found_message) for this but it doesn't seem to be firing. What am I doing wrong here?
My code:
function notfoundmessage($message, $id){
$message = 'test';
return $message;
}
add_filter( 'gform_form_not_found_message', 'notfoundmessage', 10, 2);
I tried several thing like writing the function inline, including it from the main plugin file, including it in an oop way but none of them seem to work. I was thinking that maybe I need to require a gravityforms Class but I can't find anything about that in the documentation.
Link to the hook in the documentation
It turned out the filter isn't applied yet in the version I'm using. For other people having this issue, make sure you are using the latest version of Gravityforms.

WordPress admin and front end not opening

My site is http://www.empoweryourfamily.org/wordpress/
I was working on a plugin and wrote the following code in functions.php
add_action( 'wp_enqueue_scripts', 'table' );
function table() {
wp_register_script('table1', plugins_url() . '/Webinar_Reg/table.js', false, null, true);
if(is_admin()){
wp_enqueue_script('table1');
}
}
But, the moment I clicked on update everything went blank and now neither the frontend nor the backend is opening.
You most likely have a syntax error in functions.php or the function table() is already defined.
Try rolling back the changes you just applied.
Otherwise try looking in the server log for a specific error.
Using table as a function name is bad practice. You should name a function in such a way that it will be completely unique. Use something like mypluginname_table or mytheme_table where mypluginname is the name of your plugin and mytheme the name of your theme. And remember, function names needs to be unique, no two functions can have the same name, except if one is wrapped in a if(!function_exists()) {} conditional statement
I just replaced the modified functions.php file with the old(backup file) file and it is opening properly now. Thanks for your answers.

Wordpress function returns white screen in plugin

I am brand new at writing Wordpress plugins, so to start I am trying to create a simple one that just modifies a string. I wrote the script a while ago, and know that it functions. But to use it for Wordpress I want to apply it to the post titles. When I replaced the string with the function "get_the_title()" it returns a white screen. I stripped it down to:
function display_title() {
echo get_the_title();
}
add_action('the_title', 'display_title');
This still returns a white screen. So I figure it must be the "get_the_title()" function. Can anybody explain to me why this doesn't work, and maybe a different way to retrieve the title string?
As John says the_title is a filter rather than an action hook, although your function will be called regardless of whether you register it using add_filter or add_action.
Your problem is that with filters your function is expected to return a value (normally a modified version of the argument passed). So, to modify the title using this filter you should do something like this:
function display_title($title) {
$title .= '!'; // Do something with the title string here
return $title;
}
add_filter('the_title', 'display_title');
Well, for one thing, 'the_title' isn't an action, it's a filter. So that function is never firing. So it's not the fault of that function, it is probably something else. I would suggest reading up on the plugin api and learning the difference between actions and filters. Filters are specifically designed to do what you want in a simple way:
http://codex.wordpress.org/Plugin_API/

Categories