如何在WP会话中存储和接收变量?
-
-
如果答案对您有所帮助,请考虑接受.请参阅»[有人回答我的问题时该怎么办?](http://wordpress.stackexchange.com/help/someone-answers)«和/或»[为什么投票很重要?](http://wordpress.stackexchange.com/help/why-vote)«,有关[wordpress.se]模型的更多信息,请访问[help].If the answer was helpful to you, then consider accepting it. See »[What should I do when someone answers my question?](http://wordpress.stackexchange.com/help/someone-answers)« and/or »[Why is voting important?](http://wordpress.stackexchange.com/help/why-vote)«, more information about the [wordpress.se] model is available at the [help].
- 0
- 2015-04-29
- Nicolai
-
我认为下面的链接将为您提供帮助.https://wordpress.stackexchange.com/questions/105453/getting-headers-already-sent-error-from-pluginI think below link will help you. https://wordpress.stackexchange.com/questions/105453/getting-headers-already-sent-error-from-plugin
- 0
- 2018-10-04
- Vivekanand Saraswati
-
6 个回答
- 投票数
-
- 2013-10-09
默认情况下,wordpress中未启用会话,如果您要激活php会话,请在
functions.php
的开头添加此选项:if (!session_id()) { session_start(); }
您现在可以使用
$_SESSION['your-var'] = 'your-value';
来设置会话变量.看看有关会话的PHP文档.
更新:
还有第二个答案,在我看来,这个答案也很有价值-不幸的是它被删除了,我在这里重新添加信息.答案是指 WP Session Manager 由@eamann编写的插件,作为替代解决方案.
我读了一些有关该插件的内容,但从未使用过,因为-到目前为止-我坚持使用PHP会话-从未遇到无法使用它们的问题.这是声明/评论通过插件作者本人,我发现了一些可能的优点.Sessions aren't enabled in wordpress by default, if you want to activate php sessions add this at the beginning of your
functions.php
:if (!session_id()) { session_start(); }
You now can use
$_SESSION['your-var'] = 'your-value';
to set a session variable. Take a look at the PHP documentation on sessions.
Update:
There was a second answer, which, in my mind, had a value too - unfortunately it got deleted, I'm re-adding the information here. The answer was referring to WP Session Manager a plugin written by @eamann as a alternative solution.
I read some things about the plugin, but never used it because - so far - I'm sticking with PHP sessions - never had the problem that I couldn't use them. This is a statement/comment by the plugin author himself I found about some possible advantages.-
我可以请您在这里查看与自定义字段相关的问题:https://wordpress.stackexchange.com/questions/265852/set-and-unset-the-custom-field-value吗?may I ask you to have a look at a custom field related question here : https://wordpress.stackexchange.com/questions/265852/set-and-unset-the-custom-field-value ?
- 0
- 2017-05-04
- Istiaque Ahmed
-
- 2017-06-14
将此添加到您的functions.php中
<?php function tatwerat_startSession() { if(!session_id()) { session_start(); } } add_action('init', 'tatwerat_startSession', 1);
add this at in your functions.php
<?php function tatwerat_startSession() { if(!session_id()) { session_start(); } } add_action('init', 'tatwerat_startSession', 1);
-
- 2018-08-05
我将解释如何使用 WordPress的本地PHP会话插件设置用户名会话.您可以将这种逻辑应用于您的问题.
将此添加到您的functions.php文件中
if (!session_id()) { session_start(); } if ( isset( $_POST['wp-submit'] ) ){ $_SESSION['username']=$_POST['log']; }
$ _ POST ['log']从wordpress登录表单引用用户名输入框.用户登录时,用户名将存储到$ _SESSION ['username']. 在您的情况下,您可以将" log"更改为具有" car_color"的表单变量名称.
稍后在其他一些php文件中引用用户名,
$username=$_SESSION['username'];
I will explain how to set a username session with the Native PHP Sessions for WordPress plugin. You can apply this logic to your problem.
Add this to your functions.php file
if (!session_id()) { session_start(); } if ( isset( $_POST['wp-submit'] ) ){ $_SESSION['username']=$_POST['log']; }
$_POST['log'] is referencing the username input box from the wordpress login form. When a user logs in, the username is stored to $_SESSION['username']. In your case, you would change 'log' to the form variable names you have 'car_color'.
Reference the username later in some other php file by
$username=$_SESSION['username'];
-
- 2016-08-12
Wordpress中可能没有常规会话……无论如何,Wordpress知道用户的概念.您可以使用
add_user_meta
功能管理与特定用户有关的信息,update_user_meta
,get_user_meta
和delete_user_meta
.如果您需要以这种方式保存在JavaScript中的信息,则可以编写一个小的PHP脚本来呕吐所需的内容,并使用Ajax进行调用.
Maybe there are no usual sessions in Wordpress ... anyway, Wordpress knows the concept of users. You can manage information related to specific users with the the functions
add_user_meta
,update_user_meta
,get_user_meta
, anddelete_user_meta
.If you need the information saved in this way in JavaScript, you can write a little PHP script that vomits what you need, and call it with Ajax.
-
只有在用户登录后才能使用.This is only good if the user is logged in.
- 2
- 2016-11-10
- Tim Hallman
-
- 2016-12-05
在接收AJAX请求的PHP页面上,像这样设置$ _SESSION.
$car_color = <user selected form input data>; $_SESSION['car_color'] = $car_color;
访问$ _SESSION变量
if(isset($_SESSION['car_color'])) { $value = $_SESSION['car_color']; } else { $value = 'Please select a car color.'; }
本教程详细介绍了正确的设置和会议的处置.
On the PHP page that receives the AJAX request, set $_SESSION like this.
$car_color = <user selected form input data>; $_SESSION['car_color'] = $car_color;
Access the $_SESSION variable
if(isset($_SESSION['car_color'])) { $value = $_SESSION['car_color']; } else { $value = 'Please select a car color.'; }
This tutorial goes into more detail about proper setup and disposal of sessions.
-
- 2020-08-31
您必须首先在WordPress中启动会话,但不能像这样那样在WordPress中启动会话.
在开始会话之前,您需要检查某些插件是否较早启动了会话,因为您可能会遇到意外问题.因此,您需要为此使用一些算法.
if (version_compare(PHP_VERSION, '7.0.0', '>=')) { if(function_exists('session_status') && session_status() == PHP_SESSION_NONE) { session_start(apply_filters( 'cf_geoplugin_php7_session_options', array( 'cache_limiter' => 'private_no_expire', 'read_and_close' => false ))); } } else if (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '7.0.0', '<')) { if (function_exists('session_status') && session_status() == PHP_SESSION_NONE) { session_cache_limiter('private_no_expire'); session_start(); } } else { if(session_id() == '') { if(version_compare(PHP_VERSION, '4.0.0', '>=')){ session_cache_limiter('private_no_expire'); } session_start(); } }
我还在这里添加了对旧PHP版本的支持.
You must first start a session in WordPress but you can't start a session in WordPress just like that.
Before starting sessions, you need to check if some plugin started the session earlier because you may have unexpected problems. So you need a little algorithm for that.
if (version_compare(PHP_VERSION, '7.0.0', '>=')) { if(function_exists('session_status') && session_status() == PHP_SESSION_NONE) { session_start(apply_filters( 'cf_geoplugin_php7_session_options', array( 'cache_limiter' => 'private_no_expire', 'read_and_close' => false ))); } } else if (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '7.0.0', '<')) { if (function_exists('session_status') && session_status() == PHP_SESSION_NONE) { session_cache_limiter('private_no_expire'); session_start(); } } else { if(session_id() == '') { if(version_compare(PHP_VERSION, '4.0.0', '>=')){ session_cache_limiter('private_no_expire'); } session_start(); } }
I've also added support for older PHP versions here.
我有一个带有一些复选框和输入框选择框的表单,它通过ajax调用显示用户想要的内容.问题在于,当用户单击该项目并显示详细信息页面,然后决定返回上一页时,他需要单击并再次选择先前的选择.
我想让WP在单击详细信息页面的按钮时将所有选项存储在会话中,并在会话中保存实际信息,然后当他再次访问该页面时,这些值将在会话中记录并设置如果有的话.
可以在WP中完成吗?
如果是,怎么办?
让我们简化一下,并说我们的形式如下:
我不在表单中使用"提交"按钮,而是通过AJAX在输入更改时进行处理.
在通过ajax获得的结果中,我有一个指向详细信息页面的链接:
您知道如何在浏览器中重新访问/重新加载/返回按钮时将值存储在Session中并调用它们吗?
我需要能够读取会话存储的内容并通过Javascript使用它?并通过已经运行良好的ajax触发我的搜索功能.
我只需要存储(可能在按明细按钮的href中进入$ link并读取和发送会话变量(如果存在)之前.