Javascript WordPress-JS Cookies已设置,但无法在侧边使用
我正在为一个网络(多站点)的网站制作cookie墙。 我的做法是:Javascript WordPress-JS Cookies已设置,但无法在侧边使用,javascript,wordpress,redirect,cookies,Javascript,Wordpress,Redirect,Cookies,我正在为一个网络(多站点)的网站制作cookie墙。 我的做法是: 注册自定义帖子类型“cookie\u同意” 然后,创建相应的自定义模板文件“cookie approvement.php” 在这个自定义模板文件中,在头文件中设置了一个cookie函数 此功能已激活(即cookie) 在点击按钮后设置) 然后,通过使用isset()我们研究了 cookie设置好了 如果未设置,则重定向到自定义帖子类型。如果设置, 重定向回家 然而,在所有其他页面上,我都会得到未设置cookie的返回,尽管我
- 注册自定义帖子类型“cookie\u同意”李>
- 然后,创建相应的自定义模板文件“cookie approvement.php”
- 在这个自定义模板文件中,在头文件中设置了一个cookie函数
- 此功能已激活(即cookie) 在点击按钮后设置)
- 然后,通过使用isset()我们研究了 cookie设置好了李>
- 如果未设置,则重定向到自定义帖子类型。如果设置, 重定向回家
function redirect(){
$url = get_permalink(get_page_by_title( 'Cookie Wall', '', 'cookie_consent' ));
if (!isset($_COOKIE[clickagree]) && (! is_singular( 'cookie_consent' ) ) ) {
wp_redirect ( $url );
exit;
}
else{
wp_redirect ( home_url() );
exit;
}
}
add_action( 'template_redirect', 'redirect' );
global $wp_version;
function create_post_type() {
if ( ! current_user_can('editor')) {
register_post_type( 'cookie_consent',
array(
'labels' => array(
'name' => __( 'cookie_consent' ),
'singular_name' => __( 'cookie_consent' )
),
'capabilities' => array(
'edit_post' => 'update_core',
'read_post' => 'update_core',
'delete_post' => '',
'edit_posts' => 'update_core',
'edit_others_posts' => '',
'delete_posts' => '',
'publish_posts' => '',
'read_private_posts' => '',
'create_posts' => '',
),
'public' => true,
'has_archive' => false,
'supports' => array(
'editor',
'custom-fields',
'page-attributes',
'thumbnail',
'excerpt',
),
)
);
}
}
add_action( 'init', 'create_post_type' );
define( 'cookie_consent_file', __FILE__ );
register_activation_hook( cookie_consent_file, 'cookie_consent_plugin_activation' );
function cookie_consent_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'cookie-wall'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$post = array(
'post_title' => __( 'Cookie Wall' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'cookie_consent',
);
// insert the post into the database
wp_insert_post( $post );
}
}
function agreebutton($atts, $content = null) {
extract(shortcode_atts(array($atts),$content));
return '<button onClick="SetCookie( \'clickagree\',\'yes\')">' . do_shortcode($content) . '</button>';
}
add_shortcode('button', 'agreebutton');
函数重定向(){
$url=get_permalink(按标题(“Cookie墙”、“Cookie同意”)获取页面);
如果(!isset($_COOKIE[clickagree])&(!是单数('COOKIE_approve')){
wp_重定向($url);
出口
}
否则{
wp_重定向(home_url());
出口
}
}
添加操作(“模板重定向”、“重定向”);
全球$wp_版本;
函数create_post_type(){
如果(!当前用户可以('editor')){
注册后类型(“cookie\u同意”,
排列(
“标签”=>数组(
“名称”=>(“cookie\u同意”),
“单数\u名称”=>(cookie\u同意)
),
“功能”=>阵列(
“编辑帖子”=>“更新核心”,
“阅读帖子”=>“更新核心”,
'删除帖子'=>'',
“编辑帖子”=>“更新核心”,
“编辑其他帖子”=>”,
“删除帖子”=>“”,
“发布帖子”=>”,
“阅读私人帖子”=>”,
“创建帖子”=>“”,
),
“public”=>正确,
“has_archive”=>false,
'支持'=>数组(
"编辑",,
“自定义字段”,
“页面属性”,
“缩略图”,
"摘录",,
),
)
);
}
}
添加操作('init','create_post_type');
定义('cookie\u同意\u文件',\u文件\u);
注册激活挂钩(cookie同意文件,“cookie同意插件激活”);
函数cookie\u同意\u插件\u激活(){
如果(!current_user_can('activate_plugins'))返回;
全球$wpdb;
if(null==$wpdb->get_行(“从{$wpdb->prefix}posts中选择post_名称,其中post_名称='cookiewall','ARRAY_A')){
$current_user=wp_get_current_user();
//创建post对象
$post=数组(
“贴子标题”=>(“饼干墙”),
“发布状态”=>“发布”,
“post\u author'=>$current\u user->ID,
“发布类型”=>“cookie\u同意”,
);
//将帖子插入数据库
wp_插入_员额(员额);
}
}
函数按钮($atts,$content=null){
提取(短码_atts(数组($atts),$content));
返回“”。do_短代码($content)。“”;
}
添加快捷码(“按钮”、“约定按钮”);
在模板文件中,我有以下内容:
<?php
/**
* Template Name: cookie template
* Template Post Type: cookie_consent
*/
?>
<head>
<script>
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
</script>
<head/>
<body>
<style><?php echo get_the_excerpt(); ?></style>
<div class="wrap" style="background: url('<?php the_post_thumbnail_url(); ?>');height:100%;overflow:auto">
<div id="primary" class="content-area">
<div id="main" class="site-main" role="main">
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
</div>
</div>
</div>
</body>
<footer>
</footer>
函数SetCookie(cookieName、cookieValue、nDays){
var today=新日期();
var expire=新日期();
变量nDays=365
expire.setTime(今天.getTime()+3600000*24*nDays);
document.cookie=cookieName+“=”+转义(cookieValue)
+“expires=“+expire.togmString()”;
}
不管怎样,我发现通过设置cookie,如果不设置路径/,则只能在设置的页面上访问它。因此,设置cookie的正确方法是添加+“path=/”;最后
因此,正确的代码如下所示:
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString() + "; path=/"; }
来自w3学校
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}