0

I have been tasked with creating a WordPress site where subscriberes (registered by an admin) are unaware of the back-end.

I also have to make it so that upon navigation to any page, if the user is not logged in they are showen a login form. Once logged in, the page the user was attempting to view should be shown.

I have the following code whcih detects whether or not a user is logged in and shows them a login page, but unfortunately because this requires a redirect there is apparently no way of redirecting the user to where they wanted to go originally.

What I'd like to do is detect whether or not the usre is logged in, and if they are not override the template that would ordinarily be used with a custom login.php template, similar to how the 404 template is displayed.

To do this, I'm guessing I need to somehow hook in to the Template Hierarchy and override the usual template, but I'm unsure of exactly how to achieve this and would apprciate any tips.

Here is what I've currently got, which redirects the user to a specific page if they are not logged in (which is close, but not perfect) -

/**
 * Redirect the user to the 'please login' page if they are not logged in
 */
add_action('wp', 'djg_check_login');
function djg_check_login(){

    $scheme = (is_ssl()) ? 'https' : 'http';

    /** Construct the URL of the page the user is trying to view */
    $url = sprintf(
        '%1$s://%2$s%3$s',
        $scheme,                    /** %1$s - The request shceme (http|https) */
        $_SERVER['SERVER_NAME'],    /** %2$s - The server name */
        $_SERVER['REQUEST_URI']     /** $3$s - The request URI */
    );

    /** List the pages that are authorised for non-logged in users */
    $authorised_pages = array(
        get_site_url().'/login/',
        get_site_url().'/wp-login.php'
    );

    /** Ensure that the user is actually NOT logged in */
    if(!is_user_logged_in()) :

        /** Ensure we are not trying to view an $authorised page (to avoid a loop) */
        if(!in_array($url, $authorised_pages)) :

            /** Redirect the user and exit */
            wp_safe_redirect(get_page_link(7));
            exit;

        endif;

    endif;

}

1 Answer 1

0

This turned out to be far easier than I had feared, using the template_include filter -

/**
 * Override the standard WordPress template with the 'please login' template if the current user is not logged in
 */
add_filter('template_include', 'portfolio_page_template', 99);
function portfolio_page_template($template){

    /** List the pages that are authorised for non-logged in users */
    $authorised_pages = array(
        get_site_url().'/login/',
        get_site_url().'/wp-login.php'
    );

    /** Ensure that the user is actually NOT logged in */
    if(!is_user_logged_in()) :

        /** Ensure we are not trying to view an $authorised page (to avoid a loop) */
        if(!in_array($url, $authorised_pages)) :

            /** Setup the new template */
            $new_template = locate_template(array('login-page.php'));
            if($new_template !== '') :
                return $new_template ;
            endif;

        endif;

    endif;

    return $template;

}

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.