How to use my own custom session value in WordPress?

How can i use my own (custom) session value in WordPress?
For example: $_SESSION['myname']="4lvin"

I’ve already inserted session_start() at all page i need as following.

Read More
<?php
session_start();
$_SESSION['myname'] = "4lvin";
?>

But not working Global-wise.
Just working on the self page.
It is NOT call-able Globally from another pages (using same logic).

Related posts

Leave a Reply

4 comments

  1. EDIT: “THE PLUGIN BELOW ISN’T AVAILABLE ANYMORE, SO PLEASE USE THAT PLUGIN INSTEAD: WordPress Session Plugin

    There is a good WordPress Plugin adapted from CodeIgniter Session class: WP Sessions Plugin.

    When you activate the plugin, you can start to use $session object from anywhere in your theme ($session object as long as global). For instance, to use $session object into header.php file, simply add this code:

    global $session;
    

    If you are a plugin developer and you want to adapt this plugin with yours, you can find standalone version in the package as well. Documentation of the plugin gives more information for plugin developers about how to adapt to your project.

    Here is some useful functions for both theme and plugin developers.

    You can add session data like this:

    // One value
    $session->set_userdata( 'username', 'john' );
    
    // Passing array
    $array = array(
        'username' => 'john',
        'email'    => 'john@gmail.com'
    );
    
    $session->set_userdata( $array );
    

    To retrieve session data:

    $session->userdata( 'username' );
    

    To get all session data:

    $session->all_userdata(); // returns array
    

    To remove one item from session:

    $session->unset_userdata( 'username' );
    

    To remove more items from session:

    $array = array(
        'username' => '',
        'email'    => ''
    );
    $session->unset_userdata( $array );
    

    You can also use Flashdata which is session data that will only be available for the next server request, are then automatically cleared. These can be very useful when you use them for informational or status messages (e.g. “Product has been deleted”).

    // Add Flashdata
    $session->set_flashdata( 'item', 'value' );
    
    // Retrieve Flashdata
    $session->flashdata( 'item' );
    
    // Preserving flashdata 
    // (if you need to preserve flashdata through an additional request, 
    // you can use this function):
    $session->keep_flashdata( 'item' );
    

    To destroy session:

    $session->sess_destroy();
    

    The plugin also supports shortcodes. You can print any session data on your posts or pages:

    [session key="username"]
    

    To reach second key:

    [session key="user_data" sec_key="display_name"]
    

    I hope this helps for someone.

  2. WordPress doesn’t use sessions, that’s why your session variables aren’t working.

    As a matter of fact, if certain variables are defined, WordPress will actually destroy $_SESSION to keep itself stateless.

    But if you really want to use sessions, try adding session_start() at the beginning of your wp-config.php file. This will (hopefully) start sessions whenever WP starts up, so you’ll then be able to set and read your $_SESSION variables elsewhere in the system.

  3. One simple solution without using plug-in or without modifying the wp-config.php is to update your theme’s functions.php and insert at the very beginning the following:

    <?php
    
     //WordPress functions.php file
      if(!session_id()) {           
        session_start();            
      }
    

    Since the functions.php (or so called theme functions file) is a template included in WordPress themes automatically and acts like a plugin for your WordPress site. Moreover, if you have more than 1 theme installed on your website, modifying the functions.php will affect only the chosen theme, not all (where you may not want to enable sessions).