2016-02-27 55 views
1

**已解決** 我沒有在窗體處理器中需要Wordpress。添加此線在頂部固定它(總是最簡單的事情,對吧!?): require_once(explode("wp-content" , __FILE__)[0] . "wp-load.php");Wordpress update_user_meta不能正常工作

我創建了一個客戶的網站自定義前端的空間,我添加了其他用戶的方式以最喜歡該配置文件。 (請不要建議類似的東西BuddyPress的或其它插件 - 我已經試過了十幾,和他們都沒有所有我需要的功能感謝。):)

反正....這裏是概述。我希望這很清楚。比方說,用戶1查看用戶10

  • 在數據庫中的配置文件,用戶1有一個名爲favorite_10場,可設置爲「yes」或「no」(或空)
  • 當有人點擊另一個用戶配置文件中的「收藏」按鈕,它將運行一個簡單的腳本,將值從「是」更改爲「否」或反之亦然

就是這樣。我認爲這是一個非常好的解決方案,但表格處理腳本正在打破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 ...但我不知道爲什麼。

謝謝!

+0

只是一句話,有人敲出了相當多的代碼來解決你所描述的問題,你還沒有認識到他們的努力與upvotes /接受答案。至於這個問題,你有一個在你的HTML錯誤,你有一個空間名稱屬性,並沒有空間在您的PHP – David

+0

謝謝。我還沒有找到這些帖子,而且我搜索了很多 - 並且可能讀了一打 - 但我會繼續尋找。感謝您捕獲該類型-o。我不認爲這是問題,但我會解決它(在我的調試中,我已經成功調用腳本並通過腳本的每個部分獲取,直到update_user_meta,因此我相當確信這是問題所在)。 更新:該類型o實際上不在我的代碼中。只在帖子裏。 – NYCjbd

+0

你有沒有在你的自定義文件中加載wordpress? – David

回答

0

**已解決**我沒有在窗體處理器中需要Wordpress。添加此線在頂部固定它(總是最簡單的事情,對吧!?):require_once(explode("wp-content" , __FILE__)[0] . "wp-load.php");