0

I'm looking for a hand-coded solution to do the following:

  • in some pages (so, not every page) I want to redirect the users NOT logged in to the standard login page: when they login, they are automatically redirected to the page where they were;

  • same for some posts;

I tested many free plugins but I was really not satisfied: some moves the login form in the protected page, some other lets you put a link to the login page.

I'm quite surprised "vanilla WP" doesn't offer a login option like the one I'm looking for: you can set a private post, but then it is really private (not to say invisible), then there's a password solution, but I don't see many website using this idea, it's quite uncommon, imho.

Anyway, I would like to know where I can find information about how to code this behaviour myself: there are lots of resources but it looks like anybody has a "snippet" but nothing is "definitive".

0

You can use the is_user_logged_in() and is_page (array(1,2,3-page id's)) functions to check. After that you can write the redirection.

  • Ok, but what I see online is people cutting and pasting code literally everywhere: I edited single.php, for example, and it worked, but I can't find any robust documentation about what's the best way to code a thing like this – 3000 Apr 10 '17 at 4:34
  • I think below linked question also has your same issue, wordpress.stackexchange.com/questions/56875/… – Tamilvanan N Apr 10 '17 at 7:59
0

Well i have not tested it but you can achieve your point in this way...

  if(!is_user_logged_in() && is_page(1,2,your-page-ids)){

   // wp_redirect(LOGIN_PAGE_URL.'?redirect_to='.get_the_permalink(););

    }

redirect user to login page with a query variable containing link of post or page.

put this code in your child theme's function.php

Hope it will help you...

-1

Put this CODE in your theme's functions.php file:

function admin_default_page() {
    return '/new-url';
}

add_filter('login_redirect', 'admin_default_page');
  • Problem is new-url is dynamic (it can be some posts and some pages) – 3000 Apr 8 '17 at 18:02
  • 2
    please, if you are going to post an answer, try to format the code correctly. In addition answers should have some explanation about how they actually solve the issue, not just a dump of code – Mark Kaplun Apr 8 '17 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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