remove_action from a plugin class on a different namespace - wordpress

Good evening to all, i've a little problem removing an action from wp_head that was added by Wordpress Download Manager. That's the code from the plugin:
class WordPressDownloadManager{
function __construct(){
register_activation_hook(__FILE__, array($this, 'Install'));
add_action( 'init', array($this, 'registerPostTypeTaxonomy'), 1 );
add_action( 'plugins_loaded', array($this, 'loadTextdomain') );
add_action( 'wp_enqueue_scripts', array($this, 'EnqueueScripts') );
add_action( 'wp_head', array($this, 'wpHead') );
add_action( 'wp_footer', array($this, 'wpFooter') );
spl_autoload_register( array( $this, 'AutoLoad' ) );
new \WPDM\libs\UserDashboard();
new \WPDM\libs\Apply();
new \WPDM\admin\WordPressDownloadManagerAdmin();
new \WPDM\ShortCodes();
}
and this is the code i'm using to remove it:
function remove_wpdm() {
remove_action('wp_head', array('WordPressDownloadManager', 'wpHead'));
}
add_action('wp_head', 'remove_wpdm');
without effects.. How can i solve this? this class is in a different namespace called WPDM. Thank you in advance for any help.
Best regards,
Domenico

Try below code
function remove_wpdm() {
remove_action('wp_head', array('WordPressDownloadManager', 'wpHead'));
}
add_action('init', 'remove_wpdm');

Related

Remove photoswipe-css and photoswipe-default-skin-css in WordPress

I am building a custom webshop and I am trying to remove the photoswipe style documents but I can't seem to get rid of them, I tried a couple of "solutions" that I found online but they are not working. This is what I tried so far in the functions.php:
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
remove_theme_support( 'wc-product-gallery-slider' );
and
add_filter( 'woocommerce_enqueue_styles', 'wpf_dequeue_styles' );
function wpf_dequeue_styles( $enqueue_styles ) {
unset( $enqueue_styles['photoswipe-css'] );
unset( $enqueue_styles['photoswipe-default-skin-css'] );
return $enqueue_styles;
}
and
wp_dequeue_script('photoswipe-css');
wp_dequeue_script('photoswipe-default-skin-css');
Can someone help me finding the solution? I don't have any images in my webshop so I do not need these scripts.
You can remove_theme_support using after_setup_theme action hook.
add_action( 'after_setup_theme', 'remove_photoswipe_css', 11 );
function remove_photoswipe_css() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
remove_theme_support( 'wc-product-gallery-slider' );
}
You can wp_dequeue_style using wp_enqueue_scripts action hook.
add_action( 'wp_enqueue_scripts', 'dequeue_photoswipe_css', 11 );
function dequeue_photoswipe_css() {
wp_dequeue_style( 'photoswipe-css' );
wp_dequeue_style( 'photoswipe-default-skin-css' );
}
If anyone is still looking for the answer to this then the correct code would be :
add_action( 'wp_enqueue_scripts', 'dequeue_photoswipe_css', 99);
function dequeue_photoswipe_css() {
wp_dequeue_style( 'photoswipe' );
wp_dequeue_style( 'photoswipe-default-skin-css' );
wp_dequeue_script( 'photoswipe-ui-default' );
}

How to Unhook actions/filters in within Class in plugin

