且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

插件开发中如何在wordpress中使用session

更新时间:2023-12-03 23:40:34

在您的插件或主题functions.php 文件中添加以下内容

Add following on your plugin or themes functions.php file

function wpse16119876_init_session() {
    if ( ! session_id() ) {
        session_start();
    }
}
// Start session on init hook.
add_action( 'init', 'wpse16119876_init_session' );

接下来,在SESSION中添加数据-

Next, to add data in SESSION -

// If session has started, this data will be stored.
$_SESSION['arrayImg'] = $abc;

获取ajax挂钩函数的数据-

// handle the ajax request
function wpse16119876_handle_ajax_request() {
    if ( ! session_id() ) {
        session_start();
    }

    if ( array_key_exists( 'arrayImg', $_SESSION ) ) {
        $abc = $_SESSION['arrayImg'];
    } else {
        $abc = 'NOT IN SESSION DATA';
    }

    // Do something with $abc
}