【发布时间】:2016-06-11 03:32:10
【问题描述】:
** 已解决 **
我没有在表单处理器中要求 Wordpress。在顶部添加这一行修复它(总是最简单的事情,对吧!?):
require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );
我已经为客户的网站创建了一个自定义前端配置文件,并且我正在添加一种方式让其他用户收藏该配置文件。 (请不要建议像 BuddyPress 或其他插件之类的东西——我已经尝试了大约十几个,但没有一个具备我需要的所有功能。谢谢。):)
无论如何....这里是概述。我希望这很清楚。假设 USER 1 正在查看 USER 10 的个人资料。
- 在数据库中,用户 1 有一个名为 favorite_10 的字段,可以设置为“yes”或“no”(或 null)
- 当有人点击另一个用户配置文件中的“收藏”按钮时,它会运行一个简单的脚本来将值从“是”更改为“否”,反之亦然
就是这样。我认为这是一个很好的解决方案,但表单处理脚本在update_user_meta 行中断。
我们开始吧。
foreach($sitterlist as $sitteritem) {
$code = 'favorite_'.(esc_html($sitteritem->ID));
$key = esc_html($sitteritem->ID);
$favorite[$key] = get_the_author_meta( $code, $currentuser);
}
$is_favorite=null;
if ($favorite[$usertosearch]=='yes') {
$is_favorite = true;
} else {
$is_favorite = false;
}
?>
<form method="post" action="updatefavorite.php">
<input type="hidden" value="<?php echo $usertosearch; ?>" name="usertosearch" id="usertosearch">
<input type="hidden" value="<?php echo $currentuser; ?>" name="currentuser" id="currentuser">
<input type="hidden" value="<?php echo $is_favorite; ?>" name="is_favorite" id="is_favorite">
<button type="submit" id="favoritebutton" name="favoritebutton"><span>Favorite</span></button>
</form>
这里是updatefavorite.php
<?php
if(isset($_POST['favoritebutton'])) {
$currentuserid = $_POST['currentuser'];
$currentprofileid = $_POST['usertosearch'];
$currentfavorite = $_POST['is_favorite'];
$code = 'favorite_'.$currentprofileid;
if ($currentfavorite) {
$currentfavorite = 'no';
} else {
$currentfavorite = 'yes';
}
update_user_meta($currentuserid,$code,$currentfavorite);
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
经过大量测试和调试,我发现它在 update_user_meta 处出现故障……但我不知道为什么。
谢谢!
【问题讨论】:
-
只是一个评论,有些人输入了相当多的代码来解决您描述的问题,您还没有认识到他们在支持/接受答案方面所做的努力。至于这个问题,你的 html 中有一个错误,你的 php 中有一个带空格的 name 属性,没有空格
-
谢谢。我还没有找到这些帖子,我已经搜索了很多 - 可能阅读了十几个 - 但我会继续寻找。谢谢你抓住那个类型-o。我认为这不是问题,尽管我会修复它(在我的调试中,我已经成功调用了脚本并通过了脚本的每个部分,直到 update_user_meta,所以我非常有信心这就是问题所在)。更新:那个 type-o 实际上不在我的代码中。仅在帖子中。
-
您是否在自定义文件中加载了 wordpress?
-
哦!它总是那么明显。谢谢你。我添加了这一行
require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );,它非常完美。谢谢!