如何为我的Wordpress重写主页中定义的cookie?

时间:2019-02-20 22:31:16

标签: wordpress cookies

在我的主页上,将cookie设置为默认值,然后必须在自定义function()中重写该cookie的值,然后在自定义页面中使用该值。

可以吗?不幸的是,我的价值仍然保留在默认主页上。

主页上定义的Cookie:

add_action('init', 'my_setcookie');

// my_setcookie() set the cookie on the domain and directory WP is installed on
function my_setcookie(){
    $path = parse_url(get_option('siteurl'), PHP_URL_PATH);
    $host = parse_url(get_option('siteurl'), PHP_URL_HOST);
    $expiry = strtotime('+1 month');
    setcookie('my_cookie', 'default_string', $expiry, $path, $host);
}

功能,我想在其中重写设置的Cookie:

function searchAdvertisements() {
    header('Content-Type: application/html;charset=utf-8');

    $text = $_POST['searchText'];

    global $wpdb;
    $myposts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts
     WHERE post_type = 'advertisements'
      AND post_title != 'Automatický koncept'
       and post_title LIKE '%s'", '%'. $wpdb->esc_like( $text ) .'%')  );

    $arrayID = array();

    foreach ($myposts as $post) {
        array_push($arrayID, $post->ID);
    }

    // rewrite cookie, new value will be used in custom page
    setcookie('my_cookie_name_1', $arrayID, get_page_link(45), get_page_link(45));

    $postArray = [];
    array_push($postArray, get_page_link(45));
    array_push($postArray, $text);
    array_push($postArray,$arrayID);

    echo json_encode($postArray);

    wp_reset_query();
    wp_die();
}

请有人已经解决了这个问题?我将非常感激

0 个答案:

没有答案