Javascript 在WordPress中访问两个短代码之间的会话变量
我正在使用datatales JavaScript插件并将其集成到WordPress中。我将把我的问题分解成我为解决它们而采取的步骤和方法Javascript 在WordPress中访问两个短代码之间的会话变量,javascript,php,wordpress,session-variables,session-state,Javascript,Php,Wordpress,Session Variables,Session State,我正在使用datatales JavaScript插件并将其集成到WordPress中。我将把我的问题分解成我为解决它们而采取的步骤和方法 我正在使用WOW插件(html和js代码!!)来编写客户端html和服务器端php代码,效果很好 我从wp登录表单访问用户名和用户角色,并将其保存在会话变量中 add_action('init', 'myStartSession', 1); add_action('wp_logout', 'myEndSession'); add_action( 'i
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action( 'init', 'get_current_user_role',1);
function myStartSession() {
if(!session_id()) {
session_start();
}
if ( isset( $_POST['wp-submit'] ) ){
$_SESSION['username']=$_POST['log'];
}
}
function myEndSession() {
session_destroy ();
}
function get_current_user_role() {
if(!session_id()) {
session_start();
}
if( is_user_logged_in() ) { // check if there is a logged in user
$user = wp_get_current_user(); // getting & setting the current user
$roles = ( array ) $user->roles; // obtaining the role
$_SESSION['roles']=$roles[0];
return $roles; // return the role for the current user
} else {
return array(); // if there is no logged in user return empty array
}
}