• Resolved Mew

    (@gokumew2)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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.

    Thread Starter Mew

    (@gokumew2)

    Thanks!

    Thread Starter Mew

    (@gokumew2)

    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.

    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).

    No correction from me.

    Thread Starter Mew

    (@gokumew2)

    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Require login on non-WP page’ is closed to new replies.