How to use session in wordpress in plugin development

后端 未结 2 518
面向向阳花
面向向阳花 2020-12-09 02:51

I am new to write a plugin ..I am having a testplugin.php file and a ajax.php file ..

My code in testplugin.php is

global $session;

print_r($abc);          


        
相关标签:
2条回答
  • 2020-12-09 03:15

    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.

    0 讨论(0)
  • 2020-12-09 03:42

    // 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'];
    }
    
    0 讨论(0)
提交回复
热议问题