How do you use session variables in wordpress?
Replace:
global $wp_session;
With: $wp_session = WP_Session::get_instance();
Make sure you add $wp_session = WP_Session::get_instance();
before you try to echo the variable on page 2. How to use session without using $_SESSION in wordpress
Use the init
hook to session_start()
is working fine on normal server without WP Engine.
Example code:
<?php
/**
* Plugin Name: Session plugin.
*/
add_action('init', 'sesp_init');
function sesp_init()
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$_SESSION['hello'] = 'world';
}
add_action('template_redirect', 'sesp_template');
function sesp_template()
{
echo 'hello ' . $_SESSION['hello'].'<br>';
}
It say hello world on the front pages.WP_Session
The class WP_Session
is not WordPress core class. I couldn't find this class anywhere in WordPress core files except WP_Session_Token
but that is not for session usage.The class WP_Session
is from WP Session Manager plugin (refer from this question). You have to install that plugin to access WP_Session
. Otherwise this error will be occur.
Class WP_Session not found
Use Transients.
From the document.WordPress Transients API, which offers a simple and standardized way of storing cached data in the database temporarilySo, you can use it as alternative if
session_start()
is not really work and you don't want to install other plugin.Example code:
add_action('init', 'sesp_init');
function sesp_init()
{
set_transient('sesp_hello', 'moon', 60*5);// 5 minutes.
}
add_action('template_redirect', 'sesp_template');
function sesp_template()
{
echo 'hello ' . get_transient('sesp_hello') . ' (use transient).<br>';
}
You don't need to hook into init
to set transient. The code above is for example. The third argument in set_transient()
is expiration in seconds.Transient Warning!
However, the transient is not for one visitor like the session. You have to set and check properly that the transient you are getting and setting is match for a person.WP Engine
From their support page.The biggest problem this presents is due to the unique session IDs. Unique IDs effectively bust cache and causes every session to become uncached. This will cause serious performance issues for your site.You may switch to use PHP cookie or disable WP Engine cache. This was answered here.
Set and retrieve the session values wordpress
You can do using following code.
Add below code in your theme's functions.php file
function register_session_new(){
if( ! session_id() ) {
session_start();
}
}
add_action('init', 'register_session_new');
$_SESSION['menu_lang'] = "english";
Use this line in your mini-cart.php file or wherever you want to use.echo $_SESSION['menu_lang'] ;
Wordpress session variable not working
Just hook a function on "init" in your functions.php like this :
function ur_theme_start_session()
{
if (!session_id())
session_start();
}
add_action("init", "ur_theme_start_session", 1);
Then u can use your session variables.I hope that help u.
How to get session variables in wordpress plugins
You need to add <?php session_start(); ?>
at beginning of my_page.php
After that for destroying session you can use wp_logout action in wordpress. code is as follows
<?php function custom_unset_session() {
// your code
unset($_SESSION['tag']);
}
add_action('wp_logout', 'custom_unset_session');
?>
Related Topics
When Should I Close a Database Connection in PHP
How to Add Item to the JSON File Formatted Array
Zend Framework 2 - Global Check for Authentication with Zfcuser
How to Build a Condition Based Query in Laravel
Composer:How to Add a Dependency Without Network Connection
Visits Counter Without Database with PHP
PHP Resize Image on or Before Upload
Facebook Like Custom Profile Url PHP
Why MySQL Connection Is Blocked of Many Connection Errors
PHP Using Regex to Get Substring of a String
PHP Fatal Error: Call to Undefined Function Imagettftext()
PHP Gd: How to Get Imagedata as Binary String
Object Copy Versus Clone in PHP
MySQL and Query to Satisfy on Same Column
The Behaviour of the or Operator in PHP
Error Starting Userland Proxy: Listen Tcp 0.0.0.0:3306: Bind: Address Already in Use