How to redirect non-logged users requesting for a specific page/URL to another page/URL and display a message like "for members only". I know its quite easy to code using !is_user_logged_in() function but i don't know how to code it because i am a newbie to WordPress. Care to tell me the file to put the code also.

5 Answers 5


Here are 2 examples which you will need to modify slightly to get it working for your specific needs.

add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );

function redirect_non_logged_users_to_specific_page() {

if ( !is_user_logged_in() && is_page('add page slug or ID here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {

wp_redirect( 'http://www.example.dev/page/' ); 

Put this in your child theme functions file, change the page ID or slug and the redirect url.

You could also use code like this:

add_action( 'template_redirect', 'redirect_to_specific_page' );

function redirect_to_specific_page() {

if ( is_page('slug') && ! is_user_logged_in() ) {

wp_redirect( 'http://www.example.dev/your-page/', 301 ); 

You can add the message directly to the page or if you want to display the message for all non logged in users, add it to the code.


  • Thanks for you answer I did like this add_action('template_redirect', 'redirect_to_specific_page'); function redirect_to_specific_page() { if ( is_page('http://mydomain.com/participate') && !is_user_logged_in()) { wp_redirect('http://mydomain.com/login', 301); exit; } } But its not working Commented Jan 31, 2014 at 9:15
  • Use the page i.d or slug here, not the url mydomain.com/participate codex.wordpress.org/Conditional_Tags Commented Jan 31, 2014 at 9:22
  • is_page('participate') Commented Jan 31, 2014 at 9:23

This would be better:

if ( !is_user_logged_in() ) {

// continue as normal for authenticated users

What this does is redirect the user to the login page. Once logged in, the user is redirected back to the secure page they were trying to access initially.

Documentation here:



You can't redirect to a specific page, but every non-logged-in user will be redirected to Log-In Screen.

<?php auth_redirect(); ?>

Wordpress Reference: auth_redirect()

Just to mention another solution.


How can we tell you where to put it if you didn't tell us what and where you want to display it? Whole posts? Pages? Custom parts of pages? Sorry... I guess my crystal ball isn't quite working today.

Since you are, and I quote you: "a newbie to wordpress" you should rather learn, than to ask for direct answer.

As for where you should read the reference 1 link. This will tell you which file you need to put it in.

As for how to do it you should first read reference link 2 and 3.

Overall it should look something like this:

if ( is_user_logged_in() ) {
} else {
    echo 'For members only';

Of course the above code needs to go into a loop. You can build it up as complex or as simple as you want. For example instead of simple text if not logged in you can display whole sign up form for example or - as I would suggest - a divided screen where user can log in (since user can have an account but forgot to sign in) or sign up (if he doesn't have one).

  1. Template Hierarchy
  2. Conditional Tags
  3. The Loop

Added after comments below:

To redirect use header with the wp_login_url - again, check references 1 and 2 below:

if ( is_user_logged_in() ) {
} else {
    header('Location: ' . wp_login_url());


  1. Header - PHP
  2. wp_login_url
  • Actually i don't know the funtction on wordpress that returns the requested URL or permalink. And about the dispaly message, leave it, I want to just want to redirect them when they request for certain URL without being logged. Commented Jan 31, 2014 at 4:14
  • can I do it with $pagenow? Commented Jan 31, 2014 at 4:53
  • @user3187719 edited my original answer and added the solution to redirect. PS. $pagenow? You are trying to kill a fly with a cannon sure it will work, but why so complex? Keep it simple. PS2: Learn PHP and WP Codex.
    – Borek
    Commented Jan 31, 2014 at 15:17

According to this guide: https://vvcares.com/blog/post/wordpress-buddypress-hide-page-from-non-members

How to hiding members page from non logged in users ?

Here the simple step to protect your Wordpress BuddyPress member only pages. Add this simple snippet into your theme's 'functions.php' file. That's it..

The below snippet sample will redirect the visitor if the visitor is not logged in & trying to visit the sensitive BuddyPress pages.

Will redirect non-logged-in users trying to access private content to your front page or home page

//Hide for non-logged-in users (public visitors)
function bp_logged_out_page_template_redirect() {
    if ( ! is_user_logged_in() &&
            is_page( 'members' ) ||
            is_page( 'activity' ) ||
        ) {
        wp_redirect( home_url( 'https://vvcares.com/?buddypressBlogPostRedirect' ) );
add_action( 'template_redirect', 'bp_logged_out_page_template_redirect' );
  • The code would be easier to read with line breaks.
    – Rup
    Commented Nov 15, 2021 at 11:22
  • And I'm not sure how cool it is to just copy & paste content from other websites here, even if you do provide an attribution link. (Unless I suppose you're the one who wrote their blog for them? and you still have copyright to it, i.e. they don't own it, or it's your company I suppose.)
    – Rup
    Commented Nov 15, 2021 at 11:23

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.