Я работаю над стеной cookie для сети (мультисайта) веб-сайта.Мой подход:
- Зарегистрировать пользовательский тип сообщения "cookie_consent";
- затем создайте соответствующий файл пользовательского шаблона "cookie-согласия.php"
- В этом файле пользовательского шаблона в заголовке установлена функция куки
- , эта функцияактивируется (он же 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?