Forums

[resolved] Require login on non-WP page (7 posts)

  1. GokuMew2
    Member
    Posted 6 years ago #

    Hi. What code do I need to add to a non-WP page in order to require that WP users be logged in in order to view it? I would like to create a page outside of WP but make it only viewable to those who are logged in. If the user is not logged in, I'd like it to display a message saying they must be registered and then provide a link to register. If anyone knows how to do this, please help me out! Thanks.

  2. Kafkaesqui
    Moderator
    Posted 6 years ago #

    Here's a fairly simple method (stick at the top of any non-WordPress PHP document):

    <?php
    include('wp-blog-header.php');

    global $user_identity;
    if(!$user_identity)
    die('Sorry, you must be <a href="'. get_bloginfo('home') . '/wp-login.php?redirect_to=' . $_SERVER['PHP_SELF'] . '">logged in</a> to view this page.');
    ?>

    Modify the path in the include, if necessary, to the correct location of wp-blog-header.php.

  3. GokuMew2
    Member
    Posted 6 years ago #

    Thanks!

  4. GokuMew2
    Member
    Posted 6 years ago #

    Hrmm.... I tried Kafkaesqui's suggestion and it didn't work. Even when I'm logged in, the content doesn't show. I guess I should mention I need a code that will work on a DIFFERENT domain.

    I tried using...

    <?php
    include('[ABSOLUTE URL]/wp-blog-header.php');

    global $user_identity;
    if(!$user_identity)
    die('Sorry, you must be <a href="[ABSOLUTE URL]/wp-login.php">logged in</a> to view this page.');
    ?>

    If anyone knows how to do this, I'd REALLY appreciate it. Thanks.

  5. moshu
    Member
    Posted 6 years ago #

    I need a code that will work on a DIFFERENT domain.

    That's a HUGE difference. Others may correct me, but I don't think there is something like that.
    Imagine, it would be like: you have to be logged in to this forum to see my site (which is on a different domain).

  6. Kafkaesqui
    Moderator
    Posted 6 years ago #

    No correction from me.

  7. GokuMew2
    Member
    Posted 6 years ago #

    So it can't be done, huh? Poo. =(
    I was hoping there'd be something that just checks if a WP cookie for my domain is present or not, and if it isn't then it gives a message to login. I guess I'll have to think of something else.

    Thanks for your help everyone.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags