0

Let's say i have two types of templates available when making a Page. "Default template" and "Intermediate template".

I have a role called "editor", and i do not want the editor to access the "Intermediate template".

Is this possible (with a hook) ?

3
  • Do you need to hide a template from the page editor screen back end so that the user only have the one option to choose from when he creates a new page? Please answer this in the form of an edit to your question Commented Dec 10, 2014 at 9:16
  • Good point @PieterGoosen, I've answered the question maybe incorrectly. Ooops, no, I was right, my answer was accepted.
    – cybmeta
    Commented Dec 10, 2014 at 9:52
  • @cybmeta hahaha, great. Question is a bit ambiguous. Read your answer and the question, bit confusing :-). Enjoy Commented Dec 10, 2014 at 10:43

1 Answer 1

1

If you need to change the template used, you can use the template_include filter (change "itermediate-template.php" and "page.php" with correct file names of your template files):

add_filter( 'template_include', 'cyb_exclude_template_for_editors', 99 );
function cyb_exclude_template_for_editors( $template ) {

    $user = wp_get_current_user();
    if( in_array( "editor", (array) $user->roles ) && is_page_template( "itermediate-template.php" ) ) {
        $new_template = locate_template( array( 'page.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

If you need to redirect the user to another page you can use template_redirect filter:

add_action( 'template_redirect', 'cyb_redirect_editors_from_page_template' );
function cyb_redirect_editors_from_page_template() {

    $user = wp_get_current_user();
    if( in_array( "editor", (array) $user->roles ) && is_page_template( "itermediate-template.php" ) ) {
        //Redirect to home, change to fit your needs
        wp_redirect( home_url() );
        exit();
    }

}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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