【问题标题】:How to close a notification bar with cookies and expiry date from php?如何从 php 关闭带有 cookie 和到期日期的通知栏?
【发布时间】:2015-02-04 18:09:17
【问题描述】:

在我之前的问题中,我正在寻找一种在我的 WordPress 网站上创建通知消息的方法,当有新帖子发布时。在一个很好的答案之后,这工作得很好。我可以更改帖子发布后该消息应显示多长时间的设置。时间一到,消息就会消失。

目标

所以它工作得非常好,特别感谢 Pieter Goosen,但如果用户已经看过该消息一次,我希望能够关闭通知栏并确保它不再按顺序显示给用户刷新页面不 telksens 返回消息,当然提供了一个新的帖子再次发布。

问题

我怎样才能做到这一点?我在考虑javascript。对于功能,当然应该有一个控制关闭按钮的功能,我认为应该还有一个cookie的功能,用于检查用户是否关闭消息并在计时器到期的时间内检查,以便两者相互同步。

我之前对通知的问题可以在这里找到:

How to count the total number of posts from the selected post types?

[更新] 我只是坐下来尝试弄清楚通知栏的结构,所以我把它简单地说一下,看看它是否可以工作,所以 PieterGoosen 的代码检查了 WordPress 中是否有新帖子可用并显示通知酒吧。然后该栏应在时间到期或用户单击关闭按钮后关闭。所以代码也应该检查。如果用户单击关闭按钮 == YES,则必须设置 cookie(cookie 应与 PHP 中设置的时间同步),因此只要有新帖子可用,它将删除 cookie。如果用户没有点击关闭按钮,那么请注意,如果时间已过 > 也删除 cookie。

【问题讨论】:

  • 所以您基本上希望您的栏在用户关闭时在整个会话期间保持关闭?您可能想使用 cookie。
  • @putvande 是的,你是对的 :)
  • @PieterGoosen,否 :(,不幸的是我仍然找不到解决方案,所以希望另一个人在这里有解决方案。我现在继续筛选它,我怎么能得到这个工作。如果你在此期间发现了什么,我想听听!
  • @PieterGoosen,我只是坐下来,试着把通知栏的结构弄清楚,所以我把它简单地说一下,我只是想和你分享,看看我们是否想到一个行,以便我明天可以继续尝试解决:所以我们检查了 WordPress 中是否有新帖子可用并显示通知栏。 >>
  • @PieterGoosen >> 然后栏应该在时间到期或用户点击关闭按钮后关闭。所以我们也应该检查一下。如果用户点击关闭按钮 == YES,那么我们应该设置一个 cookie(cookie 应该与 PHP 中设置的时间同步),所以只要有新帖子可用,它就会删除 cookie。如果用户没有点击关闭按钮,那么请注意,如果时间已过期 > 也删除 cookie。你怎么看?这是正确的流程吗? :)

标签: javascript wordpress jquery-cookie


【解决方案1】:

我有一个解决方案。我已经用我能想到的尽可能多的场景尽可能多地测试了代码。

我使用的代码与在this answerthis 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;

}

【讨论】:

  • 如果我删除enqueue_cookie_scripts 函数,网站会正确显示,但我在你的代码中没有看到任何奇怪的地方,这是我的错误的罪魁祸首。
  • 我很高兴它现在正在工作。我应该在回答中提到代码至少需要 PHP 5.4+。享受:-)
  • 你好@PieterGoosen,我知道你很早就回答了这个问题,但你的代码仍然有效,谢谢。小问题,如果你不介意的话,有没有办法把它变成柜台?并隐藏按钮作为收集计数的铃铛?并确保避免 cookie 删除新通知,直到每个用户点击它!提前致谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多