0

I have used this tutorial here (https://codex.wordpress.org/Integrating_WordPress_with_Your_Website) as a guide to set up non-wordpress pages to access some wordpress resources on non-wordpress pages. Eg I can view posts with the code snippet below on non-wordpress page.

<?php
require('/the/path/to/your/wp-blog-header.php');
?>

<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>    
<?php the_excerpt(); ?> 
<?php
endforeach;
?>

In addition, I need to be able to detect if a user is logged into wordpress on the non-wordpress page. I am using this code below for that:

if(is_user_logged_in()){
$current_user = wp_get_current_user();
/**
 * @example Safe usage: $current_user = wp_get_current_user();
 * if ( !($current_user instanceof WP_User) )
 *     return;
 */
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
echo get_avatar( $current_user->user_email, 32 ). '<br />';
echo '<hr />';  

}
else{
 echo 'Not logged-in';
}

Even though I am logged into wordpress, when I navigate to the non-wordpress page, I see wordpress posts listed but, the is_user_logged_in() function returns false and prints 'Not logged-in'!!!

What is recommended here (Wordpress check if user is logged in from non wordpress page) does not work, and gives the same false.

Why is that so, or Am I missing something? I need guidance to resolve this.

3 Answers 3

1

You are following https://codex.wordpress.org/Integrating_WordPress_with_Your_Website which means your main site is not basically WordPress. You use some features of WordPress like post publish, displaying etc. But, main site is not WordPress. For that reason, you can't detect a user is logged in or not in WordPress.

2
  • this question wordpress.stackexchange.com/questions/41079/…; apparently sought to address my question and was marked up as been useful in resolving this, though I could not replicate the same outcome in my case. So when you say It is not possible, I wonder who accepted and marked up the response on that thread? What did they do differently?
    – Terungwa
    Jan 23, 2017 at 17:11
  • I saw thttp://wordpress.stackexchange.com/questions/41079/wordpress-check-if-user-is-logged-in-from-non-wordpress-page. But codex.wordpress.org/Integrating_WordPress_with_Your_Website makes sense that whole WordPress system is only exists in subdirectory (myexample.com/blog) as it describe at introduction. However, it is possible to get database from other website. But database can't indicate log in/out. It need whole system. Jan 24, 2017 at 5:04
0

you should try this:

require_once(ABSPATH.'wp-includes/pluggable.php');
if(is_user_logged_in()){
     ...........
}

also, it's better to use:

require('./path/to/wp-load.php');   //instead of wp-blog-header.php

otherwise, you have to use: define( 'WP_USE_THEMES', false ); before require(...)

1
  • require('./blog/wp-load.php'); require_once('./blog/wp-includes/pluggable.php'); if(is_user_logged_in()){ echo 'Inside......'; }. This solution failed as well. @Chip Bennett; @T.Todua. @tosco. Has anything changed within the wordpress core, that would prevent the replication of outcomes claimed to have being recorded in the past here (wordpress.stackexchange.com/questions/41079/…)?
    – Terungwa
    Jan 24, 2017 at 7:38
0

In case you had not resolved this, the following works for me.

require_once(ABSPATH.'wp-includes/pluggable.php');

wp_setcookie($user_login, $user_pass, false, '', '', $cookieuser);

if(is_user_logged_in()){
     ...........
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.