Well I made a plugin that uses template_redirect
for the custom post type squeezepages
and it works all fine and dandy, but I copied the exact same code, put it in a new plugin, activated it (with a few minor adjustments, for example I changed the custom post type to newpages
and the template_redirect
is taking the template from a different source) and suddenly it doesn’t work?
I can have both plugins active at the same time, the squeeze pages work, the new pages don’t. It puzzles me so much. I even disabled the squeeze pages plugin and it still doesn’t work. I’ve tried all combinations (there’s only a couple) and still nothing. It seems like it’s not working for NO reason. I’ve been staring at the same code for 2 hours and I’m not able to figure it out.
Here’s the code for my custom post types:
add_action( 'init', 'ifp_create_post_type' );
function ifp_create_post_type() {
register_post_type( 'newpages',
array(
'labels' => array(
'name' => __( 'New Pages' ),
'singular_name' => __( 'New Page' )
),
'public' => true,
'menu_position' => 5,
'rewrite' => true,
'rewrite' => array('slug' => 'newpage', 'with_front' => FALSE),
'supports' => array('title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes')
)
);
}
And my template_redirect
:
function ifp_default_template() {
if(get_post_type() == 'newpages') : global $wp_query, $post, $posts;
include(PLUGINDIR . '/newpage/themes/default.php');
exit; endif;
}
add_action('template_redirect', 'ifp_default_template');
I solved my own problem. I needed to put
flush_rewrite_rules( false );
after myregister_post_type
function. The reason it wasn’t working is because it was returning the page with a 404 error, so I figured out it was a permalinks problem and the code above solved it for me.Not much code here… Let’s break it down into things that can go wrong:
Verify that
ifp_default_template()
is firing (for example earlier redirect could includeanother template and dies, never reaching it).
Verify that
get_post_type()
returns expected value.Verify that include path is being generated correctly (I’d use
plugins_url()
, it’s more robust than building path from strings).