最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Remove a filter added by a plugin

programmeradmin2浏览0评论

I'm using a plugin that adds this filter :

$this->base    = 'hotel-room';
$this->taxBase = 'hotel-room-category';
add_filter( 'single_template', array( $this, 'registerSingleTemplate' ) );

I want to remove this filter because I want to be able to manage this file in my child-theme.

The filter is part of this class :

class HotelRoomRegister implements Lib\PostTypeInterface {
    /**
     * @var string
     */
    private $base;
    /**
     * @var string
     */
    private $taxBase;

    public function __construct() {
        $this->base    = 'hotel-room';
        $this->taxBase = 'hotel-room-category';

        add_action( 'admin_menu', array( $this, 'removeLocationTagMetaBox' ) );
        add_action( 'admin_menu', array( $this, 'removeExtraServicesTagMetaBox' ) );
        add_action( 'admin_menu', array( $this, 'removeReviewTagMetaBox' ) );

        add_filter( 'single_template', array( $this, 'registerSingleTemplate' ) );
    }

I added the following to my functions.php in my child theme, but it doesn't change anything:

global $HotelRoomRegister;
remove_filter( 'single_template', array( 'hotel-room', 'registerSingleTemplate' ) );

How can I manage this ?

Also, in case it's helpful, here is the function linked to this filter in the same class:

public function registerSingleTemplate( $single ) {
    global $post;

    if ( isset( $post ) && $post->post_type == $this->base ) {

        if ( ! file_exists( get_template_directory() . '/single-' . $this->base . '.php' ) ) {
            return MIKADO_HOTEL_CPT_PATH . '/hotel-room/templates/single-' . $this->base . '.php';
        }
    }

    return $single;
}

I'm using a plugin that adds this filter :

$this->base    = 'hotel-room';
$this->taxBase = 'hotel-room-category';
add_filter( 'single_template', array( $this, 'registerSingleTemplate' ) );

I want to remove this filter because I want to be able to manage this file in my child-theme.

The filter is part of this class :

class HotelRoomRegister implements Lib\PostTypeInterface {
    /**
     * @var string
     */
    private $base;
    /**
     * @var string
     */
    private $taxBase;

    public function __construct() {
        $this->base    = 'hotel-room';
        $this->taxBase = 'hotel-room-category';

        add_action( 'admin_menu', array( $this, 'removeLocationTagMetaBox' ) );
        add_action( 'admin_menu', array( $this, 'removeExtraServicesTagMetaBox' ) );
        add_action( 'admin_menu', array( $this, 'removeReviewTagMetaBox' ) );

        add_filter( 'single_template', array( $this, 'registerSingleTemplate' ) );
    }

I added the following to my functions.php in my child theme, but it doesn't change anything:

global $HotelRoomRegister;
remove_filter( 'single_template', array( 'hotel-room', 'registerSingleTemplate' ) );

How can I manage this ?

Also, in case it's helpful, here is the function linked to this filter in the same class:

public function registerSingleTemplate( $single ) {
    global $post;

    if ( isset( $post ) && $post->post_type == $this->base ) {

        if ( ! file_exists( get_template_directory() . '/single-' . $this->base . '.php' ) ) {
            return MIKADO_HOTEL_CPT_PATH . '/hotel-room/templates/single-' . $this->base . '.php';
        }
    }

    return $single;
}
Share Improve this question edited Nov 21, 2019 at 15:19 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Nov 21, 2019 at 14:55 GregoryGregory 6025 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

So after some digging managed to find the asnwer and instead of removing the filter you can overrid the single_template filter to add your own custom template.

add_filter( 'single_template', 'my_custom_single_template', 99, 1 );

function my_custom_single_template( $single ) {

    global $post;

    if ( isset( $post ) && $post->post_type == 'hotel-room' ) {
        $single = require_once( STYLESHEETPATH . '/single-hotel-room.php');
    }

    return $single;
}
发布评论

评论列表(0)

  1. 暂无评论