WordPress | Using PHP Session

Author - Mamta
21.11.2017
|
0 Comments
|

Many times it may happen that you create session variables in a particular code of the WordPress site and you are not able to access that session variable in any other part of the site except in your custom code. So here are the solutions you can apply to solve the issue :

1. Getting access to the session if you are not creating a plugin or theme.

In this case what can you do is just add the following code in the wp-config.php file before the call to wp-settings:

If(!session_id())
session_start();

2. If you are creating a plugin.

You can’t put your session_start in wp-config.php because of permissions issues you can hook it into the “init” action. To do that you can add the following code to your plugin or your theme’s functions.php:

add_action('init', 'myStartSession', 1);
function myStartSession() 
{
if(!session_id()) 
{
session_start();
}
}

This will start the session early in the initialization process, here 1 is the priority to cause this to run before other initialization. You can now access the session variable inside your site anywhere once this code executes.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send your message