Redirect WordPress Custom Post Type

WordPress hook is a popular way to overwrite default function in WordPress. Perhaps you want to prevent non logged-in user to access your singular custom post which by default will be displayed using single.php file template. This is explained in WordPress template hierarchy. template_include hook can be used as one of the options available.

Here sample code to be included in your theme functions.php file. In this example, when non-logged in user trying to access the ‘mycustompost’ single post type, it will be displayed the 404 template.

function download_redirect($template)
{
    if( is_singular( 'mycustompost' ) && ! is_user_logged_in() )
    {
        return get_template_part('404');
    } else {
        return $template;
    }
}
add_action( 'template_include', 'download_redirect' );

Related Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2011 - myPHPmaster. All rights reserved.
top

This is sample for Menu caller


Related Posts:

false

Related Posts: