WordPress | Using PHP Session

| By Webner

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:


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() 

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.

Leave a Reply

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