Skip to main content
13 votes

Preventing session timeout

By default, WordPress makes your login session cookie expire in 48 hours (or on browser close), or 14 days if you check the “Remember Me” box. Maybe you have some plugins which force your login cookie ...
Aishan's user avatar
  • 762
9 votes
Accepted

Does using set_transient() function can lead to MySQL problems?

Does using set_transient() function can lead to MySQL problems? No, although if you used it so much that the MySQL server ran out of disk space, then yes it could cause issues. However, you would ...
Tom J Nowell's user avatar
  • 59.7k
8 votes

How to store and receive variables in WP sessions?

add this at in your functions.php <?php function tatwerat_startSession() { if(!session_id()) { session_start(); } } add_action('init', 'tatwerat_startSession', 1);
Abdo-Host's user avatar
  • 236
7 votes

How to share cookies and sessions between domain and subdomain?

I'll skip steps related to sharing DB and sharing users data and meta, based on assumption that you've done it correctly. Let's concentrate on wp-config.php of both sites. These are defines that MUST ...
Frank P. Walentynowicz's user avatar
5 votes
Accepted

How to log out everywhere else, destroy all sessions "all other devices"?

That button sends an AJAX request that runs wp_ajax_destroy_sessions(). It's not really abstracted in such a way that you can re-use it outside of AJAX, but if you copy the source into your own ...
Jacob Peattie's user avatar
5 votes

Show menu item only if user is logged In (not word press login)

