How to remove actions from Yoast SEO plugin - wordpress

I need to remove the actions that Yoast SEO has added. This is my code:
function remove_actions() {
// deregister all not more required tags
remove_action( 'wp_head', '_wp_render_title_tag', 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'test123' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'front_page_specific_init' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metadesc' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'robots' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metakeywords' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'canonical' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'adjacent_rel_links' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'publisher' ), 50 );
}
add_action( 'wp_head', 'remove_actions', 1000 );
This code does not remove the actions. What is wrong? How can I remove the actions successfully?

Consider these notes from the remove_action Documentation:
You may need to prioritize the removal of the action to a hook that occurs after the action is added.
You cannot successfully remove the action before it has been added.
You also cannot remove an action after it has been run.
To remove an action the priority must match the priority with with the function was originally added.
In your case, I believe several of these issues (especially #3 and #4) are causing problems:
First, the priority on your add_action is too high. By setting it this high, it's running after all of the Yoast wp_head actions are run. Instead, hook into the same action you want to remove, but with a very low number, such as -99999, to cause it to run before the Yoast actions are run. (Further, I've broken into two functions, just to be sure they are run at the correct time - one for each action - wp_head and wpseo_head).
Second, your priorities do not match the priorities in the Yoast code. I've dug through all the Yoast code to find all of these actions and documented / corrected in the code below - and I can tell you for example the metakeywords hook in Yoast code is 11, so your remove_action (with priority 40) will not work.
Finally, Yoast adds these actions to $this (an instantiated version of the WPSEO_Frontend class), not static version of the class methods. This means that remove_action is not able to find them based on the function array(WPSEO_Frontend, head), for example. Instead, you need to load the instantiated version of Yoast, and pass that in to the remove_action functions.
Documented code below:
// Remove ONLY the head actions. Permits calling this at a "safe" time
function remove_head_actions() {
// not Yoast, but WP default. Priority is 1
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
// If the plugin isn't installed, don't run this!
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the WPSEO_Frontend instantiated class
$yoast = WPSEO_Frontend::get_instance();
// removed your "test" action - no need
// per Yoast code, this is priority 0
remove_action( 'wp_head', array( $yoast, 'front_page_specific_init' ), 0 );
// per Yoast code, this is priority 1
remove_action( 'wp_head', array( $yoast, 'head' ), 1 );
}
function remove_wpseo_head_actions() {
// If the Yoast plugin isn't installed, don't run this
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the Yoast instantiated class
$yoast = WPSEO_Frontend::get_instance();
remove_action( 'wpseo_head', array( $yoast, 'head' ), 50 );
// per Yoast code, this is priority 6
remove_action( 'wpseo_head', array( $yoast, 'metadesc' ), 6 );
// per Yoast code, this is priority 10
remove_action( 'wpseo_head', array( $yoast, 'robots' ), 10 );
// per Yoast code, this is priority 11
remove_action( 'wpseo_head', array( $yoast, 'metakeywords' ), 11 );
// per Yoast code, this is priority 20
remove_action( 'wpseo_head', array( $yoast, 'canonical' ), 20 );
// per Yoast code, this is priority 21
remove_action( 'wpseo_head', array( $yoast, 'adjacent_rel_links' ), 21 );
// per Yoast code, this is priority 22
remove_action( 'wpseo_head', array( $yoast, 'publisher' ), 22 );
}
Final Notes:.
Remove the WPSEO_Frontend::head action is very heavy handed. This will yank a whole host of other things you probably don't want removed.
Second, It's probably better to modify the output of these actions, rather than removing them completely.
For example,
add_action('wpseo_metakeywords', 'your_metakeywords_function');
function your_metakeywords_function( $keywords ) {
// modify the keywords as desired
return $keywords;
}

Many of these actions have filters and output can be removed by returning false.
// Removes 'meta name="description"' tag from output
add_filter( 'wpseo_metadesc', 'my_custom_metadesc' );
function my_custom_metadesc() {
return false;
}
In some cases like WPSEO_Opengraph there's a filter pattern: wpseo_og_ + property name with underscores instead of colons.
// Filters '<meta property="article:tag" content="Foo" />'
add_filter( 'wpseo_og_article_tag', 'my_custom_article_tag' );

