Remove action defined within plugin class - wordpress

I am developing a ecommerce theme.
I have installed the WooCommerce PayPal Checkout Payment Gateway plugin for payment, and I want to change the location of this checkout button, I tried to remove_action display checkout button but it didn't work, How can I remove action in this case?
Hook in plugin file:
plugins/woocommerce-gateway-paypal-express-checkout/includes/class-wc-gateway-ppec-with-spb.php
This is my code in functions.php file, it not works:
function remove_anon_filters( $name, $functionname, $priority ) {
global $wp_filter;
foreach ( $wp_filter[ $name ][ $priority ] as $key => $data ) {
if ( stripos( $key, $functionname ) ) {
remove_action( $name, $key, $priority );
}
}
}
add_action( 'plugins_loaded', 'demo_init', 999 );
function demo_init() {
remove_anon_filters( 'woocommerce_review_order_after_submit', 'display_paypal_button', 10 );
}
// or.
add_action( 'init', 'remove_init', 999 );
function remove_init() {
remove_action( 'woocommerce_review_order_after_submit', array( 'WC_Gateway_PPEC_With_SPB', 'display_paypal_button' ), 10 );
}
Any help is appreciated, thank you.

To remove a non-static method, you should instantiate the class, and remove the action in the wp_head https://developer.wordpress.org/reference/functions/remove_action/
Additionally, the remove_action should be the same priority as the add action, which in this case is not specified.
add_action( 'wp_head', 'remove_ppec_with_spb', 10);
function remove_ppec_with_spb() {
$class = new WC_Gateway_PPEC_With_SPB;
remove_action( 'woocommerce_review_order_after_submit', array( $class , 'display_paypal_button' ) );
}

Related

Remove product title when product is "out of stock" on WooCommerce shop and archives pages

I am running an art gallery website using WooCommerce and the owner does not want to show the product name/title if the product has been sold/out of stock.
I have got this far with putting together a function, but it doesn't work. I wondered if anyone could give me any tips?
// Hides title on Sold products
add_filter( 'woocommerce_shop_loop_item_title', 'remove_name', 10, 2 );
function remove_name ( $product ) {
if ( ! $product->is_in_stock()) {
$title = '';
}
return $title;
}
Your code contains some errors and mistakes:
woocommerce_shop_loop_item_title is NOT an filter but an action hook
No arguments are passed to this hook , while your code says there would be 2
$product is undefined, use global $product;
Note: I've added a line with debug information, it can be removed
So you get:
function action_woocommerce_shop_loop_item_title() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Should be removed but may be useful towards debugging
echo '<p style="color: red; font-size: 20px;">DEBUG information: ' . $product->get_stock_status() . '</p>';
// NOT in stock
if ( ! $product->is_in_stock() ) {
// Removes a function from a specified action hook.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
}
}
}
add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 9 );
Please try to use this hook to remove product title. Use this inside the condition to check if product is in stock:
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
This does what you want but still allows access to the product page.
add_action( 'woocommerce_shop_loop_item_title', function() {
global $product;
if ( ! $product->is_in_stock()) {
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
}
}, 5 );
add_action( 'woocommerce_single_product_summary', function() {
global $product;
if ( ! $product->is_in_stock()) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 5);
}
}, 1);

How to remove gutenberg_render_title_tag from theme function file?

