I have used this tutorial here () 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.
I have used this tutorial here (https://codex.wordpress/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.
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jan 23, 2017 at 16:00 TerungwaTerungwa 1711 gold badge3 silver badges14 bronze badges3 Answers
Reset to default 1You are following https://codex.wordpress/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.
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(...)
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()){
...........
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745168026a4614752.html
评论列表(0条)