session_start() 防止在 Wordpress 编辑器中保存 [英] session_start() prevents saving in Wordpress editor

查看:59
本文介绍了session_start() 防止在 Wordpress 编辑器中保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义 Wordpress 插件中有以下代码:

I have the following code in my custom Wordpress plugin:

add_action('init', 'wwp_StartSession', 1);

function wwp_StartSession() {
    if(!session_id()) {
       session_start();
    }
}

当我在 Wordpress 编辑器中编辑它时,它可以被保存.但是,如果我想在多次编辑后再次保存,则会出现以下错误:

When I edit this in the Wordpress editor it can be saved. However if I want to save again after more edits I get the following error:

Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.

当我删除该行

sesion_start();

我可以再次保存.

我已经在谷歌上搜索了一段时间,有人说 if(!session_id()) 应该可以解决问题,但似乎没有.

I already Googled for quite a while now and some say that the if(!session_id()) should do the trick, but it seems it doesn't.

希望有人对此有任何想法.

Hoping someone has any ideas on this.

推荐答案

我终于知道了:).我在这里找到了答案:https://core.trac.wordpress.org/ticket/47320

Finally I found out :). I found the answer here: https://core.trac.wordpress.org/ticket/47320

这是我的代码:

class Session {
    public static function startSession() {
         // This loads variables to $_SESSION for reading
         if(!session_id()) {
            session_start();
            session_write_close(); // Other plugins can restart a session again via session_start()
        }
    }

    public static function endSession() {
        session_destroy ();
    }

    public static function storeData($key, $value) {
        session_start();
    
        $_SESSION[$key] = $value;
    
        session_write_close();
    }
}

startSession 与 init 挂钩endSession 连接到 wp_login 和 wp_logout以及我需要保存数据的地方调用 storeData

startSession is hooked to init endSession is hooked to wp_login and wp_logout and wherever I need to save data call storeData

这篇关于session_start() 防止在 Wordpress 编辑器中保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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