我有一个解决方案。我已经用我能想到的尽可能多的场景尽可能多地测试了代码。
我使用的代码与在this answer 到this question 中使用的代码相同。所以我不会再处理那个部分了
工作流程第 1 部分
我们将使用 jquery 来隐藏通知栏,以及一个 cookie,它有两个用途,保存最新的帖子 ID 并保持通知隐藏,直到发布新帖子或到期时间到期
为此,我们将使用 jquery 中的hide() 函数在用户单击隐藏按钮时隐藏通知栏。您可以根据需要自定义此按钮或使用任何其他类型的符号。
我们现在需要使用一些方法来隐藏按钮,直到发布新帖子。这将通过在单击隐藏按钮时设置 cookie 来完成。 cookie 设置为 2 天后过期,因此如果在这两天内没有发布新帖子,则 cookie 将自动过期。要设置 cookie,我们需要下载 jquery-cookie 插件。此插件还会在仍然设置 cookie 的情况下在发布新帖子时强制删除 cookie。
这部分很大程度上依赖于我们在new_post_notification 中设置的帖子 ID。问题是,您不能将 php 变量直接传递给 jquery。幸运的是,Wordpress 有一个名为wp_localize_script 的函数,我们可以使用它来将帖子 ID 传递给 jquery 脚本,在那里我们将使用它作为 cookie 值。
第 1 节到此结束,开始编码
让代码部分 1
首先,下载插件,解压并将jquery.cookie.js文件复制到主题的js文件夹中。接下来,在您的 js 文件夹中创建一个新文件并将其命名为 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: '/' } );
});
});
这是用于隐藏通知栏并设置 cookie 的代码。 var post_id = parseInt( cookie_Data.post_id, 10 ); 将保存帖子 ID,这是此处最重要的信息
我们现在需要注册并入队这两个js文件,并将post ID设置为wp_localize_script函数。打开你的functions.php并将以下内容粘贴到那里。如果您的主题中已经有 wp_enqueue_scripts 挂钩,只需从此处剥离相关代码并将其粘贴到您的函数中
function enqueue_cookie_scripts() {
wp_enqueue_script( 'jquery-cookie', get_template_directory_uri() . '/js/jquery.cookie.js', array( 'jquery' ), '' , true );
wp_register_script( 'set-cookie-option', get_template_directory_uri() . '/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' );
您还可以复制和粘贴在发布新帖子时设置new_post_notification 选项的功能。有关此代码如何工作的参考,请查看here。这段代码进入functions.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 );
工作流程第 2 部分
我们现在已经为 jquery 工作做好了一切准备,我们现在需要设置将显示通知栏的功能,如果没有设置新帖子,则显示隐藏按钮并删除 cookie还没过期。
此代码已被很好地注释,因此您现在将无法遵循它。这里最重要的部分是获取 cookie 的值,该值存储在全局变量中,可以使用$_COOKIE['hide_post_cookie'] 检索。这实际上是一个帖子 ID,这将与存储在 get_option( 'new_post_notification' )->ID 中的帖子进行检查
隐藏按钮将隐藏<div class="notifications_bar"></div> 中的任何内容,因此您将在该div 中添加通知栏。根据需要进行自定义。
我已经在一个函数中添加了所有代码,您可以在标题中调用如下
echo get_new_post_notification_bar();
第 2 部分代码
这段代码也进入你的functions.php
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;
}