I have created a custom user role in my functions.php file. I am creating a new page template for a page on my website that I would like to restrict by role(not by capabilities). Basically I would like to make it so that if a user with this new role or any editor/administrator role accesses the page they will see the content, otherwise it will display the default Wordpress login form. I also want it to send them back to this specific page after logging in. Here's the code I am using so far, it seems to be mostly working except that the redirect doesn't work. I also tried using wp_login_form()
in place of the redirect, but that displays an unstyled form rather than what I would see by accessing wp-login
:
<?php
/*
* Template Name: Restricted content
* Description: Restricted content template
*/
get_header(); ?>
<?php if( current_user_can('custom-role') || current_user_can('administrator') || current_user_can('editor')) { ?>
<div>
Restricted content displayed here
</div>
<?php get_footer(); ?>
<?php
} else {
wp_redirect(wp_login_url());
}
?>
get_header
.