Support » Developing with WordPress » Content template only for logged in users?

  • I have a CPT called “reports”.

    I only want Reports to be visible to logged-in users. Logged-out users will load a sign-in form instead.

    Is it enough to simply modify the single-reports.php template?

    <?php
    if ( is_user_logged_in() ) {
        the_content ();
    } else {
        wp_login_form ();
    }
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    Yes, though you may want to pass the form the current URL so the user will be redirected back to the page on login. https://developer.www.4f19.cn/reference/functions/wp_login_form/

    Thanks for the input. The codex mentions that the the redirect must be an absolute.

    Is there no way to redirect dynamically?

    For example I have several reports (url.com/report/October and url.com/report/November).

    Obviously, giving the form url.com/october won’t work for the november report.

    • This reply was modified 1 month, 2 weeks ago by oguruma.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    Sure. It’s a variable, after all. Another option is to use the plugin “Peters Login Redirect” and base the redirection on some attribute of the user.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.