I'm wondering if using is_user_logged_in() to wrap all my theme templates (page.php, index.php, etc) is good way to require login. I'm wanting to use SAML to allow users to login. I tried using Justin Tadlock's plugin "Members" to lock down the site, but it doesn't seem to play nice with SAML.

I have this working right now on a sandbox website (that isn't public right now or I would share). I know I would also need to disable the RSS feeds.

I would like other opinions on whether this is secure (it's simply content that we prefer not to be public, but not the end of the world if it is), whether there are other ways content can be exposed by using this method (besides RSS which I would disable).

Is there a simple plugin that locks a site down and requires login (but allows for making the login screen custom in order to use SAML?)

Below is my code I would use.

<?php if ( is_user_logged_in() ) {

//all of the template code here (header, nav, body content, sidebar, footer)

 } else { 
//a link via SAML to login to the site

} ?>

Just before any of the frontend template loads, WordPress fires an action - template_redirect. You can use this action hook to show SAML login if user isn't logged in. This way, you won't have to put the condition in every template file.

// you can place this code in your theme's functions.php file.

add_action( 'template_redirect', 'wpse_login_required' );

function wpse_login_required() {
    if ( ! is_user_logged_in() ) {
        // a link via SAML to login to the site
        // ....

        // kill the execution

This way, the content will never be public as well.

  • Thanks for the help Shazzad - can you elaborate on how to enter a URL in that PHP? So if I wanted this code to redirect to a URL if the user is logged out.
    – stemlund
    Apr 17 '20 at 20:34
  • To redirect, you can use wp_redirect function.
    – Shazzad
    Apr 17 '20 at 21:21

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.