How to use session in wordpress in plugin development

浪尽此生 提交于 2019-11-28 21:17:06

// On your plugin or themes functions.php

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

// To set a SESSION data -

$_SESSION['arrayImg'] = $abc;

// To get the data on ajax hooked function -

function resolve_the_ajax_request(){
    if( !session_id())
        session_start();

    $abc = $_SESSION['arrayImg'];
}

In my case I was using that session variable in plugin activation as well. So did something unorthodox. Instead of defining my session_start in a hook I made it as the first line in my plugin :).

To heck with plugins, as soon as wordpress scans through my file it initiates the session.

At the end I do not destroy the session on user logout. I simply unset my variable. This is to just in case if some other plugin is also using session. If I destroy session it may affect other plugins.

Cheers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!