I created function for loging in users on frontend using this example: https://gist.github.com/iandunn/8162246
After user logs in is_user_logged_in() function returns true only inside this function where I placed code for login part.
How do I login users globally?
This is my code:
function programmatic_login( $username ) {
if ( is_user_logged_in() ) {
wp_logout();
}
add_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
$user = wp_signon( array( 'user_login' => $username ) );
remove_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
if ( is_a( $user, 'WP_User' ) ) {
$user_id = $user->ID;
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
if ( is_user_logged_in() ) {
return true;
}
}
return false;
}
function allow_programmatic_login( $user, $username, $password ) {
return get_user_by( 'login', $username );
}
function process_login(){
// this comes from login form
$username = $_POST["login_username"];
programmatic_login( $username );
// it returns true only here, on any other function it returns false
if(is_user_logged_in()){
echo "ok";
}else{
echo "not ok";
}
}
This is one example where I try to check if user is logged in, outside previous function:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
$loginPage = get_page_by_title("Login");
$registerPage = get_page_by_title("Register");
if(is_user_logged_in()){
$items .= "<li><a href='" . wp_logout_url('index.php') . "' title='Logout'>Logout</a></li>";
}else{
$items .= "<li><a href='". site_url() . '/' . '?page_id=' . $loginPage->ID ."'>Login</a></li><li><a a href='". site_url() . '/' . '?page_id=' . $registerPage->ID ."'>Register</a></li>";
}
return $items;
}
On codex wp_set_current_user there is an example to set the current user and log them in.
$user_id = 12345;
$user = get_user_by( 'id', $user_id );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
The simplest fix is to use wp_login_form()
see reference: http://codex.wordpress.org/Function_Reference/wp_login_form
If i understand correctly this should handle everything you want.
This will set the auth cookie and can redirect to the page you want, the login works globally
Related
Hi I use this code to redirect users on login based on the user role.
I want to modify it to use redirect based on the user id.
How could i do this?
Thanks
function my_login_redirect( $url, $request, $user ){
if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if( $user->has_cap( 'administrator' ) ) {
$url = home_url('mypage.html');
} else {
$url = home_url('/index.php');
}
}
return $url;}add_filter('login_redirect', 'my_login_redirect', 10, 3 );
Your $user object already contains the ID.
Your code would then look like this if you want to check for user with id 79:
function my_login_redirect( $url, $request, $user ){
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if ( $user->ID == 79 ) {
$url = home_url( '/mypage.html' );
} else {
$url = home_url( '/index.php' );
}
}
return $url;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Put the above in your functions.php. Tested and works.
I'm trying to disable admin login from front end of my wordPress site but my backend login also gets disable both login shows admin cannot login here
<?php
add_filter( 'authenticate', 'wp_admin_prevent_authentication', 30, 3 );
function wp_admin_prevent_authentication( $user, $username, $password ) {
if ( $user instanceof WP_User && is_page( 'my-account' ) ) {
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
};
};
return $user;
};
Not sure you can use is_page() inside authenticate but you can get page name using $_SERVER['REQUEST_URI']. check the below code.
function wp_admin_prevent_authentication( $user, $username, $password ) {
$url = explode( '/', rtrim( $_SERVER['REQUEST_URI'], '/') );
// If in My account dashboard page
if( $user instanceof WP_User && end( $url ) == 'my-account' ){
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
}
}
return $user;
}
add_filter( 'authenticate', 'wp_admin_prevent_authentication', 30, 3 );
Tested and works.
You can try with the below code, maybe it will work for you.
function wpum_admin_prevent_authentication( $user, $username, $password ) {
if ( $user instanceof WP_User && is_page( wpum_get_core_page_id( 'login' ) ) ) {
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
}
}
return $user;
}
add_filter( 'authenticate', 'wpum_admin_prevent_authentication', 30, 3 );
How can I automatically assign a new email to a user in WordPress AND restrict any further update after the first login ?
For example setting the email to the following structure:
{user_id}#example.com
I've tried a few things but couldn't figure it out, any help is appreciated.
I wrote this code, but only users whose email has been registered, their email has been edited, other people who have not registered email, no email has been registered for them. How do I solve this?
my code:
add_action ('admin_head','set_auto_mail');
function set_auto_mail() {
$users = get_users(array('fields'=>'all'));
foreach($users as $user){
$user = get_userdata($user->ID);
if ( in_array( 'subscriber', (array) $user->roles ) ) {
$mail = ($user->ID)."#example.com";
if($mail!=' ') wp_update_user( array ('ID' => $user->ID, 'user_email' => $mail) );
else wp_update_user( array ('ID' => $user->ID, 'user_email' => $user->user_email) );
if($user->user_email == '')
wp_update_user( array ('ID' => $user->ID, 'user_email' => $user->user_email) );
}
}
}
This should do exactly what you wanted.
We set a new user meta data called is_new_user which we will use later on to detect if this is the first ever login for a specific user.
We set a hook to update the user_email on first time login and we update is_new_user to specify that this isn't a new user anymore. Like that we prevent permanent update on login.
Then we fire up the necessary functions to disable email updates. Visually and from the backend for all non-new users.
I'm using first_name, las_name and ID for the email structure. Output:
$first_name.$last_name$ID#bogus.com
<?php
/**
* Adds meta data is_new_user to a user, which let us detect new users on first login.
*
* Fires immediately after a new user is registered.
*
* #link https://developer.wordpress.org/reference/hooks/wp_login/
*/
add_action( 'user_register', 'wpso66983605_add_user_meta_is_new_user' );
function wpso66983605_add_user_meta_is_new_user( $user_id ) {
add_user_meta( $user_id, 'is_new_user', 1, true );
};
/**
* Update user email on first login.
*
* Fires after the user has successfully logged in.
*
* #link https://developer.wordpress.org/reference/hooks/wp_login/
*/
add_action( 'wp_login', 'wpso66983605_set_user_email_on_first_login', 10, 2 );
function wpso66983605_set_user_email_on_first_login( $user_login, $user ) {
if ( ! get_user_meta( $user->ID, 'is_new_user', true ) ) {
return;
};
if ( get_user_meta( $user->ID, 'is_new_user', true ) ) {
update_user_meta( $user->ID, 'is_new_user', 0, 1 );
$userdata = array (
'ID' => $user->ID,
'user_email' => sanitize_email( sanitize_title_with_dashes( $user->first_name . '.' . $user->last_name . $user->ID) . '#bogus.com' ),
);
wp_update_user( $userdata );
};
};
/**
* Prevent users from changing their email address
*
* Modified to ONLY fire when a user is considered as non-new user (user has already made at least 1 login).
*
* #link https://wordpress.stackexchange.com/a/363376/190376
*/
if ( ! class_exists( 'DisableMailChange' ) ) {
class DisableMailChange {
public function __construct() {
add_action( 'personal_options_update', array( $this, 'disable_mail_change_BACKEND' ), 5 );
add_action( 'show_user_profile', array( $this, 'disable_mail_change_HTML' ) );
}
public function disable_mail_change_BACKEND( $user_id ) {
if ( ! get_user_meta( get_current_user_id(), 'is_new_user', true ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
$user = get_user_by( 'id', $user_id );
$_POST['email'] = $user->user_email;
};
};
}
public function disable_mail_change_HTML( $user ) {
if ( ! get_user_meta( get_current_user_id(), 'is_new_user', true ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
echo '<script>document.getElementById("email").setAttribute("disabled","disabled");</script>';
};
};
}
};
new DisableMailChange();
};
I am trying to change the product edit url which is created from this plugins function:
function dokan_edit_product_url( $product ) {
if ( ! $product instanceof WC_Product ) {
$product = wc_get_product( $product );
}
if ( ! $product ) {
return false;
}
if ( 'publish' === $product->get_status() ) {
$url = trailingslashit( get_permalink( $product->get_id() ) ) . 'edit/';
} else {
$url = add_query_arg(
[
'product_id' => $product->get_id(),
'action' => 'edit',
],
dokan_get_navigation_url( 'products' )
);
}
return apply_filters( 'dokan_get_edit_product_url', $url, $product );
}
You can see it has a apply_filters available. So I am trying to create a filter to modify the URL to be: example.com/edit-product/product-id
add_filter( 'dokan_get_edit_product_url', function() {
// I need to get the PRODUCT ID here somehow.
$url = 'example.com/dashboard/edit-product' . $product_id;
return $url;
} );
How can I get the product ID in my filter? I need to grab the product ID and attach it to: example.com/dashboard/edit-product/ + product_id
Here is one attempt:
add_filter( 'dokan_get_edit_product_url', function( $product ) {
$product = wc_get_product( $product );
var_dump($product);
return $url;
} );
Result:
/app/filters.php:175:boolean false
Following a suggestion in the comments from #howard-e, I was simply missing global $product;
Here is my full filter:
add_filter( 'dokan_get_edit_product_url', function( $product ) {
global $product;
$url = dokan_get_navigation_url() . 'edit-product?product_id=' . $product->get_id();
return $url;
} );
Obviously the format of the $url is unique to my set up, but worth posting the full code anyway.
I am working on login template...but I want to make login with username as well as email.I got code with for that but it is not worked,it is as follow:
function login_with_email_address($username) {
$user = get_user_by('email',$username);
if(!empty($user->user_login))
$username = $user->user_login;
return $username;
}
add_action('wp_authenticate','login_with_email_address',10,1);
I already put action in 'authenticate' for email verification,it is as follow:
function check_user_status($user, $username, $password) {
if (in_array( 'subscriber', (array) $user->roles ) ) {
if (get_user_meta($user->ID, 'confirm_mail', true) == 1) { return $user; }
else{ return new WP_Error('Account Not Active.'); }
}
else{ return $user; }
}
add_filter('authenticate','check_user_status', 30, 3);
Try this
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
add_filter( 'authenticate', 'tcb_authenticate_username_password', 20, 3 );
function tcb_authenticate_username_password( $user, $username, $password ) {
if ( ! empty( $username ) && is_email( $username ) ) :
if ( $user = get_user_by_email( $username ) )
$username = $user->user_login;
endif;
return wp_authenticate_username_password( null, $username, $password );
}