使用WordPress

时间:2017-09-11 15:10:52

标签: wordpress plugins filter overwrite

我在名为“JobBoard Package”的WordPress插件中有以下过滤器

        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;
    }

在插件顶部调用如下:

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

我已尝试通过functions.php禁用apply_job_validate,并使用以下代码及其变体作为插件,没有运气,非常感谢一些帮助。

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

我也尝试过如下:

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

任何帮助将不胜感激。非常感谢!

1 个答案:

答案 0 :(得分:1)

您应该使用稍后的操作挂钩,例如after_setup_theme。你正在挂钩plugins_loaded,在你的functions.php文件被解析之前被触发。尝试:

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

但是,您应该检查“JB_Package”类何时被挂钩(如果它被挂钩)。如果它挂在init,那么您需要在wp_loaded之后加入一些内容。

//if class 'JB_Package' is hooked at 'init'
add_action('wp_loaded','remove_package');