I have a business website for vacation home rentals in which the WordPress theme has its own booking system for house bookings only and it has been great!
I also, have Tour/Services (not Houses) also bookable but for these I use WooCommerce/WooCommerce Bookings plugins, not the House booking system.
Everything has been great!
But recently the theme update introduced a WooCommerce checkout option for the Home booking system which never existed prior to the update. Although I have the themes WooCommerce checkout option turned OFF, the code is still being executed in the theme's core plugin. I have been going back and forth with an open support ticket with the theme provider for a few weeks for them to put a conditional statement to NOT run the new code when the user has the option turned OFF but I do not know when or if it will ever be done. What happens now is that the theme's core plugin is manipulating the WooCommerce checkout and after the final step of placing the order of any Tour/Services in which I had always been previously using WooCommerce separately now times out after placing the order and generates over 1,000 additional "Phantom" Checkout pages in the back-end.
Bottom line:
When 3 lines (lines 27-29) are commented out of the theme's core plugin file, everything works fine. But I do not want to have to comment out these 3 lines after each plugin update.
//add_action( 'woocommerce_thankyou', array($this,'order_attach') );
//add_filter( 'woocommerce_checkout_fields', array($this,'custom_override_checkout_fields') );
//add_filter('woocommerce_create_account_default_checked', '__return_true');
Here is the full code (with lines 27-29 commented out): https://pastebin.com/jqtBpCpA
I have tried the instructions on both pages below without success:
https://mekshq.com/remove-wordpress-action-filter-class
https://github.com/herewithme/wp-filters-extras
I have inserted the following into the functions.php file
METHOD 1:
global $Wpestate_Global_Payments; //get access to the class object instance
remove_action( 'woocommerce_thankyou', array($Wpestate_Global_Payments,'order_attach') );
remove_filter( 'woocommerce_checkout_fields', array($Wpestate_Global_Payments,'custom_override_checkout_fields') );
remove_filter('woocommerce_create_account_default_checked', '__return_true');
Then using the plugin, here: https://github.com/herewithme/wp-filters-extras
I tried both methods:
METHOD 2:
remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
METHOD 3:
remove_filters_for_anonymous_class( 'woocommerce_thankyou', 'Wpestate_Global_Payments', 'order_attach' );
remove_filters_for_anonymous_class( 'woocommerce_checkout_fields', 'Wpestate_Global_Payments', 'custom_override_checkout_fields' );
remove_filters_for_anonymous_class( 'woocommerce_create_account_default_checked', 'Wpestate_Global_Payments', '__return_true' );
class Wpestate_Global_Payments {
public $stripe_payments;
public $is_woo;
public $userID;
public $user_email;
function __construct() {
$this->is_woo = wprentals_get_option('wp_estate_enable_woo','') ;
$current_user = wp_get_current_user();
$this->userID = $current_user->ID;
$this->user_email = $current_user->user_email;
add_filter( 'woocommerce_cart_item_permalink','__return_false');
add_action( 'wp_ajax_wpestate_woo_pay', array( $this, 'wpestate_woo_pay') );
add_action( 'wp_ajax_mopriv_wpestate_woo_pay', array( $this, 'wpestate_woo_pay') );
add_filter( 'woocommerce_thankyou_order_received_text', array($this, 'wpestate_woocommerce_thankyou_order_received_text'),10,2 );
add_action( 'woocommerce_before_single_product', array($this, 'wpestate_product_redirect') );
add_action( 'woocommerce_product_query', array($this, 'wpestate_custom_pre_get_posts_query' ));
add_action( 'woocommerce_order_status_completed', array($this, 'wpestate_payment_complete') );
add_action( 'woocommerce_order_status_processing', array($this, 'wpestate_payment_complete') );
//EVERYTHING WORKS ONLY WHEN THE 3 LINES BELOW ARE COMMENTED OUT
//add_action( 'woocommerce_thankyou', array($this,'order_attach') );
//add_filter( 'woocommerce_checkout_fields', array($this,'custom_override_checkout_fields') );
//add_filter('woocommerce_create_account_default_checked', '__return_true');
Can anyone provide any insight into how I can insert a remove_action & remove_filter for each in the functions.php to remove/unhook the 3 actions/filters that I currently have commented out in the plugin file?
I'm not 100% sure but my guess would be that your attempts to remove the hooks are being called before the class Wpestate_Global_Payments. This basically means they can't be removed because they haven't been added yet.
I would suggest retrying your methods but when the wp_loaded action is fired.
wp_loaded This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
So for example, in your functions.php file...
function remove_my_themes_actions() {
remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
}
add_action( 'wp_loaded', 'remove_my_themes_actions' );
If your running PHP version 5.3.0 or greater you can use an anonymous function...
add_action( 'wp_loaded', function() {
remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
} );
Hope this helps!

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');

instantiate a class from plugin in WordPress

I have a question about class written in the plugin. Let's say I have this class.
class Woo {
public static init(){
register_activation_hook( __FILE__, array( __CLASS__, 'woo_activate' ));
add_action( 'admin_menu', array(__CLASS__, 'woo_register'));
}
Woo::init();
add_action( 'plugins_loaded', array( 'Woo', 'init' ) );
My question is why put Woo::init(); there where we already have the add_action( 'plugins_loaded', array( 'Woo', 'init' ) );
Are both executed? can woo::init() be deleted and it will work fine? and vice versa?

Overwriting a filter in plugin using WordPress

I have the following filter in a WordPress plugin named 'JobBoard Package'
function apply_job_validate($validate){
$package = jb_package_get_current_package();
if(!$package){
jb_notice_add(esc_html__('You need to purchase a package before applying job.', 'jobboard-package'), 'error');
return true;
}
$applied = JB()->candidate->count_applied_all();
$limit = get_post_meta($package->ID, '_apply', true);
if($applied >= $limit){
jb_notice_add(esc_html__('Apply job limited, you can update your package.', 'jobboard-package'), 'error');
return true;
}
return $validate;
}
Being called at the top of the plugin as follows:
private function actions(){
add_action( 'wp_enqueue_scripts', array($this, 'add_scripts'));
add_action( 'admin_menu', array ($this, 'get_menu_notice') , 100);
add_action( 'admin_enqueue_scripts', array($this, 'add_admin_scripts'));
add_action( 'save_post_jobboard-post-jobs', array($this, 'save_post'));
add_filter( 'jobboard_query_endpoint_args', array($this, 'add_endpoint'));
add_filter( 'jobboard_query_endpoint_package_title', array($this, 'add_endpoint_package_title'));
add_filter( 'jobboard_query_endpoint_transactions_title', array($this, 'add_endpoint_transactions_title'));
add_filter( 'jobboard_employer_navigation_args', array($this, 'add_endpoint_menu'));
add_filter( 'jobboard_candidate_navigation_args', array($this, 'add_endpoint_menu'));
add_action( 'jobboard_endpoint_employer_new', array($this, 'get_template_add_new'), 0);
add_action( 'jobboard_endpoint_employer_package', array($this, 'get_template_package') );
add_action( 'jobboard_endpoint_candidate_package', array($this, 'get_template_package') );
add_filter( 'jobboard_form_handler_validate_add_job', array($this, 'add_new_job_validate'));
add_filter( 'jobboard_form_handler_validate_apply_job', array($this, 'apply_job_validate'));
}
I have tried disabling apply_job_validate via the functions.php and as a plugin using the following code and its variations with no luck and would really appreciate some help.
if( class_exists('JB_Package' ) ){
//This should work in whatever case
remove_filter('jobboard_form_handler_validate_apply_job', array( 'JB_Package', 'apply_job_validate'));
//or Instantiating a new instance
//remove_filter('jobboard_form_handler_validate_apply_job', array( new JB_Package(), 'apply_job_validate'));
//or Targeting the specific instance, not tested
//remove_filter('jobboard_form_handler_validate_apply_job', array( JB_Package::get_instance(), 'apply_job_validate'));
}
I have also tried it as follows:
function remove_package() {
if (class_exists('JB_Package')) {
remove_filter('jobboard_form_handler_validate_apply_job', array( 'JB_Package', 'apply_job_validate'));
}
}
add_action('plugins_loaded','remove_package');
Any help would be appreciated. Many thanks!
You should use a later action hook, like after_setup_theme. You're hooking into plugins_loaded, which is fired before your functions.php file is parsed. Try:
function remove_package() {
if (class_exists('JB_Package')) {
remove_filter('jobboard_form_handler_validate_apply_job', array( 'JB_Package', 'apply_job_validate'));
}
}
add_action('after_setup_theme','remove_package');
However, you should check to see when the 'JB_Package' class is hooked (if it's hooked at all). If it's hooked at init, then you need to hook into something later like wp_loaded.
//if class 'JB_Package' is hooked at 'init'
add_action('wp_loaded','remove_package');

Resources