dtttlua7165 2016-05-11 22:31
浏览 30

Wordpress cookie只有一次

i'm struggling with a cookie matter for the past 2 hours. What i'm trying to do is to set a cookie with Javascript with a click event on a link and then let the default redirection to be happened. In the new page a want to perform an operation because of the cookies value and then delete it, so on a possible reload the site works normal again. The cookie will be a switch in order to determine if i'm going to run a pre_get_post function on my archive. The problem is that i cant find how to delete the cookie after i've grabbed it's value. Here is my code

function modify_query( $query ) {
    $cookie_data = $GLOBALS['cookie_data'];
    if (  $query->is_main_query() && isset( $cookie_data )) {
        $meta_query = array(
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key'   => 'key',
                    'value' => $cookie_data,
                )
            )
        );
        $query->set( 'meta_query', $meta_query );
    } // end if
}
add_action( 'pre_get_posts', 'modify_query' );



function get_cookie() {
        if(isset($_COOKIE['test_data'])){
            global $cookie_data;
            $cookie_data = $_COOKIE['cookie_data'];
            unset( $_COOKIE['cookie_data'] );
            setcookie( 'cookie_data', '', time()-3600, '/');
        }
    }
    add_action('init', 'get_cookie', 90);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用