• sayze35

    (@sayze35)


    Is there a consensus of WHICH hook should be used to require a user to be logged in before view a certain page?

    If yes, which one? Why is “that one” the correct answer?

    My need:

    home page: the public can view.
    a subscriber page: ONLY a subscriber or admin can view this page.
    an editor page: ONLY an editor or admin can view this page.
    admin pages: ONLY admin can view these pages and any page.

    thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • crstauf

    (@crstauf)

    I think the general consensus would be template_redirect, because that’s the earliest hook that WordPress knows which page is being shown. You could try wp action, but template_redirect is named appropriately for your purpose.

    Thread Starter sayze35

    (@sayze35)

    Thanks crstauf, so the order of calling between template_redirect vs login_redirect is what?

    I’m a little confused about which hook should be used for what purpose? and why?
    Do you know?

    Thank you

    Moderator bcworkz

    (@bcworkz)

    login_redirect only fires during logins. template_redirect fires when any theme template is being used. A list of actions that are roughly in the order of firing can be found here:
    https://codex.wordpress.org/Plugin_API/Action_Reference

    This is not a comprehensive list, some ordering could vary depending upon the request, and not all actions always fire.

    Deciding upon which action to hook to use typically involves examining the source code that executes around the time you want to do something. You’ll then see which hooks are used and their context will indicate how to use each one.

    Thread Starter sayze35

    (@sayze35)

    bcworks…
    that is the BEST page on this site 🙂 thank you for linking it!

    question: why are some hooks red color, what does the color mean?
    why isn’t login_redirect ON that page?

    PS. I have debug proof that login_redirect is fired BEFORE or AS the wp-login.php loads, which makes zero sense. So when you say, “login_redirect only fires during logins”, I agree it is fired after the submit button is clicked, HOWEVER, it is also fired BEFORE the submit button is clicked.

    Moderator bcworkz

    (@bcworkz)

    Red links in the old Codex don’t lead to anything useful. They represent placeholder links where contributors are invited to create new content, but no one ever did. The Codex is no longer actively maintained, but it still contains useful bits of information. It has mostly been replaced by “HelpHub” and the Code Reference.

    login_redirect is not on the list perhaps because it does not fire on normal WP requests? IDK, the real answer is no one bothered to add it. The Codex was a wiki style resource built by volunteer community contributions.

    You are correct, login_redirect fires when ever the file is loaded. It sets a variable that could be used to redirect users after they log in. The variable is always set whether it is used or not. The variable is only used after login. Usage and filter assignment are two independent processes. It’s not the purpose of filters to indicate a particular event happened. They are to alter values that WP uses. Action hooks OTOH indicate a specific event happened. You’re trying to think of a filter hook as an action and that is simply not what it is for.

    If you want to capture the login action, you have the wrong hook. Maybe the ‘wp_login’ action is what you are looking for?

    Thread Starter sayze35

    (@sayze35)

    THANK YOU bcworkz…
    this type of information is GOLD. extremely needed (can’t be found anywhere?)

    even worse, if you did find same/similar information… you would never know if it old, can be trusted, or some random person was just talking BS.

    thank you!!!

    PS. So why doesn’t WP Core provide details/information like you did?

    Moderator bcworkz

    (@bcworkz)

    So forum volunteers have something to do? 😛

    Documentation cannot anticipate every single need and address it. There’s not enough resource to do so for one thing. And it’s difficult to know ahead of time what everyone’s need would be. Your need is obvious to you, maybe much less obvious to others. Docs can address the needs of most, but sadly fringe needs are left to their own devices.

    At least WP is open source. The answer to everything WP could be deduced from source code. Unfortunately prying such answers from source code is a very specialized skill that very few have. Thankfully there is a community of knowledgeable users willing to help each other out in these forums.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘what hook should be used to restrict page access based on user’ is closed to new replies.