0

I have a member site which maintains a collection of books. I want to have a page on a folder outside of my loop that can see if the user is logged in to the main site, and then display the book viewer. If the user is not logged in to the main site, display a different page. I think I am having some trouble with cookies. I installed the Root Cookie Plugin, to help

This is the code I've put together, what needs to be done better:

<?php
define('WP_USE_THEMES', false); 
if (file_exists('../books/wp-blog-header.php')) {
    require_once('../books/wp-blog-header.php');
} else {
    echo 'ERROR: blog header does not exist';
}
global $current_user;
get_currentuserinfo();
// Start Site
$sitetitle = 'Book Viewer';
include 'head.php';

if ($current_user->ID == 0)
{
    include ('guest.php');
    exit;
}
else
{
    include ('viewer.php');
}
include 'foot.php';
?>

Should I be using wp-load.php instead of wp-blog-header or do I need to do something with wp_signon

4
  • Not sure what all the included files do here and where exactly is the problem. Jun 16, 2014 at 17:22
  • @Chittaranjan Problem is that if I log in to my books website and then visit this page, it does not show that I'm logged in. I'm guessing the cookies do not carry from wp site to an external page and need to write some sort of cookie check, but I do not know where to start or what the best way would be..
    – Rizzo
    Jun 16, 2014 at 18:14
  • Can you check with is_user_logged_in() function? Jun 16, 2014 at 18:26
  • @Chittaranjan I've tried both is_user_logged_in() and current_user_can()
    – Rizzo
    Jun 16, 2014 at 18:34

1 Answer 1

0

this worked with root cookie:

<?php
define('WP_USE_THEMES', false); 
if (file_exists('../books/wp-blog-header.php')) {
    require_once('../books/wp-blog-header.php');
} else {
    echo 'ERROR: blog header does not exist';
}

global $current_user;
get_currentuserinfo();

# Either load WordPress : http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
# or Manually define your url like...

$cookieurl = "mybooksite.com";

define( 'COOKIEHASH', md5( $cookieurl ) );

$cookiename = "wordpress_logged_in_" . COOKIEHASH;

$sitetitle = 'Book Viewer';
include 'head.php';


if (isset($_COOKIE[$cookiename])) {
    include ('viewer.php');
} else {
    include ('guest.php');
}

include 'foot.php';

?>

Not sure if there is a better/safer way to do this.

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.