update_option doesn't work in single php file

2019-09-21 09:27发布

问题:

In the admin page of my WordPress plugin, I have a function that makes an ajax call to a PHP file. If everything goes well this PHP file should update the option in WordPress. But for some reason, it doesn't work when I try to use update_option.

Here is my 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;
}

So everything goes well until the update_option. When I put them in, nothing happens anymore and there's a server error.

What am I doing wrong?

EDIT

So here is my jquery ajax code:

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:

As pointed out in the comments your file is outside wordpress environment, so it doesn't recognize the update_option function. Even knowing the ajax api was mentioned in the comments, I'm putting here what you should do:

  1. Require the activate.php file in the main plugin file if you aren't doing this yet. Something simple like require_once('admin/activate.php'); should do it.

  2. Hook an action into wordpress ajax using the wp_ajax hooks. You can place this code in your main plugin file or in activate.php (since it's required by the former).

    add_action( 'wp_ajax_my_plugin_activate', 'my_plugin_activate' );
    add_action( 'wp_ajax_nopriv_my_plugin_activate', 'my_plugin_activate' );
    
  3. Surround your activate.php code with the function named above, like this:

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

    Note that you don't have to test against $_GET['activate'] anymore.

  4. Change the url of the ajax post to wp-admin/admin-ajax.php and pass the action as the data attribute. This should be done localizing your script (as seen in the docs). For simplifing purposes I'm putting it here directly:

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

Excuse my English, it's not my mother language. Hope it helps!



标签: php wordpress