• Resolved Clickadelic

    (@clickadelic)


    I’m working for a tire company and I’m writing two plugins.

    The first plugin is to show alloy wheels.
    The second plugin is to show tires.

    Both plugins come as a custom post type ‘wheels’ and ‘tires’, and both plugins have a template logic in it (templates/archive-wheels.php and templates/archive-tires.php)

    **My problem:**
    When both plugins are active, the **wheels** plugin overwrites the **tires** archive page and I don’t know why on earth it is doing it.

    I have really tried to avoid namespace conflicts in advance by using other function names, other prefixes etc.

    Here is the relevant code for registering the CPTs and the template logic for both plugins:

    Register Custom Post Type wheels:

    
    function rg_wp_wheels_register_cpt(){
        // Custom Post Type Name
        $cpt_name = 'wheels';
        // CPT Features
        $cpt_features = array(
    	'title',
    	'revisions'
        );
        // Slug for archive page
        $cpt_slug = 'designs';
        $labels = array(
            'name'          =>      __('Wheels', 'rg-wp-wheels'),
    	'singular_name' =>	__('Wheel', 'rg-wp-wheels'),
    	'menu_name'	=>	__('Wheels', 'rg-wp-wheels'),
    	'name_admin_bar'=>	__('Wheel', 'rg-wp-wheels'),
    	'all_items'	=>	__('Designs', 'rg-wp-wheels'),
    	'add_name'	=>	__('Add new wheel', 'rg-wp-wheels'),
    	'add_new_item'	=>	__('Add new wheel', 'rg-wp-wheels'),
    	'edit'		=>	__('Edit wheel', 'rg-wp-wheels'),
    	'edit_item'	=>	__('Edit wheel', 'rg-wp-wheels'),
    	'new_item'	=>	__('New wheel', 'rg-wp-wheels'),
    	'view'		=>	__('View', 'rg-wp-wheels'),
    	'view_item'	=>	__('View', 'rg-wp-wheels'),
    	'search_items'	=>	__('Search ', 'rg-wp-wheels'),
    	'parent'	=>	__('Parent', 'rg-wp-wheels'),
    	'not_found'	=>	__('No wheels found', 'rg-wp-wheels'),
    	'not_found_in_trash'=>	__('No wheels found in Trash', 'rg-wp-wheels')
    	);
    	$args = array(
                'labels'			=>	$labels,
                'public'			=>	true,
                'publicly_queryable'	=>	true,
                'exclude_from_search'	=>	false,
                'show_in_nav_menus'		=>	true,
                'show_ui'		        =>	true,
    	    'show_in_menu'		=>	true,
    	    'show_in_admin_bar'		=>	true,
                'menu_position'		=>	21,
    	    'menu_icon'			=>	'dashicons-marker',
    	    'can_export'		=>	true,
    	    'delete_with_user'		=>	false,
                'hierarchical'		=>	false,
                'has_archive'		=>	true,
    	    'query_var'			=>	true,
    	    'capability_type'		=>	'post',
    	    'map_meta_cap'		=>	true,
    		// 'capabilities'	=> array(),
    		'rewrite'		=>	array(
    		    'slug'		=> $cpt_slug,
    		    'with_front'        => true,
    	            'pages'		=> true,
    			'feeds'		=> false
    		),
    		'supports'		=> $cpt_features
    	);
    	register_post_type($cpt_name, $args);
    }
    add_action('init', 'rg_wp_wheels_register_cpt');
    

    Template Logic for the **wheels** plugin

    
    function rg_wp_wheels_template_logic($original_template) {
    	$post_type = get_post_type();
    	
    	if(is_archive() || is_search() && $post_type == 'wheels') {
    
                if(file_exists(get_template_directory_uri() . '/archive-wheels.php')) {
                    return get_template_directory_uri() . '/archive-wheels.php';
                } else {
                    return plugin_dir_path(__FILE__) . 'templates/archive-wheels.php';
                }
    
    	} elseif(is_single() && $post_type == 'wheels') {
    	    if(file_exists(get_template_directory_uri() . '/single-wheels.php')) {
                     return get_template_directory_uri() . '/single-wheels.php';
                } else {
                     return plugin_dir_path(__FILE__) . 'templates/single-wheels.php';
                }	
    	}
    	return $original_template;
    }
    add_action('template_include', 'rg_wp_wheels_template_logic');
    

    Register Custom Post Type tires:

    
    function rg_wp_tires_register_cpt(){
    
    	// Custom Post Type Name
    	$cpt_name = 'tires';
    	// CPT Features
    	$cpt_features = array(
    		'title',
    		'revisions'
    	);
    	// Slug
    	$cpt_slug_tires = 'profiles';
    	$labels = array(
    	    'name'            =>	__('Tires', 'rg-wp-tires'),
    	    'singular_name'   =>	__('Tire', 'rg-wp-tires'),
                'menu_name'	      =>	__('Tires', 'rg-wp-tires'),
                'name_admin_bar'  =>	__('Tires', 'rg-wp-tires'),
                'all_items'       =>	__('Profiles', 'rg-wp-tires'),
    	    'add_name'	      =>	__('Add new tire', 'rg-wp-tires'),
    	    'add_new_item'    =>	__('Add new tire', 'rg-wp-tires'),
                'edit'            =>	__('Edit tire', 'rg-wp-tires'),
                'edit_item'       =>	__('Edit tire', 'rg-wp-tires'),
                'new_item'        =>	__('New tire', 'rg-wp-tires'),
                'view'            =>	__('View', 'rg-wp-tires'),
                'view_item'       =>	__('View', 'rg-wp-tires'),
                'search_items'    =>	__('Search ', 'rg-wp-tires'),
                'parent'          =>	__('Parent', 'rg-wp-tires'),
                'not_found'	      =>	__('No tires found', 'rg-wp-tires'),
                'not_found_in_trash' =>	__('No tires found in Trash', 'rg-wp-tires')
    	);
    
    	$args = array(
                 'labels'	      =>	$labels,
                 'public'         =>	true,
                 'publicly_queryable'	=>	true,
                 'exclude_from_search'	=>	false,
                 'show_in_nav_menus'		=>	true,
                 'show_ui'				=>	true,
                 'show_in_menu'			=>	true,
                 'show_in_admin_bar'		=>	true,
    	     'menu_position'			=>	22,
                 'menu_icon'	                =>	'dashicons-marker',
                 'can_export'			=>	true,
                 'delete_with_user'		        =>	false,
                 'hierarchical'			=>	false,
                 'has_archive'			=>	true,
                 'query_var'			=>	true,
    		'capability_type'		=>	'post',
    		'map_meta_cap'			=>	true,
    		// 'capabilities'		=> array(),
    		'rewrite'				=>	array(
                        'slug'	        => $cpt_slug_tires,
                        'with_front'        => true,
                        'pages'		=> true,
                        'feeds'		=> false
    		),
    		'supports'		=> $cpt_features
          );
          register_post_type($cpt_name, $args);
    }
    add_action('init', 'rg_wp_tires_register_cpt');
    

    Template logic for the **tires** plugin:

    
    function rg_wp_tires_template_logic($original_template) {
    	$post_type = get_post_type();
    	
    	if(is_archive() || is_search() && $post_type == 'tires') {
    
    		if(file_exists(get_template_directory_uri() . '/archive-tires.php')) {
    			return get_template_directory_uri() . '/archive-tires.php';
    		} else {
    			return plugin_dir_path(__FILE__) . 'templates/archive-tires.php';
    		}
    
    	} elseif(is_single() && $post_type == 'tires') {
    
    		if(file_exists(get_template_directory_uri() . '/single-tires.php')) {
    			return get_template_directory_uri() . '/single-tires.php';
    		} else {
    			return plugin_dir_path(__FILE__) . 'templates/single-tires.php';
    		}
    		
    	}
    	
    	return $original_template;
    }
    add_action('template_include', 'rg_wp_tires_template_logic');
    

    When I deactivate the wheels plugin, the tire plugin template logic works perfectly fine. When both plugins are active, the archive-wheels.php ALWAYS overwrites the archive-tires.php. Why?

    Please help me, I’m losing my mind on this.

    • This topic was modified 8 years, 11 months ago by Clickadelic.
    • This topic was modified 8 years, 11 months ago by Clickadelic. Reason: Code formatting
