How to set is_user_logged_in() true in Wordpress - php

I am using wp_signon from inside a plugin and inside a shortcode function to login a user. To clarify, the short code is being called from a page content. The wp_signon function successfully returns the user info. But is_user_logged_in() is returning false. Why it's happening or how can I set is_user_logged_in() true so that user stays login?
ob_start();
$user_data = array();
$user_data['user_login'] = $username;
$user_data['user_password'] = $password;
$user_data['remember'] = $remember;
$user = wp_signon( $user_data, false );
ob_get_clean();
if ( is_wp_error($user) ) {
$err = $user->get_error_message();
} else {
$_SESSION['fc_user']=$user->ID;
var_dump($user); // gives the user info
var_dump(is_user_logged_in()); // gives false
}

The function wp_signon
sends headers to the page. It must be run before any content is returned.
So running it inside a shortcode (like you specified in your comment) is too late: headers have already been sent.
In order to achieve what you want you should divide your code in two parts, to be run at different time, something like this:
add_action( 'after_setup_theme', function() {
// run this only on the page you want
if( !is_page('the_page_you_want') )
return;
$user_data = array();
$user_data['user_login'] = $username;
$user_data['user_password'] = $password;
$user_data['remember'] = $remember;
$user = wp_signon( $user_data, false );
if ( is_wp_error($user) ) {
$GLOBALS['user_error_message'] = $user->get_error_message();
} else {
$_SESSION['fc_user'] = $user->ID;
}
} );
add_shortcode( 'your_shortcode', function( $atts ) {
if( is_user_logged_in() ) {
// get user info
return print_r( get_userdata(get_current_user_id()), 1 );
} elseif( isset($GLOBALS['user_error_message']) ) {
// show error
return print_r( $GLOBALS['user_error_message'], 1 );
}
} );

I just ran into a similar issue with an MVC Wordpress plugin.
For me the solution was to include
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-includes/pluggable.php');
to have access to the core function via a plugin.

Related

Check if user had autologin & if so, logout

With Wordpress, I'm trying to create a function which autologins users, but upon visiting certain pages also performs an autologout if the initial login was obtained via autologin.
I've written the following code but I guess my boolean check for the autologin boolean does not work.
/* AUTO LOGIN / LOGOUT */
function autologinout() {
global $wp;
// Autologin
if( isset($_GET['username']) ) {
$user = get_user_by('login', $_GET['username']);
// Redirect URL //
if ( !is_wp_error( $user ) ) {
if ( in_array( 'customer', (array) $user->roles ) ) {
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
wp_redirect( '/lovelists/toon-lovelist/' );
$autologin = true;
exit();
}
}
}
// Autologout
$path = $_SERVER['REQUEST_URI'];
if ( is_user_logged_in() && $autologin = true && ( $path == '/lovelists/maak-lovelist/' || $path == '/lovelists/login/' ) ) {
wp_clear_auth_cookie(); // so you don't get the cache error
wp_logout(); // this will logout user
$autologin = false;
}
}
add_action( 'init', 'autologinout' );
Any idea what I'm doing wrong?

Authenticate out WP to get current user

I am trying to get user detail out side wordpress file (But same server) and for that I am using this code
<?php
define( 'WP_USE_THEMES', false ); // Do not use the theme files
define( 'COOKIE_DOMAIN', false ); // Do not append verify the domain to the cookie
define( 'DISABLE_WP_CRON', true ); // We don't want extra things running...
//$_SERVER['HTTP_HOST'] = ""; // For multi-site ONLY. Provide the
// URL/blog you want to auth to.
// Path (absolute or relative) to where your WP core is running
require("/var/www/yourdomain.com/htdocs/wp-load.php");
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
} else {
$creds = array();
// If you're not logged in, you should display a form or something
// Use the submited information to populate the user_login & user_password
$creds['user_login'] = "";
$creds['user_password'] = "";
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
echo $user->get_error_message();
} else {
wp_set_auth_cookie( $user->ID, true );
}
}
if ( !is_wp_error( $user ) ) {
// Success! We're logged in! Now let's test against EDD's purchase of my "service."
if ( edd_has_user_purchased( $user->ID, '294', NULL ) ) {
echo "Purchased the Services and is active.";
} else {
echo "Not Purchased";
}
}
?>
but it doesn't worked, I am creating custom dashboard out wp, which user wp info, as back-end. So please tell me what wrong am i doing? Any help is highly appreciated.
Since you are using easy-digital-downloads be sure it was included
if ( !function_exists( 'edd_has_user_purchased' ) ) {
require_once 'path-to-plugin/user-functions.php';
}
In your if ( !is_wp_error( $user ) ) statement you can use
echo "<p>ID: ".$user->ID;
echo "<p>Name: ".$user->data->display_name;
echo "<p>Login: ".$user->data->user_login;
echo "<p>Email: ".$user->data->user_email;
echo "<p>URL: ".$user->data->user_url;
echo "<p>Registered: ".$user->data->user_registered;
to show user wp info.
Try
print_r($user);
to overview all accessible fields

