0

I have a multiple author wordpress blog. I want to force redirected to login page, if any visitor view or access to specific pages. example : If user visit to specific page like, www.mysite.com/author-terms/, thereafter redirect to www.mysite.com/wp-admin.php/. Thanks in advance.

We have created a page in our blog called 'Instructions for Contributors'. If any visitor copied the link to this page ('Instructions for Contributors') and opens it directly in the browser. So it will open, and anyone can read the instructions given in it. Want to limit it to the blog's contributors. If a visitor tries to open the 'Instructions for Writers' page link directly, then it is redirected directly to the login page.

1 Answer 1

0

You can check, if a user is logged in with is_user_logged_in() and for certain page with is_page(). If conditions are met, redirect the user with wp_redirect().

function redirect_non_loggedin_user() {
  // if user is on certain page and not logged in
  if ( ! is_user_logged_in() && is_page('instructions-for-contributors') ) {
    // redirect to login page and back after login
    wp_redirect( wp_login_url( get_permalink() ) );
    exit;    
  }  
}
add_action('template_redirect', 'redirect_non_loggedin_user');
1
  • Thanks a lot @antti-koskinen sir, for your guide. but we have put this code at child theme functon.php, but not worked. Because we want to user redirect to login to view this page. 'code'function redirect_non_loggedin_user() { // if user is on certain page and not logged in if ( ! is_user_logged_in() && is_page(exam2jobs.prachidigital.in/frontend-dashboard/') ) { // redirect to login page and back after login wp_redirect( wp_login_url( get_permalink() ) ); exit; } } add_action('template_redirect', 'redirect_non_loggedin_user'); thanks again. Commented Mar 17, 2020 at 7:39

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.