Found a way to save settings using ajax when using the settings API.
The main mistake I made in my code is that I used the wrong URL.
Instead of using the standard ajaxurl
, which you usually use when creating ajax calls in wordpress; we use the action call of your api form settings, which is equal to options.php
.
Since we are using this URL, there is no need for the php function to process the request, as options.php handles all this for us.
Therefore, we only need to process the js function, which looks like this in my instance.
function save_main_options_ajax() { $('.main-options-form').submit( function () { var b = $(this).serialize(); $.post( 'options.php', b ).error( function() { alert('error'); }).success( function() { alert('success'); }); return false; }); } save_main_options_ajax();
That is, after saving, I received a warning about successful completion, and my options were saved.
Note: There is only one feature that I noticed. Upon completion of the POST
request and showing a warning of success, the page makes a GET
request for the version page of the parameter page, in which the parameters &settings-updated=true
added to the end of the URL.
I donβt know whether to worry about it, I did not encounter any problems, but in the end it may be something that needs to be considered.
source share