Use the wp_nav_menu_objects filter to access the menu items I have used this code: add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2); function ad_filter_menu($sorted_menu_objects, $args) { ...
Jacob Thygesen's user avatar
4 votes
Accepted

How can I get logged in user's session data from admin-ajax?

wp_get_current_user will get you the WP_User object for the currently logged-in user: add_action( 'wp_ajax_create_team', 'create_team' ); function create_team() { $current_user = ...
Milo's user avatar
  • 78.5k
4 votes
Accepted

How should you hook a session_start() when authoring a plugin?

There is no regular output and hence no header sent before template_redirect on the front end. If you need sessions on the back end too, use the action wp_loaded to cover both. Example: add_action( '...
fuxia's user avatar
  • 107k
3 votes

How to log out everywhere else, destroy all sessions "all other devices"?

I know this is an old topic but destroy_all() will destroy all sessions but destroy_others() destroys all sessions except the current session Destroy all sessions: $sessions = WP_Session_Tokens::...
Hossein's user avatar
  • 31
3 votes
Accepted

How to get session token of current user in wp_login hook?

You can't access the $_COOKIE in this hook. You can only access $_COOKIE after the page HTTP headers are set with the $_COOKIE its mean in the next page. You have other hook that you can hook into to ...
Shibi's user avatar
  • 1,833
3 votes
Accepted

update_user_meta() does not work

You set the variable $user before you created this user.. It should be like this and you need to check if the user already exists $user = get_user_by("email", $email); // Its return you the user ...
Shibi's user avatar
  • 1,833
3 votes
Accepted

Safe to start a php session on get_header action?

There’s nothing that requires that get_header run before output starts, you could put it in the middle of a template file after other content and still have a perfectly valid theme. template_redirect ...
Milo's user avatar
  • 78.5k
3 votes
Accepted

Handling nonces for actions from guests to logged-in users

I went for the solution as suggested by @Rup in the comments. Replacing the two nonce functions, wp_create_nonce() and wp_verify_nonce() to prefix a nonce with a 1 or a 0 for logged-in and logged-out ...
Swen's user avatar
  • 1,374
3 votes

Site Health : An active PHP session was detected

I was facing same issue. I replaced the code session_start(); with if (!isset($_SESSION)) { session_start(['read_and_close' => true]); } and it worked for me.
Shiv Babbar's user avatar
3 votes
Accepted

Multi-instance WordPress usingn Memcached to handle sessions requests login every time a requests is handled by a different server

The problem was that my *_KEY tokens were being generated each time Kubernetes was creating a new instance, as @Rup and @Tom-J-Nowel rightly pointed. Because my docker image evolved from having an wp-...
Jesus Iniesta's user avatar
3 votes
Accepted

How to Extend login session times to a Month

By default the login cookie lasts for 14 days if you tick 'remember me' 48 hours if you don't So as a short term fix you probably want to tick 'remember me', and to extend that to 30 days you can ...
Rup's user avatar
  • 4,287
2 votes

How to store and receive variables in WP sessions?

Maybe there are no usual sessions in Wordpress ... anyway, Wordpress knows the concept of users. You can manage information related to specific users with the the functions add_user_meta, ...
Philipp Zedler's user avatar
2 votes

how to create and show sessions in word press?

TomC is right, but to build on that here's what I do. Mainly I use this with a global object which I serialise into the session to save and unserialise from the session to use. Here I've just used ...
Andy Macaulay-Brook's user avatar
2 votes

Why does $_SESSION only work when I am logged in?

I have been thinking about using Wordpress' meta keys as session variables. Something like this: add_user_meta( int $user_id, string $meta_key, mixed $meta_value, bool $unique = false )...
Roddy P. Carbonell's user avatar
2 votes

update_user_meta() does not work

Solved the problem by changing these lines: register_new_user($email, $_SESSION["email"]); update_user_meta($user, "quiz_scores", $score); To this: $registered = register_new_user($email, $_SESSION[...
Limpuls's user avatar
  • 307
2 votes

WordPress and PHP Sessions - Security and Performance

This has very little to do with wordpress, but if this answer will help eliminate the usage of session, it might be worth it.... Sessions are stored on the hard disk of the server. This is ...
Mark Kaplun's user avatar
  • 23.4k
2 votes

Displaying "One Time" Notification in Plugins

The idea is that you need to save_errors or update the option that has the errors/notices whenever you want. As soon as its output once by admin_notices it will be cleared. /** * ...
Ahmed Fouad's user avatar
  • 1,343
2 votes

Front end ajax user login session issue

You are not setting an authentication cookie. The second parameter of the wp_signon function is set to false. This function sets an authentication cookie. Users will not be logged in if it is not ...
Willi's user avatar
  • 69
2 votes
Accepted

Unset session variable on page reload / setup but exclude AJAX

Try using wp_doing_ajax() like so: function unset_filter_session() { if ( ! wp_doing_ajax() ) { //Reset sessions on refresh page unset( $_SESSION['expenditure_filter'] ); } } ...
Sally CJ's user avatar
  • 39.3k
2 votes

How to expire session after 2 hours and also expire when browser closed?

It is the correct hook. I often also add a bit of javascript, to active the checkboxes inside the form. In your case it would not be necessary to use the $accepted_args parameter – 3 – of the ...
bueltge's user avatar
  • 17.1k
2 votes

keep a variable for all pages in memory from the url

TLDR: Use a cookie Why Not PHP Sessions? PHP sessions don't work on most hosts ( e.g. WP Engine which say in their docs they do not support PHP sessions ). PHP sessions are incompatible with caching ...
Tom J Nowell's user avatar
  • 59.7k
2 votes
Accepted

How do I add $_SESSION[''] to my wordpress page?

Remove your session_start() and at the beginning of your functions.php put this: if (!session_id()) { session_start(); } For use from a plugin use this: function register_session(){ if( !...
drcrow's user avatar
  • 171
2 votes

Using session in WP without trouble with the API REST

The only way to make this go away is to not use PHP sessions and use cookies directly instead. PHP sessions are fundamentally incompatible with a lot of page caching and CDN mechanisms e.g. cloudflare,...
Tom J Nowell's user avatar
  • 59.7k
2 votes
Accepted

homepage redirects to login page when user session expires

If you looked at your plugins one by one and none of them causes the problem, look at your template files that display your home page – could be index.php, home.php, content.php, content-{slug/id}.php ...
zoltankundi's user avatar
2 votes
Accepted

Having Issue on Redirecting With Session in WordPress

header can't be used once the headers have been sent, the moment you output anything such as a tag, or even a blank space, PHP will send out the headers telling the browser to expect a HTML document. ...
Tom J Nowell's user avatar
  • 59.7k

Only top scored, non community-wiki answers of a minimum length are eligible