1

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

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;
}
| improve this answer | |
  • hello, thanks for your fast reply. I have done this change, but it doesn't work. Still the same result. – Gregory Nov 21 '19 at 15:01
  • Question, why is the class at this place, and in "add_filter" is something else ? – Gregory Nov 21 '19 at 15:04
  • 1
    In the add filter the array takes the $this because the function defined is hooked inside the class itself and in your case you want to remove the filter outside the class. So if you were going to put the remove_filter inside the 'HotelRoomRegister' class then you would use $this – giannisrig Nov 21 '19 at 15:08
  • ok, thanks for the tip. So i wonder why the remove_filter does'nt take effect. If i comment the add_filter in the original file, it works well. I readed that maybe i remove filter before it has been added ? How can i manage this ? – Gregory Nov 21 '19 at 15:09
  • I updated the answer. Try to add 99 after the array which is the priority. If this does not work either ther is another way – giannisrig Nov 21 '19 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.