WordPress - JS Cookies设置但不是全面可用

问题描述 投票:0回答:1

我正在为网络(多站点)网站​​的cookie墙做准备。我的方法是:

  • 注册自定义帖子类型“cookie_consent”;
  • 然后,创建一个相应的自定义模板文件“cookie-consent.php”
  • 在此自定义模板文件中,在标头中设置cookie功能
  • 单击按钮后激活此功能(也就是设置cookie)
  • 然后,通过使用isset(),我们调查是否设置了cookie。
  • 如果未设置,请重定向到自定义帖子类型。如果设置,则重定向到主页。

但是,在所有其他页面上,我得到了未设置cookie的返回,尽管我可以在检查器中看到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');

在模板文件中,我有以下内容:

<?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>

我不明白为什么插件无法识别其他页面/帖子上设置的cookie然后是cookie_consent?

javascript wordpress redirect cookies
1个回答
1
投票

没关系,我发现通过设置一个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=/"; }
© www.soinside.com 2019 - 2024. All rights reserved.