Will get_current_user_id() in wordpress fire a database request? - wordpress

I am using get_current_user_id() and wp_get_current_user() multiple times in my plugin. Are these functions going to send a database request everytime I use them or is the user object of the current user always available (cause wordpress requests it anyway on initializing)?
Would it be better to declare a global var at the start of my script with the current user info?
<?php
global $currentUser;
$currentUser = wp_get_current_user();
function function1() {
global $currentUser;
echo $currentUser->ID;
}
?>

No it will not. It will read the wordpress global variable $current_user

Related

how do you get current userid in wordpress

Hi have a simple php file in my wordpress site, where I need the current userid
I have tried the following, but keep getting
Call to undefined function get_current_user_id()
<?php
include '../../mydbfile.php';
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
You need to include wp-load.php to call the global $wpdb
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
This has been tested and works.

Wordpress - Form to match registered users

I need to create a form on wordpress for registered users.
Users don't have to reach backend but they land on a page with just a form where they write their email and submit.
If the mail matches with the mail registered as user they'll be redirected to the next page.
If there's no matching mail on user list they just receive an error to try again.
It's for a kind of browser game...
Thanks
For checking whether an email exists or not, you can use the email_exists() function provided by WordPress.
Here's the example:
$email = 'myemail#example.com';
$exists = email_exists( $email );
if ( $exists ) {
echo "That E-mail is registered to user number " . $exists;
} else {
echo "That E-mail doesn't belong to any registered users on this site";
}
You can use it to define your logic.
And for redirection, WordPress has a function called wp_redirect()
Here's an exammple:
wp_redirect( 'http://www.mynewurl.com/blah/' ); exit;
Make sure to call exit just after calling that function.
And if you are having trouble figuring out how to know whether the user is actually a logged in user or not, you can use the is_user_logged_in() function.
Here's an example:
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, guest!';
}
Now you got all the functions needed to complete your task! Just make use of these functions properly based on your logic and you will be able to complete it!

Kill previous session if the same user logged in again wordpress

Is there an option to destroy all other user session after a user logs in. I found an option for destroying sessions as
// get all sessions for user with ID $user_id
$sessions = WP_Session_Tokens::get_instance( $user->ID );
// we have got the sessions, destroy them all!
$sessions->destroy_all();
It can use on authenticating a user, but I am using a social login plugin for authenticating users, so need to hack the plugin for achieving the same. Can you help me figure out an option to destroy all previous sessions on or after "wp_login" action.
Resolved it!!.
destroy_all() will destroy all the sessions so when we are use this in wp_login hook it will destroy the current session too, instead we can use destroy_others() function.
The final code shown below
function your_function( $user,$user_id) {
$sessions = WP_Session_Tokens::get_instance( get_current_user_id() );
$token = wp_get_session_token();
$sessions->destroy_others( $token );
}
add_action('wp_login', 'your_function',10,2);
You could try to hook into the wp_login action. Could be done in the plugin or in your functions.php
<?php
function your_function() {
// your code
}
add_action('wp_login', 'your_function');
?>
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_login

wordpress programmatically logout everywhere else

How can I logout from every where else this place in wordpress? [not wp_logout() because it destroys current session only]
I used this function but it did not work:
WP_User_Meta_Session_Tokens::destroy_other_sessions();
You must first get the user id, then get the token, protect it and destroy the other sessions. Here's a working example:
global $wp_session;
$user_id = get_current_user_id();
$session = wp_get_session_token();
$sessions = WP_Session_Tokens::get_instance($user_id);
$sessions->destroy_others($session);
Here is a working version hooked to the init action.
/**
* Destroys all sessions for this user except the one with the given token (presumably the one in use).
*/
add_action( 'init', 'destroy_all_other_current_user_sessions' );
function destroy_all_other_current_user_sessions() {
$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
$manager->destroy_others( wp_get_session_token() );
};

How to know the role of current user in WordPress?

While a user is creating a new post, how do I determine his current role?
I'm assuming you know what hooks of Wordpress you want to use. So skipping that part, it's pretty easy to get the current role of the user
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
$roles = $current_user->roles; //$roles is an array
Now, you can iterate over that array to see if the user has a particular role.
Or, you can use current_user_can to look for specific capabilities, if you just want to check whether or not a user has a specific permission versus whether or not they're in the role. For example:
if (current_user_can('delete_posts')) {
//display the delete posts button.
}
This code will help you
<?php echo array_shift(wp_get_current_user()->roles); ?>

Resources