while use wp_signon( $creds, false ) get the error

I want to login user automatically when user is activated in multisite.
but when I am trying to use wp_signon() it response with the error
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\krosswall\wp-includes\class.wp-styles.php:127) in C:\xampp\htdocs\krosswall\wp-includes\pluggable.php on line 955
I am trying to do this
function kw_activate_blogs( $user_id, $password, $meta ) {
add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
$user = new WP_User( (int) $user_id );
$creds = array();
$creds['user_login'] = $user->user_login;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
wp_set_current_user($user->ID);
if ( is_wp_error($user) ) {
echo $user->get_error_message();
} else {
// safe redirect to actually login the user - otherwise they would need to manually refresh the page
// PLUS: this clears the activation confirmation page with the plain text password printed on screen
//wp_safe_redirect( get_home_url() );
exit;
}
}
add_action( 'wpmu_activate_user', 'kw_activate_blogs', 10, 3 );
I added this code in functions.php file. Please help me to solve it.
You can do this:
function kw_activate_blogs($user_id, $password, $meta)
{
$result=add_user_to_blog('2', $user_id, get_site_option('default_user_role', 'subscriber'));
if (!is_wp_error($result))
{
wp_set_current_user($user_id);
if (wp_validate_auth_cookie() == FALSE)
{
wp_set_auth_cookie($user_id, true, false);
}
}
else
{
//do something here on error
}
}
You don't need their password in this case. You can also remove 2nd and 3rd arguments if you don't need $meta and change add_action to add_action( 'wpmu_activate_user', 'kw_activate_blogs', 10);

direct users with set_transient

i wanna redirect users every 12hours to another page
and want use header(location:url) and set_transient
i use this codes
$transient = get_transient( 'name_my_transient' );
if ( empty( $transient ) ){
function redirect() {
$data = 'redirected';
return $data;
header('location: http://www.google.com');
}
add_action('wp_head', 'redirect');
set_transient('name_my_transient', $data, 60*60*12 );
}
if i remove return $data it always redirect users
Your reflexion is good but i think you had made some mistakes on your code organization, try with this :
<?php
// Call your function with a top priority
add_action( 'wp_head', 'redirect', 1 );
function redirect() {
// Get the transien
$transient = get_transient( 'name_my_transient' );
// If the data do not exist, set it and redirect user
if ( $transient === false ) {
// Set the transient for 12 hours
set_transient('name_my_transient', 'redirected', 12 * HOUR_IN_SECONDS );
// Redirect the user with wp_redirect
wp_redirect( 'http://www.google.com' ); exit;
}
}
?>

maintenance mode function for multiple users - php array not working?

I have created a maintenance mode function, but I can only get it to work for one username.
get_currentuserinfo();
global $current_user;
// MAINTAINANCE MODE
function website_site_maintenance() {
global $current_user;
if ( 'josh' != $current_user->user_login ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
}
add_action('get_header', 'website_site_maintenance');
add_action('admin_init', 'website_site_maintenance');
// CUSTOM LOGIN MESSAGES
function website_my_login_message() {
if( $_GET['mode'] == 'maintenance' ){
$message = '<p class="message"><b>Site undergoing maintenance.</b></p>';
return $message;
}
}
add_filter('login_message', 'website_my_login_message');
this above function works, but I want to add more maintenance users.
So i've tried this but it does not work...
// MAINTAINANCE MODE
function website_site_maintenance() {
global $current_user;
$maintenance_users = array('josh','george','bob');
if ( $maintenance_users != $current_user->user_login ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
}
I simply tried to add the users into an array.
$maintenance_users = array('josh','george','bob');
But this doesn't seem to work as it should.
Instead of using
if ( $maintenance_users != $current_user->user_login ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
use
if ( !in_array($current_user->user_login, $maintenance_users) ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
The problem you're having is that you're comparing an array to a string, and this is not going to work. In the "correct" version you'll be checking whether the username of the currently logged in user is in the array $maintenance_users you specified. Hope that helps.

Categories