dougui5419 2016-02-27 23:26
浏览 142

Wordpress update_user_meta无效

** SOLVED ** I had failed to require Wordpress in the form processor. Adding this line at the top fixed it (always the simplest thing, right!?): require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );

I've created a custom front-end profile for a client's site, and I'm adding a way for other users to favorite that profile. (Please don't suggest anything like BuddyPress or another plugin -- I've tried about a dozen, and none of them have all of the capabilities I need. Thanks.) :)

Anyway.... here is the overview. I hope it's clear. Let's say that USER 1 is viewing the profile of USER 10.

  • In the database, user 1 has a field called favorite_10 which can be set to "yes" or "no" (or null)
  • When someone clicks the "Favorite" button in another user's profile, it will run a simple script to change the value from "yes" to "no" or vice-versa

That's it. I think it's a pretty good solution, but the form processing script is breaking at the update_user_meta line.

Here we go.

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>

And here is 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']);

?>

After a lot of testing and debugging, I've figured out that it's breaking at update_user_meta ... but I don't know why.

Thank you!

  • 写回答

1条回答 默认 最新

  • dongou4052 2016-02-28 23:06
    关注

    ** SOLVED ** I had failed to require Wordpress in the form processor. Adding this line at the top fixed it (always the simplest thing, right!?): require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?