Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get session variables in wordpress plugins

Tags:

php

wordpress

Hi iam new to wordpress and I have created a plugin at which I need to print all the session data.First I have created a file in plugin folder and added code like

function myplugin_classname() {
    print_r($_SESSION);
}

And I put an click event for two button with class tags like

$('.tags').on('click',function(){
     $.post('my_page.php',{val:$(this).val()});
});

and in my_page.php I kept like

$_SESSION['tag'] = $_POST['val'];

but when it comes to printing the session variables at myplugin_classname (by refreshing the page)it doesnt prints the newly assigned session variable....How to solve this..??I have started session through theme-my-login login.

like image 605
Gautam3164 Avatar asked Jul 15 '13 12:07

Gautam3164


People also ask

How do I find session variables in WordPress?

The simplest way to get access to the session is to add the following lines to wp-config. php before the call to wp-settings: if (! session_id()) session_start();

Does WordPress use session variables?

WordPress Core does not use sessions. All "user state" is managed via cookies. This is a Core design decision. However, some plugins or themes will use session_start() or PHP's $_SESSION superglobal.

How do I use session plugins in WordPress?

From your WordPress dashboardVisit 'Plugins > Add New'. Search for 'Sessions'. Click on the 'Install Now' button. Activate Sessions.

Where Are session variables stored?

By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier).


1 Answers

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');
?>
like image 110
Datta Parad Avatar answered Sep 22 '22 13:09

Datta Parad