我们在WordPress的主题或者插件开发中,可能需要是用到session,百度了一圈居然搜不到内容,这里记录一下具体方法。
Table of Contents
注册session
function register_session(){ if( !session_id() ) session_start(); } add_action('init','register_session');
本方法在主题functions.php或者插件的函数中使用。
使用session
设置值
$_SESSION['myKey'] = "Some data I need later";
在其他地方使用
if(isset($_SESSION['myKey'])) { $value = $_SESSION['myKey']; } else { $value = ''; }
销毁session
add_action('wp_login', 'myEndSession'); function myEndSession() { session_destroy (); }