0

I have a theme which is using a custom post type ("room"), registered in their own plugin.

// add action to create room post type
add_action( 'init', 'gdlr_create_room' );
if( !function_exists('gdlr_create_room') ){
    function gdlr_create_room() {
        global $theme_option;

        if( !empty($theme_option['room-slug']) ){
            $room_slug = $theme_option['room-slug'];
            $room_category_slug = $theme_option['room-category-slug'];
            $room_tag_slug = $theme_option['room-tag-slug'];
        }else{
            $room_slug = 'room';
            $room_category_slug = 'room_category';
            $room_tag_slug = 'room_tag';
        }

        register_post_type( 'room',
            array(
                'labels' => array(
                    'name'               => __('Rooms', 'gdlr-hotel'),
                    'singular_name'      => __('Room', 'gdlr-hotel'),
                    'add_new'            => __('Add New', 'gdlr-hotel'),
                    'add_new_item'       => __('Add New Room', 'gdlr-hotel'),
                    'edit_item'          => __('Edit Room', 'gdlr-hotel'),
                    'new_item'           => __('New Room', 'gdlr-hotel'),
                    'all_items'          => __('All Rooms', 'gdlr-hotel'),
                    'view_item'          => __('View Room', 'gdlr-hotel'),
                    'search_items'       => __('Search Room', 'gdlr-hotel'),
                    'not_found'          => __('No rooms found', 'gdlr-hotel'),
                    'not_found_in_trash' => __('No rooms found in Trash', 'gdlr-hotel'),
                    'parent_item_colon'  => '',
                    'menu_name'          => __('Rooms (Hotel)', 'gdlr-hotel')
                ),
                'public'             => true,
                'publicly_queryable' => true,
                'show_ui'            => true,
                'show_in_menu'       => true,
                'query_var'          => true,
                'rewrite'            => array( 'slug' => $room_slug  ),
                'capability_type'    => 'post',
                'has_archive'        => true,
                'hierarchical'       => false,
                'menu_position'      => 7,
                'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields' )
            )
        );

        // create room categories
        register_taxonomy(
            'room_category', array("room"), array(
                'hierarchical' => true,
                'show_admin_column' => true,
                'label' => __('Room Categories', 'gdlr-hotel'), 
                'singular_label' => __('Room Category', 'gdlr-hotel'), 
                'rewrite' => array( 'slug' => $room_category_slug  )));
        register_taxonomy_for_object_type('room_category', 'room');

        // create custom taxonomy for room category
        if( is_admin() && class_exists('gdlr_tax_meta') ){
            global $gdlr_sidebar_controller;

            new gdlr_tax_meta( 
                array(
                    'taxonomy'=>'room_category',
                    'slug'=>'gdlr_hotel_branch'
                ),
                array(
                    'upload' => array(
                        'title'=> __('Hotel Thumbnail', 'gdlr-song'),
                        'type'=> 'upload'
                    ),
                    'content' => array(
                        'title'=> __('Hotel Location', 'gdlr-song'),
                        'type'=> 'textarea'
                    )
                )
            );
        }


        // create room tag
        register_taxonomy(
            'room_tag', array('room'), array(
                'hierarchical' => false, 
                'show_admin_column' => true,
                'label' => __('Room Tags', 'gdlr-hotel'), 
                'singular_label' => __('Room Tag', 'gdlr-hotel'),  
                'rewrite' => array( 'slug' => $room_tag_slug  )));
        register_taxonomy_for_object_type('room_tag', 'room');  

        // add filter to style single template
        add_filter('single_template', 'gdlr_register_room_template');
    }
}

if( !function_exists('gdlr_register_room_template') ){
    function gdlr_register_room_template($single_template) {
        global $post;

        if ($post->post_type == 'room') {
            $single_template = dirname(dirname( __FILE__ )) . '/single-room.php';
        }
        return $single_template;    
    }
}

I basically just want to over-ride the last bit there so that instead of the 'room' type using "dirname(dirname( FILE )) . '/single-room.php';" that it rather uses my own template in my child theme directory, ie:

$single_template = get_stylesheet_directory() .'/single-room.php';

How do I override it so that it uses the above file?

edit: This is what I have in my childs function.php file, but doesnt appear to be working (even though it appears to be loading the code):

add_filter('single_template','nobleman_rooms');

function nobleman_rooms($single_template) {
    global $post;

    if ($post->post_type == 'room') {
        $single_template = get_stylesheet_directory() .'/single-room.php';
    }
    return $single_template;    
}
  • What exactly is the desired template functionality? Are you looking to use the template if found in the current parent/child theme but fall back to the plugin's copy if it's not found in either theme? – Chris Cox Aug 10 '17 at 10:39
  • The desired functionality is that my template for this custom post type is used instead of the file in the wp-content/plugins directory. This is a theme I bought and its displaying the page for a 'room' with elements I do not want. I have made a copy of the original file and removed the elements; now just need the theme to actually use it. (see edit above) – SupaMonkey Aug 10 '17 at 10:41
1
remove_filter('single_template', 'gdlr_register_room_template', 11);

That should do it. The original hook doesn't specify a priority so it will default to 10, specifying 11 should ensure that it's removed straight afterward.

WP will then revert to its default behaviour of looking for the template in the root of the child theme followed by the root of the parent theme.

  • Glorious! That did the trick, I just needed to add a priority to my add_filter of 12 as well and then it all started working! Thanks! ie: add_filter('single_template','nobleman_rooms',12); – SupaMonkey Aug 10 '17 at 10:59
  • It shouldn't even need your add_filter at all, checking the child theme first for a CPT template is what WP does by default, ref locate_template() codex.wordpress.org/Function_Reference/locate_template – Chris Cox Aug 10 '17 at 11:02
  • Yeah, not sure why it doesnt work without it though. I'm going to assume it has something to do with the fact that the original is part of a plugin thats part of a theme as opposed to directly part of the theme? I dont know - but it working now and thats all that matters :) Thanks! – SupaMonkey Aug 11 '17 at 8:36

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.