插件开发中如何在wordpress中使用session [英] How to use session in wordpress in plugin development

查看:72
本文介绍了插件开发中如何在wordpress中使用session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编写插件的新手..我有一个 testplugin.php 文件和一个 ajax.php 文件..

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

我在 testplugin.php 中的代码是

My code in testplugin.php is

global $session;

print_r($abc); //$abc is array of my data ..

$session['arrayImg']=$abc; //saving data in session 

echo  $session['arrayImg']; //displayin "Array"

我的ajax.php包含以下代码

And my ajax.php consists of following code

global $session;

$abc = $session['arrayImg'];

print_r ("abs== ".$abc); //displayin "abs== Array"

如果使用 session_start();

出现以下错误

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent 

我只想将插件的一个文件中的数据数组发送到另一个文件...

I just want to send array of data from one file of my plugin to another file ...

推荐答案

在您的插件或主题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
}

这篇关于插件开发中如何在wordpress中使用session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