Restricting page to logged in users only .

Hi Guys,

I have 'coursepress pro' plugin , and using 'Theme my login' to manage user profile / registration / login ( Although coursepress has its login mgnt also) . Now there is a page -'Goals' , I want that only logged in user can access this page (Otherwise redirect to login page) . Which approach will be best to do this in this scenario ?


  • Patrick

    Hi there @Raitis

    I hope you're well today!

    Any membership plugin can handle that for you. But that would be a bit overkill if you only have one or two pages you want to restrict to logged-in users only.

    There are 2 easy solutions to that:

    - Try the Eyes Only User Access Shortcodes plugin. You can wrap content in shortcodes to restrict/grant access depending on usernames, user roles, capabilities, custom groups, or logged-in status. While there is no redirect option in that plugin, you could simply include a link to your login page for not-logged-in users.

    - Or you could use a simple function to your theme's functions.php file to redirect not-logged-in users to your login page, like below. Replace 'Goals' with the name or slug of your Goals page, and replace /login with the slug of your login page.

    if (!(is_user_logged_in()) && is_page('Goals')){
    	wp_redirect( home_url() . '/login' );

    I hope this helps! And thanks for being a member :slight_smile:

  • Ash

    Hello @Raitis

    I hope you are well today.

    When you edit the Goals page in admin, the URL looks like: /wp-admin/post.php?post=781&action=edit and here 781 is the ID. Find the ID for Goals page. Then try the following code:

    add_action( 'template_redirect', 'protect_goal_page' );
    function protect_goal_page() {
    	global $post;
    	if( $post->ID == 781 ){
    		if( ! is_user_logged_in() ){
    			wp_redirect( home_url() . '/login' );

    Change the ID to match Goal page ID.

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.