Action init not firing after update WP 5.2.2 - wordpress

I recently updated my website to WP 5.2.2 and 'init' action seems not to fire anymore.. I had few VC element mapped to that action but they are not working anymore..
This is the vc_map init code:
class VC_Extensions_FancyBox extends WPBakeryShortCode {
function __construct() {
if( has_action('init') ){
die('has init'); // this is printing correctly
}
add_action( 'init', array($this, 'banner_init'));
add_shortcode('vc_fancybox', array($this, 'vc_fancybox_func'));
}
function banner_init() {
if( has_action('init') ){
die('has banner_init'); //this is not printing at all..
}
vc_map( array(........) );
}
function vc_fancybox_func() {
....
}
}
I added 2 checks in the code, the first one debugs correctly, the other one doesn't. Any idea why this is happening?
Thank you very much
EDIT: using action 'wp_loaded' the element is showing correctly...

As per Edit, I just replace 'init' to 'wp_loaded' and Worked perfectly
add_action( 'wp_loaded', array( $this, 'vc_progressbar_mapping' ) );
//add_action( 'init', array( $this, 'vc_progressbar_mapping' ) );

Related

Remove an action - Wordpress (Specifically Woocommerce PIP plugin)

I have Woocommerce website with the plugin Print Invoices/Packing Lists installed.
I'm trying to remove an action and I've narrowed down the action to this (shortened) code;
class WC_PIP_Document_Invoice extends WC_PIP_Document {
public function __construct( array $args ) {
parent::__construct( $args );
// add a "View Invoice" link on order processing/complete emails sent to customer
add_action( 'woocommerce_email_order_meta', array( $this, 'order_paid_email_view_invoice_link' ), 40, 3 );
}
}
So I'm looking at removing this using; https://codex.wordpress.org/Function_Reference/remove_action
However as the action as added within a class, I can't quite work out what to pass into the function name. Would it be something like;
remove_action( 'woocommerce_email_order_meta', array( 'WC_PIP_Document_Invoice', 'order_paid_email_view_invoice_link' ), 40 );
Can anyone point me in the right direction?
Many thanks
Try following code that should work for you.
add_action( 'init', 'wpse_106269_remove_hooks', 11 );
function wpse_106269_remove_hooks(){
remove_action( 'woocommerce_email_order_meta', 'action_woocommerce_email_order_meta', 10, 4 );
}

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

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

WP Plugin: using add_filter inside of a class

I am using WP v3.3.1, and I am trying to make a plugin. I have gotten it semi-working. It initiated, and add_action works, but for some reason my filters don't get triggered. When I googled around, I saw I was supposed to do it like this, but it is not working. I also tried including it outside of the class, which didn't work either. The error log is written to from the constructor, but not the xmlAddMethod. I tested the xmlrpc call in a single file, and it worked, but having problems making classes.
//DOESN'T WORK HERE
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
class TargetDomain extends Domain
{
public function __construct()
{
error_log('TARGET: __construct');
//DOESN'T WORK HERE EITHER
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
parent::__construct();
}
function xmlAddMethod( $methods )
{
error_log('TARGET: xml_add_method');
$methods['myBlog.publishPost'] = 'publishMyPost';
return $methods;
}
Change this:
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
To:
add_filter( 'xmlrpc_methods', array( 'TargetDomain', 'xmlAddMethod') );
You can also use php's magic __CLASS__ constant.
add_filter( 'xmlrpc_methods', array( __CLASS__, 'xmlAddMethod') );

WordPress plugins wp_enque_script()

so i'm developing my first WordPress plugin and i am having some difficulties...
I am doing it Object Oriented...
In the bottom, when 'plugins_loaded', i Create a new instance of myClass. It also enques a javascript, everytime any page is loaded. This script registration works, because i get a console.log every page load. It then registers an action on 'publish_post' that is fired when an admin publishes(saves) a new post and invokes my publish_post() method.
The method is called, when a post is published; i know it because if i uncomment it's two first lines, the sctipt dies with my var_dump.
My problem is that wp_enque_script() is not working in this method. For some reason my script isn't called...
Here's the code:
<?php
class myClass{
function __construct(){
// hooks & filters..
add_action( 'publish_post', array($this, 'publish_post'));
wp_enqueue_script(
'plugin', //$handle
plugins_url('/js/plugin.js', __FILE__)//$src
);
}
function publish_post(){
//global $wp_query;
//die(var_dump($wp_query));
wp_enqueue_script(
'publish', //$handle
plugins_url('/js/publish.js', __FILE__)//$src
);
}
}
/* Initialise outselves */
add_action( 'plugins_loaded', create_function( '', 'global $myObject; $myObject = new myClass;' ));
?>
Anyone has any idea why this is happening? thanx
Just had the same problem. You have to add it to a hook, for example the init (I tried with admin_head hook but that didn't work so I picked init because I saw it in another plugin. And it seem to work fine for me)
In you construct add:
add_action('init', array($this, 'loadMyScripts'));
and in the function called by the action:
public function loadMyScripts()
{
wp_enqueue_script(
'publish', //$handle
plugins_url('/js/publish.js', __FILE__)//$src
);
}

Resources