我正在尝试在不使用任何插件的情况下发布新帖子时在前端获得一个简单的通知,此代码有效,但是如何将通知作为新帖子的计数器而不是(如果单击(在“隐藏”按钮上,我将消失。)和(隐藏)按钮像贝尔一样收集通知计数数字。
另一个问题是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
function enqueue_cookie_scripts() {
wp_enqueue_script( 'jquery-cookie', get_stylesheet_directory_uri() . '/custom-js/jquery.cookie.js', array( 'jquery' ), '' , true );
wp_register_script( 'set-cookie-option', get_stylesheet_directory_uri() . '/custom-js/hide.notification.bar.js', array( 'jquery', 'jquery-cookie'), '' , true );
$cookie_data = array(
'post_id' => get_option( 'new_post_notification' )->ID
);
wp_localize_script( 'set-cookie-option', 'cookie_Data', $cookie_data ); // this one do the magic
wp_enqueue_script( 'set-cookie-option' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_cookie_scripts' );
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 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;
}