Viewing 3 replies - 1 through 3 (of 3 total)
  • Your problem is here:
    if(is_archive() || is_search() && $post_type == 'wheels') {

    is_archive is resolving as true for both, so whichever runs last is going to be used. You need to properly group your conditions like so:

    if( ( is_archive() || is_search() ) && $post_type == 'wheels') {

    I’ve put is_archive() and is_search() in brackets so they’re treated as one condition that has to be true and the post type is the one you want.

    I’d propose doing it slightly differently though. get_post_type() isn’t the correct function to check if you’re viewing a post type archive or a search with a post type defined. It will just get the post type of whatever the global $post object is set to when it’s run, which you can’t reliably use to determine what archive you’re viewing.

    Instead, use is_post_type_archive():

    if ( is_post_type_archive( 'tires' ) || ( is_search() && $_GET['post_type'] === 'tires' ) ) {

    Thread Starter Clickadelic

    (@clickadelic)

    I will never be able to pay back this help.

    As I’m reading through your explaination, it all becomes clear to me.
    Man, thank you, thank you, thank you, thank you!!!!

    Please give me your address in a private message and I’ll send you at least a kilo of German candy (I’m dead serious :D).

    I’m so greatful for this. You have made my day for the next two weeks!!

    Thank you so much! WordPress community is unbelievable!

    Toby

    Moderator bcworkz

    (@bcworkz)

    I hate to throw a wet blanket on a clearly well intentioned offer of gratitude, but offering to pay for assistance is contrary to forum guidelines. It’s great that the offered compensation is not monetary, nor solicited, but it’s still contrary to the guideline intent. I am thus closing this topic. It sometimes sucks to be the enforcer.

    Toby, perhaps you could pay it forward. Find some forum topics where you can offer advice 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘archive-*.php gets overwritten by other plugin’s archive-*.php’ is closed to new replies.