Related

Move variations under product thumbnail

I'm trying to move the variations under my thumbnails but i'm not sure where to start.. Should i use a hook or a template ?
Here's what i'm trying to do :
Feel free to give me ideas or even a solution if you ever encountered this issue !
You can use woocommerce_product_thumbnails to add your variation under the thumbnail. you need to remove default woocommerce_variable_add_to_cart action hook. try the below code. code will go in your functions.php file.
function move_variation_under_thumbnail(){
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
global $product;
// Enqueue variation scripts.
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Get Available variations?
$get_variations = count( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );
// Load the template.
wc_get_template(
'single-product/add-to-cart/variable.php',
array(
'available_variations' => $get_variations ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes(),
)
);
}
add_action( 'woocommerce_product_thumbnails', 'move_variation_under_thumbnail', 10 );
Or you can do below as Martin Mirchev suggested.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_product_thumbnails', 'woocommerce_template_single_add_to_cart', 30 );ommerce_product_thumbnails', 'woocommerce_template_single_add_to_cart', 30 );
Tested and works

Woocommerce - remove scheduled email

I have a plugin which help me to book events. I have integrated a mail scheduler for reminder emails & feedback emails.
On confirmed booking emails are scheduled for reminder as well as feedback. I have both options to define it globally as well as individually to each event. This class is adding actions for the scheduled emails.
class WooEvent_Reminder_Email {
public function __construct()
{
add_filter( 'exc_mb_meta_boxes', array($this,'email_reminder_metadata') );
// Schedule the event when the order is completed.
add_action( 'woocommerce_order_status_completed', array( $this, 'remind_email' ), 10, 1 );
add_action( 'woocommerce_order_status_completed', array( $this, 'remind_email_fb' ), 11, 1 );
// Trigger the email.
add_action( 'wooevent_email_reminder', array( &$this, 'send_mail' ),10,4 );
add_action( 'wooevent_email_reminder_2', array( $this, 'send_mail' ),10,4 );
add_action( 'wooevent_email_reminder_3', array( $this, 'send_mail' ),10,4 );
add_action( 'wooevent_email_reminder_feedback', array( $this, 'send_mail_fb' ),10,3);
// send email to attendee
add_action( 'we_send_email_reminder', array( &$this, 'send_mail_attendee' ),10,1 );
}
Now, due to some reasons the event is not happening and I have created one more category for products "event-deactivated". If this category, I want to disable the event as well as want to remove all further email actions (i.e. no more notifications to the customer). For this objective, I am running this code:
add_filter('woocommerce_is_purchasable', 'nms3_catalog_mode_on_for_product', 10, 2 );
function nms3_catalog_mode_on_for_product( $is_purchasable, $product ) {
global $WooEvent_Reminder_Email;
if( has_term( 'seminar-deaktiviert', 'product_cat', $product->get_id() ) ) {
// you can set multiple conditions here
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// Trigger the email.
remove_action( 'woocommerce_order_status_completed', array( $WooEvent_Reminder_Email, 'remind_email' ), 10, 1 );
remove_action( 'woocommerce_order_status_completed', array( $WooEvent_Reminder_Email, 'remind_email_fb' ), 11, 1 );
remove_action( 'wooevent_email_reminder', array( $WooEvent_Reminder_Email, 'send_mail' ), 10, 4 );
remove_action( 'wooevent_email_reminder_2', array( $WooEvent_Reminder_Email, 'send_mail' ), 10, 4 );
remove_action( 'wooevent_email_reminder_3', array( $WooEvent_Reminder_Email, 'send_mail' ), 10, 4 );
remove_action( 'wooevent_email_reminder_feedback', array( $WooEvent_Reminder_Email, 'send_mail_fb' ), 10, 1 );
// send email to attendee
remove_action( 'we_send_email_reminder', array( &$WooEvent_Reminder_Email, 'send_mail_attendee' ),10,1 );
return false;
}
return $is_purchasable;
}
With which action/filter, I should use the above or any modifications.
I am able to disabled the event on the basis of category, but already scheduled email are still queued up.
Any suggestions or help will be appreciated.
Thanks

Problem to use add_action or remove_action

I know the rule of add_action or remove_action like:
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display', 10 );
But how to apply remove_action this type of hook
remove_action( 'woocommerce_cart_actions', ' Points_Rewards_For_WooCommerce_Public–>mwb_wpr_woocommerce_cart_coupon', 10 );
Its not working
I think the following should work. Have you tried ?
remove_action( 'woocommerce_cart_actions', array( 'Points_Rewards_For_WooCommerce_Public', 'mwb_wpr_woocommerce_cart_coupon' ), 10 );
The syntax is like
remove_action( 'hook_name', array( 'class_name', 'callback_name' ), priority );

Activating a plugin from inside Woocomerce single product page using a check box

i am looking for a way to activate a plugin from a woocommerce single product page, the plugin displays shipping countdown for delivery but its not relevant to all products and i would like to use a check box inside the single product to turn on and of per product page
the plugin is being activated across all products at the moment using the add action from theme mod to be able to change the position this really is not necessary so if need this could be removed and replaced with some other way on activating the plugin on a per product basis
$scfwc_render_location = get_theme_mod( 'scfwc_render_location');
switch ( $scfwc_render_location ) :
case 'scfwc_after_heading' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 6 );
break;
case 'scfwc_after_price' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 11 );
break;
case 'scfwc_after_short_desc' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 21 );
break;
case 'scfwc_after_add_cart' :
add_action( 'woocommerce_single_product_summary', array( $this, 'scfwc_html_product' ), 31 );
break;
case 'scfwc_after_single_product_summary' :
add_action( 'woocommerce_product_thumbnails', array( $this, 'scfwc_html_product' ), 30 );
break;
endswitch;
I don't know what class $this is referencing, or how to get an instance of it, so this is pseudo-code. But once you have the meta field displaying and saving in the admin, you can use the meta field's value to conditionally add the scfwc_html_product() method.
function kia_conditionally_add_countdown() {
global $product;
if( $product->get_meta( '_show_countdown', true ) ) {
$my_class = somehow_get_instance_of_class();
add_action( 'woocommerce_single_product_summary', array( $my_class, 'scfwc_html_product' ), 11 );
}
}
add_action( 'woocommerce_before_single_product_summary', 'kia_conditionally_add_countdown' );
This doesn't take into account the theme mod and just hard-codes the priority/position. You could add support for the theme_mod inside the conditional logic, if needed.

