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.");
}
});
}
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:Require the
activate.php
file in the main plugin file if you aren't doing this yet. Something simple likerequire_once('admin/activate.php');
should do it.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).Surround your activate.php code with the function named above, like this:
Note that you don't have to test against
$_GET['activate']
anymore.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:Excuse my English, it's not my mother language. Hope it helps!