Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Daan Kortenbach

    (@daankortenbach)

    Hi hybridixstudio,

    Compare the original code:

    add_action( 'parse_request', 'dmk_redirect_to_login_if_not_logged_in', 1 );
    /**
     * Redirects a user to the login page if not logged in.
     *
     * @author Daan Kortenbach
     */
    function dmk_redirect_to_login_if_not_logged_in() {
    	is_user_logged_in() || auth_redirect();
    }

    With this adjusted code with added conditionals:

    add_action( 'parse_request', 'dmk_redirect_to_login_if_not_logged_in', 1 );
    /**
     * Redirects a user to the login page if not logged in.
     * Added is_page() conditional
     *
     * @author Daan Kortenbach
     */
    function dmk_redirect_to_login_if_not_logged_in() {
    
    	// If on this page, do not redirect
    	if ( is_page( 'change-me-to-page-slug' ) )
    		return;
    
    	// If on this post, do not redirect
    	if ( is_single( 'change-me-to-post-slug' ) )
    		return;
    
    	// Conditional(s) above were false, redirect
    	is_user_logged_in() || auth_redirect();
    }

    You can fork the plugin at Github (https://github.com/daankortenbach/redirect-to-login-if-not-logged-in) and change the code to your liking.

    Also, here is a gist with the changed code: https://gist.github.com/daankortenbach/9428467

    Thread Starter hybridixstudio

    (@hybridixstudio)

    That’s awsome ?? thank you very much dude.

    Plugin Author Daan Kortenbach

    (@daankortenbach)

    You’re welcome, let me know if it worked for you.

    Jonbie

    (@jonbie)

    Hi,

    Great plugin by the way. I am using it to redirect users to my login page but i would like them to be able to register if they are not logged in by clicking on the register link. Unfortunately i can get the above code to work for me.

    ny help would be greatly appreciated!

    thanks,

    Jonbie

    Plugin Author Daan Kortenbach

    (@daankortenbach)

    Sorry, Jonbie. I haven’t had time to set you up with something. Multiple people are asking for a bit of configurability. Maybe in the future I’ll add them.

    No problem Dan, thanks for getting back to me.

    would it be possible to do in the child themes function.php to avoid updates causing issues etc?

    Plugin Author Daan Kortenbach

    (@daankortenbach)

    It is possible but it would be better to fork the plugin and change the functionality in the new plugin. Plugins you can easily disable if something goes wrong, not something you would want to do with your theme.

    The source code is here:
    https://github.com/daankortenbach/redirect-to-login-if-not-logged-in

    Just create a Github account (if you do not already have one) and fork it.

    What could I change to the code if I only need to have one page blocked. I just have a single page with member-only information I would like to require a login for. But the rest of the website I would like to remain open to the public. Thanks for the help ??

    Plugin Author Daan Kortenbach

    (@daankortenbach)

    Just set a password on that page in the post/page edit screen. No need for this plugin.

    Can you tell me what am I doing wrong in the following code if I want to block only one page. (I dont want to use the option to set the password from the page edit screen)

    function dmk_redirect_to_login_if_not_logged_in()
    {if ( (!is_user_logged_in()) && ( is_page( id ) ) )
    auth_redirect();
    }

    Plugin Author Daan Kortenbach

    (@daankortenbach)

    That is not going to work. The action is on parse_request, no query has been run and the page id is unknown. You need to create a new function and hook it to the template_redirect hook.

    It will probably look something like this (I’m doing this from my head, untested and may contains bugs):

    add_action( 'template_redirect', 'your_redirect_function' );
    
    function your_redirect_function() {
        // Set page id
        $page_id = 'replace_with_your_page_id';
    
        // Do the redirect if we're on the correct page
        if ( is_page( $page_id ) ) {
    
            // Redirect to the login url and set the redirect for the login url to current page
            wp_redirect( wp_login_url( get_permalink( $page_id ) ) );
            exit;
        }
    }

    Hi Daan,

    Your plugin does exactly what i need, except the fact that a nog logged-in user now gets redirected every time. Is there a way to only make the user redirect when entering a direct link to a page that is for users only? When they click that certain link now, they get the message that the page does not exist, which is a bit confusing, because after logging in en clicking the link, they do get to the post.

    I’m just looking for a way to notify the user they are viewing a post that is for registered users only en get the login screen automaticly.

    Any idea how to achieve that?

    Thanx!

    Plugin Author Daan Kortenbach

    (@daankortenbach)

    Hi bvstrien,

    You can’t show a page (with a message) and at the same time show the login page. It’s a paradox. You might be better of with a membership plugin for your needs.

    Daan

    Hi Daan,

    Thanx for your reply. I think i understand that, but isn’t it possible to only redirect to the login screen when a visitor is viewing a page for registered users only? This way the public pages are still viewable, but redirection only occurs when trying to view a page for registered users only?

    I’m not a programmer, but is it possible to do something like:

    If viewing public page -> show page, if viewing registered page -> redirect to login screen??

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘how could I allow pages to not redirect?’ is closed to new replies.