我真的很需要您的帮助,如何避免Cookie删除旧的通知!我如何保留所有这些,直到到期或单击“隐藏”按钮。
我不需要删除旧的Cookie通知,我需要保留所有通知!
感谢您的帮助和时间。
hide.notification.bar.js
jQuery(document).ready(function($) {
$("#notification_hide_button").click(function(){
$(this).hide();
$(".notifications_bar").hide();
if ($.cookie( 'hide_post_cookie' ) ) {
$.cookie( 'hide_post_cookie', null )
}
var post_id = parseInt( cookie_Data.post_id, 10 );
$.cookie( 'hide_post_cookie', post_id, { expires: 2, path: '/' } );
});
});
function.php>检查帖子状态。
add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
//Check if our post status then execute our code
if ( $new_status == 'publish' && $old_status != 'publish' ) {
if ( get_option( 'new_post_notification' ) !== false ) {
// The option already exists, so we just update it.
update_option( 'new_post_notification', $post );
} else {
add_option( 'new_post_notification', $post );
}
}
}, 10, 3 );
function.php>获取new_post_notification并保存它们。
function get_new_post_notification_bar() {
// Get the new_post_notification which holds the newest post
$notification = get_option( 'new_post_notification' );
// Get the post ID saved in the cookie
$cookie_post_ID = isset( $_COOKIE['hide_post_cookie'] ) ? (int) $_COOKIE['hide_post_cookie'] : false;
$output = '';
if( false != $notification ) {
//First check if we have a cookie, if not, show the notification bar
// If a cookie is set, do not display the notification bar
if( false === $cookie_post_ID ) {
//Get the post's gmt date. This can be changed to post_date
$post_date = strtotime( $notification->post_date_gmt );
//Get the current gmt time
$todays_date = current_time( 'timestamp', true );
//Set the expiry time to two days after the posts is published
$expiry_date = strtotime( '+2 day', $post_date );
//Show the notification bar if the expiry date is not yet reached
if( $expiry_date > $todays_date ) {
$output .= '<div class="notifications_bar">';
$output .= 'If you click on the "Hide" button, I will disappear.';
$output .= '</div>';
$output .= '<button id="notification_hide_button">';
$output .= 'Hide';
$output .= '</button>';
}
}else{
/**
* If a cookie is set, check the cookie value against the post id set as last post
* If the two don't match, delete the cookie and show the notification bar if a new post is published
* This code only run once, that is when a cookie is still set, and new post is published within the time
* in which the cookie is still set
*/
if( (int) $notification->ID !== $cookie_post_ID ) {
?>
<script>
jQuery(document).ready(function($) {
$.removeCookie('hide_post_cookie', { path: '/' });
});
</script>
<?php
$output .= '<div class="notifications_bar">';
$output .= 'If you click on the "Hide" button, I will disappear.';
$output .= '</div>';
$output .= '<button id="notification_hide_button">';
$output .= 'Hide';
$output .= '</button>';
}
}
}
return $output;
}