2016-03-02 30 views
-2

在我的WordPress插件的管理頁面中,我有一個函數可以調用一個PHP文件的ajax。如果一切順利,這個php文件應該更新wordpress中的選項。但由於某些原因,當我嘗試使用update_option時,它不起作用。update_option不能在單個php文件中工作

這裏是我的activate.php:

if(isset($_GET['activate']) && $_GET['activate'] == "true") { 
    if(isset($_REQUEST['txtAC']) && isset($_REQUEST['txtKey'])) { 
     $ac = $_REQUEST['txtAC']; 
     $key = $_REQUEST['txtKey']; 

     if($ac != "" && $key != "") { 
      $api_url = "http://192.168.2.75/wouter/yii2/basic/web/cars/activate/" . $ac . "/" . $key; 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_URL, $api_url); 
      $result = curl_exec($ch); 
      curl_close($ch); 
      $count = json_decode($result); 
      if($count == 1){ 
       update_option("ac", $ac); 
       update_option("auth_key", $key); 
       echo "success"; 
      } else { 
       echo "failed"; 
      } 
      return; 
     } else { 
      echo "notSet"; 
      return; 
     } 
    } 
    return; 
} 

所以一切順利,直到該update_option。當我把它們放入時,什麼都沒有發生,並且服務器錯誤。

我在做什麼錯?

編輯

因此,這裏是我的jQuery Ajax代碼:

function activatePlugin() { 
    jQuery("#acError").html(""); 
    jQuery("#keyError").html(""); 

    var txtAC = document.getElementById("txtAC").value; 
    var txtKey = document.getElementById("txtKey").value; 

    if(txtAC == "") { 
     jQuery("#acError").html("Vul een klantnummer in"); 
    } 
    if(txtKey == "") { 
     jQuery("#keyError").html("Vul een key in"); 
    } 
    if(txtAC != "" && txtKey != "") { 
     jQuery.ajax({ 
      url: "../wp-content/plugins/autocommerce/admin/activatePlugin.php?activate=true", 
      method: "POST", 
      data: { txtAC : txtAC, txtKey: txtKey } 
     }).done(function(msg) { 
      alert(msg); 
      if(msg == "success") { 
       location.reload(); 
      } else if(msg == "failed") { 
       jQuery("#activateError").html("Gegevens onjuist. Controleer uw gegevens en probeer het opnieuw."); 
      } else if(msg == "notSet") { 
       jQuery("#activateError").html("Een of meerdere velden zijn onjuist ingevuld."); 
      } else { 
       alert(msg); 
       jQuery("#activateError").html("Er is een fout opgetreden. Probeer het later opnieuw."); 
      } 
     }); 
    } 
+1

你會得到什麼輸出? '成功','失敗'或'notSet'。當你說有服務器錯誤時,它是什麼?它在PHP錯誤日誌? nginx/apache錯誤日誌? – Latheesan

+1

*「和theres一個服務器錯誤。」* - 究竟是什麼* * –

+0

如果我刪除update_option。我會得到「成功」 –

回答

2

正如在評論中指出的文件以外WordPress的環境,所以它不承認update_option功能。即使知道了AJAX API是在評論中提到的,我把這裏你應該做的:

  1. 要求的activate.php文件中的主要插件文件,如果你不這樣做呢。像require_once('admin/activate.php');這樣的簡單應該做到這一點。

  2. 使用wp_ajax掛鉤將動作掛鉤到wordpress ajax中。你可以將這段代碼放在你的主插件文件或者activate.php文件中(因爲它是前者所要求的)。

    add_action('wp_ajax_my_plugin_activate', 'my_plugin_activate'); 
    add_action('wp_ajax_nopriv_my_plugin_activate', 'my_plugin_activate'); 
    
  3. 與上面提到的功能,這樣您的環繞代碼activate.php:

    function my_plugin_activate() { 
        if(isset($_REQUEST['txtAC']) && isset($_REQUEST['txtKey'])) { 
         $ac = $_REQUEST['txtAC']; 
         $key = $_REQUEST['txtKey']; 
    
         // the code... 
    
        } 
        wp_die(); 
    } 
    

    請注意,你不必再次進行測試$_GET['activate']了。

  4. 將ajax帖子的網址更改爲wp-admin/admin-ajax.php並將該操作作爲數據屬性傳遞。這應該完成本地化腳本(如文檔所示)。對於simplifing目的,我把它直接在這裏:

    jQuery.ajax({ 
        url: "../wp-admin/admin-ajax.php", // you should use wp_localize_script in your PHP code and the defined variable here 
        method: "POST", 
        data: { action: 'my_plugin_activate', txtAC : txtAC, txtKey: txtKey } 
    }) 
    

原諒我的英語,這不是我的母語。希望能幫助到你!

+0

謝謝@FelipeElia。我現在找到了一種方法來做到這一點。但這樣好多了。如果我有時間,我會用它來代替。 –

相關問題