我正在尝试设置cookie,以将返回的用户重新路由到Wordpress站点中的特定页面。
我想从以下两方面得到一些建议:
加载任何内容以处理重定向之前,应在Wordpress php文件的何处检查Cookie?是否有一个好的文件应该存在于其他文件中setcookie(\'cookie_name\', \'cookie_value\', time()+4000); 似乎没有将任何cookie保存到我的系统中
我正在尝试设置cookie,以将返回的用户重新路由到Wordpress站点中的特定页面。
我想从以下两方面得到一些建议:
加载任何内容以处理重定向之前,应在Wordpress php文件的何处检查Cookie?是否有一个好的文件应该存在于其他文件中setcookie(\'cookie_name\', \'cookie_value\', time()+4000); 似乎没有将任何cookie保存到我的系统中
啊,意识到我需要把这个挂在init()
.
解决方案:我在函数中创建了一个函数。php来设置和检查cookie。为了使其正常工作,在定义函数后,在函数外部调用以下内容:
add_action(\'init\', \'function-name\');
1-您可以检查Cookie并使用在任何输出之前调用的挂钩(如“init”挂钩)进行重定向:
<?php
// Hook the function "redirect()" on to the "init" action
add_action(\'init\', \'redirect\');
// redirect() may redirect the user depending on the cookies he has
function redirect(){
/* CODE */
}
?>
2-设置cookie的最佳方法是使用如下“init”挂钩:<?php
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_name_1\', \'my_cookie_value_1\', $expiry, $path, $host);
/* more cookies */
setcookie(\'my_cookie_name_2\', \'my_cookie_value_2\', $expiry, $path, $host);
}
?>
如果你的博客位于www.example.com/blog, coockie将不在www.example。com www.example。com/store示例。com www.www2。实例com
您可能应该使用常量COOIKEPATH
和COOKIE_DOMAIN
, 自WP 3.0起存在
setcookie("your_cookie", $your_value, time()+3600, COOKIEPATH, COOKIE_DOMAIN);
This way worked :
add_action( \'init\', \'function-to-setcookie\' );
function function-to-setcookie(){
//use condition here , in which page you eant to set cookie
//choose a page where you want the cookie to be set
$pageurl = get_option(\'siteurl\').\'/set-cookie-page\';
// use a function to get current page url and use condition
//to match it with the desired page where you want to set cookie
if ( $pageurl === current_page_url() ) {
setcookie( \'cookie_name\', \'cookie_value\', $expiryTime, $cookiepath, $siteurl );
}
}
在将任何内容写入页面之前(在发送标题之前),您需要删除cookie。
要设置cookie,请包括路径、域,我还建议将最后2个参数设置为true($secure
和$httponly
). 您还需要将相同的参数提供给setcookie()
删除时,除了$expiry
(应该是负数)和$value
(应为空“”)。
如果要通过cookie传递json,似乎需要base64_encode
它也是,否则它将无法正确解码。
所有这些都应该在一个类中完成,这样您以后就可以在代码中访问cookie的值。该类可以添加到插件或函数中。php。
下面是一个示例,我们使用cookie存储操作响应,然后在重定向后将其显示给用户:
class my_custom_class {
const MY_COOKIE_NAME_JSON = \'my_cookie_name_json\';
const COOKIE_LIFETIME = 3600;
private $cookie_value;
function __construct() {
// ensure you are deleting cookie before headers are sent
add_action(\'init\', [$this, \'process_cookie_json\'], 10);
// uses bootstrap alert to format return message
add_filter(\'the_content\', [$this, \'filter_the_content_in_the_main_loop\'], 1);
}
static function some_action_that_sets_the_cookie($message, $response_type = \'success\') {
$responses = [];
if (isset($_COOKIE[self::MY_COOKIE_NAME_JSON]))
$responses = json_decode(base64_decode($_COOKIE[self::MY_COOKIE_NAME_JSON]));
$responses[$response_type][] = $message;
self::set_cookie_json($responses);
}
static function set_cookie_json(array $cookie_value) {
setcookie(self::MY_COOKIE_NAME_JSON, base64_encode(json_encode($cookie_value)), time() + self::COOKIE_LIFETIME, "/", $_SERVER[\'HTTP_HOST\'], true, true);
}
function process_cookie_json() {
if (!isset($_COOKIE[self::MY_COOKIE_NAME_JSON]))
return false;
$this->cookie_value = json_decode(base64_decode($_COOKIE[self::MY_COOKIE_NAME_JSON]), true);
setcookie(self::MY_COOKIE_NAME_JSON, \'\', -1, "/", $_SERVER[\'HTTP_HOST\'], true, true);
unset($_COOKIE[self::MY_COOKIE_NAME_JSON]);
}
function filter_the_content_in_the_main_loop($content) {
if (!$this->cookie_value || !is_array($this->cookie_value))
return $content;
$alerts = [];
foreach ($this->cookie_value as $response_type => $messages)
$alerts[] = \'<div class="alert alert-\' . $response_type . \'" role="alert">\' . implode(PHP_EOL, $messages) . \'</div>\';
return implode(null, $alerts) . $content;
}
}
$my_custom_class = my_custom_class;
然后,您可以通过以下方式设置cookie:my_custom_class::some_action_that_sets_the_cookie(\'the message\');
我有SQL文件和美国邮政编码(超过41000行代码)。我想做一个插件,将这个SQL文件插入WP数据库,这样用户就可以按邮政编码进行搜索。我想用PHP而不是PhpMyAdmin插入SQL