【问题标题】:update_option doesn't work in single php fileupdate_option 在单个 php 文件中不起作用
【发布时间】:2016-03-02 13:12:33
【问题描述】:

在我的 WordPress 插件的管理页面中,我有一个对 PHP 文件进行 ajax 调用的函数。如果一切顺利,这个 PHP 文件应该会更新 WordPress 中的选项。但是由于某种原因,当我尝试使用 update_option 时它不起作用。

这是我的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;
}

所以在更新选项之前一切都很顺利。当我把它们放进去时,什么也没有发生,而且出现了服务器错误。

我做错了什么?

编辑

这是我的 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.");
            }
        });
    }

【问题讨论】:

  • 你得到什么输出? successfailednotSet。当你说有一个服务器错误时,它是什么?它在php错误日志中吗? nginx/apache 错误日志?
  • “并且出现服务器错误。” - 究竟是什么?
  • 如果我删除 update_option。我会得到“成功”
  • update_option 是 WordPress 的一个功能
  • 如果你在插件中使用Ajax,你应该follow the standard WordPress method;使用回调意味着您可以使用所有 WordPress 方法,而无需手动修改包括内容。

标签: php wordpress


【解决方案1】:

正如 cmets 中所指出的,您的文件位于 wordpress 环境之外,因此它无法识别 update_option 函数。即使知道 cmets 中提到了 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 帖子的 url 更改为 wp-admin/admin-ajax.php 并将操作作为数据属性传递。这应该通过本地化您的脚本来完成(如文档中所示)。为了简单起见,我直接把它放在这里:

    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 }
    })
    

对不起,我的英语不是我的母语。希望能帮助到你!

【讨论】:

  • 感谢@FelipeElia。我现在找到了一种方法。但这要好得多。如果我有时间,我会改用它。
猜你喜欢
  • 2011-02-19
  • 1970-01-01
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多