Wordpress remove_action within class

I am trying to remove an action added by the Woocommerce Memberships plugin. Tracing back the actions contains functions it is initially added to the hook under
class WC_Memberships_Frontend {
public function __construct() {
add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 );
But this class is called by a private function and is a protected by the variable that its parent class declaration (as far as I can tell)
class WC_Memberships extends Framework\SV_WC_Plugin {
protected $frontend;
private function frontend_includes() {
// init shortcodes
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-shortcodes.php' );
\WC_Memberships_Shortcodes::initialize();
// load front end
$this->frontend = $this->load_class( '/includes/frontend/class-wc-memberships-frontend.php', 'WC_Memberships_Frontend' );
}
I have search and tried a number of ways to remove this
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships', 'maybe_render_thank_you_content' ), 9 );
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships_Frontend', 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships, 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships_Frontend;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships_Frontend, 'maybe_render_thank_you_content' ), 9 );
None of the above work, and other ways of trying to call WC_Memberships()->frontend; throw errors like 'Cannot access protected property WC_Memberships::$frontend'
Im not sure if the private function or protected variable are getting in the way, or if Im just not understanding something about removing an action within a class or nested classes but help would be greatly appreciated.
Edit:
Based on the code found here Ive tried
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );
but still no success.
My issue was trying to figure out how to match the $this in the original add_action add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 ); to remove it.
I finally stumbled on this chunk of code which showed me how to reference the proper instance of the class.
Final working code:
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );
As you probably know, it's usually not good practice to modify core or plugin files. Using a modification to the answer here , you could try adding to your themes functions.php file
function remove_aggravating_wcactions()
{
remove_action( 'woocommerce_thankyou', 'maybe_render_thank_you_content', 19 );
remove_action( 'woocommerce_thankyou', 'maybe_some_other_content', 20);
...
}
add_action('template_redirect','remove_aggravating_wcactions');

Resources