I have try remove action but not working.
add_action( 'wp_loaded', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
or
add_action( 'init', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
i am using yoast plugin. Yoast plugin also using remove action https://github.com/Yoast/wordpress-seo/blob/trunk/src/integrations/front-end-integration.php#L220 but it's not working. Still tag coming two time. i wants to remove gutenberg title tag.
I got the solution for this.
add_action( 'wp', 'remove_default_guten_title' );
function remove_default_guten_title() {
foreach ( gutenberg_get_template_type_slugs() as $template_type ) {
add_filter( str_replace( '-', '', $template_type ) . '_template', 'remove_default_title', 21, 3 );
}
}
function remove_default_title() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
return gutenberg_dir_path() . 'lib/template-canvas.php';
}

WP Woo - custom endpoint is deleted after saving Woo settings

I have added an endpoint to my WP plugin (to manage extra information in my-account section in woocommerce -> my points) - my-account/moje-punkty
I did that all based on bunch of tutorials, the endpoint starts on plugin activation and is followed by a rewrite flush.
My colleague reported to me that this suddenly stops working (returning 404 error) when an user is added to the DB, although I added plenty of users while testing it and never encountered this problem.
However today I noticed that after saving Woo settings, my custom endpoint is also deleted.
Does anyone know how I can make sure that the custom endpoint stays always as long as the pluing in active?
The code reference:
function __construct()
{
register_activation_hook( __FILE__, array( $this, 'foc_points_moje_punkty_endpoints' ) );
register_deactivation_hook( __FILE__, array( $this, 'foc_points_flush_rewrite_rules' ) );
}
add_filter( 'query_vars', array ( $this, 'my_custom_query_vars'), 0 );
function my_custom_query_vars( $vars ) {
$vars[] = 'moje-punkty';
return $vars;
}
add_filter ( 'woocommerce_account_menu_items', array ( $this, 'foc_front_account_menu_points_info') );
function foc_front_account_menu_points_info( $items ) {
echo '<div class="points_dropdown_area">' . esc_html__( 'Your points', 'foc-lang' ) . ': <em>' . $punkty_meta_value . '</em></div>';
$items['moje-punkty'] = esc_html__( 'Points history', 'foc-lang' );
return $items;
}
add_action( 'woocommerce_account_moje-punkty_endpoint', array ( $this, 'foc_front_punkty_endpoint_content' ) );
function foc_front_punkty_endpoint_content() {
// my code
}
/**
* Flush rewrite rules on plugin activation.
*/
function foc_points_moje_punkty_endpoints() {
add_rewrite_endpoint( 'moje-punkty', EP_ROOT | EP_PAGES );
$this->foc_points_flush_rewrite_rules();
}
/**
* Flush rewrite rules on plugin de-activation.
*/
function foc_points_flush_rewrite_rules() {
flush_rewrite_rules();
}
Thank you!

rewrite url with custom plugin with permalink

I have developed plugin and uses shortcode to display.
So, there is two type of items to display like
if get title in url, display single item and if not all items so i have added into if else in condition.
So for that title, i have passed
add_query_arg( array('title' =>sanitize_title($data->get_title())), get_permalink($post_id) );
now url is generated by this is domain.com/post-slug/?title=nameoftitle
but i want to url like domain.com/post-slug/nameoftitle
I have tried following code but not working
function update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
}
add_action( 'init', 'update_rewrite_rules' );
but its not working
I got solution of it. working code is:
function js_update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
$rewrite_rules = get_option( 'rewrite_rules' );
$newVarRegex = '^([^/]*)/title/([^/]*)/?';
// check if the rule exists
if ( !isset( $rewrite_rules[$newVarRegex] ) ) {
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
flush_rewrite_rules();
}
}
function js_on_activation() {
js_update_rewrite_rules();
}
register_activation_hook( __FILE__, 'js_on_activation' );
function js_on_init() {
js_update_rewrite_rules();
}
add_action( 'init', 'js_on_init' );

wordpress moving publish metabox

I'm working on a plugin and I'm trying to:
1- move the publish metabox from side to the bottom of the page in the normal section.
2- force 1 column layout for the plugin custom post type edit page.
3- remove the screen options tab for the custom post type.
I'm using the next code block but it dont work:
function sds_do_meta_boxes() {
remove_meta_box( 'submitdiv', 'sliding_panel', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sliding_panel', 'normal', 'core' );
}
add_action('do_meta_boxes', 'sds_do_meta_boxes');
function SDS_init() { //The current user is already authenticated by this time
add_filter( 'screen_layout_columns', 'so_screen_layout_columns' );
add_filter( 'get_user_option_screen_layout_dashboard', 'so_screen_layout_dashboard' );
add_filter( 'screen_options_show_screen', 'SDS_remove_screen_options_tab');
}
add_action( 'init', 'SDS_init' );
function so_screen_layout_columns( $columns ) {
$columns['dashboard'] = 1;
return $columns;
}
function so_screen_layout_dashboard() {
return 1;
}
function SDS_remove_screen_options_tab() {
return false;
}
So, the 'publish' metabox is removed but it is not re-added. Also, the 1 column layout filters don't work. I need help:)
Changing the $priority parameter from core to high and setting priority of add_action to 0 should do the trick. I would also suggest using the dynamic add_meta_boxes_{$post_type} hook:
add_action( 'add_meta_boxes_sliding_panel', 'sds_do_meta_boxes', 0, 1 );
function sds_do_meta_boxes( $post )
{
remove_meta_box( 'submitdiv', 'sliding_panel', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sliding_panel', 'normal', 'high', null );
}

Resources