Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:47:52+00:00 2026-06-09T14:47:52+00:00

I have been wrestling with this problem for quite some time now. I have

  • 0

I have been wrestling with this problem for quite some time now.
I have an options page for a theme, and a single option registered.

I have been trying to get the option updated via ajax every time a user presses the save button, here is my code.

JS:

       function save_main_options_ajax() {

        $('.main-options-form').submit( function () { 

            var b       =  $(this).serialize(),
                optdata =  { action : "wp_ajax_main_options_save", data: b };

            $.post( ajaxurl, b, function (response) {
                if (response == 1 ) { alert('sucess'); }
                else { alert(optdata);}
            });
            return false;               
        });
    }
save_main_options_ajax();

The php:

 function main_options_save_ajax() { 

        check_ajax_referer('_wpnonce', '_wpnonce' );

        $data = $_POST;
        unset($data['option_page'], $data['action'], $data['_wpnonce'], $data['_wp_http_referer']);


        if ( update_option('main_options', $data ) )
        { die(1); }
        else { die (0); }           
}
add_action('wp_ajax_main_options_save', 'main_options_save_ajax' );

The response i see in firebug is 0. Im not sure what im missing here, i have tried this with some variations but nothing seems to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:47:54+00:00Added an answer on June 9, 2026 at 2:47 pm

    Found a way to save settings via ajax when using Settings API.

    The main mistake that i made in my code is that i used the wrong url path.

    Instead of using the standard ajaxurl which is what you would usually use when making ajax calls in wordpress; we use the action call of your settings api form, which is options.php.

    Because we use this url path, there is no need for a php function to handle the request as options.php handles all of this for us.

    Therefore we only need to handle 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 it, after saving i got the success alert, and my options were saved.

    Note: There is only one peculiarity that I noticed. After finishing the POST request, and showing the success alert, the page makes a GET request for a page version of your options page which has the parameters &settings-updated=true added onto the end of the url.

    I don’t know if this is something to be worried about, I have not encountered any problems, but it could be something to consider in the long run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a thought problem, and one I have been wrestling with for about
I have been wrestling for this for a while and I need some help.
Ok so I have been wrestling with this and now thought I ask for
Okay... I've been wrestling this error for at least an hour now. I have
I've been wrestling with this for a while now. I have an app that
I have been wrestling with this for a few days and after tons of
I've been wrestling with this problem for an hour or so and it seems
I've been wrestling with this for a while now. I'm not sure MYSQL query
So I have been wrestling with this issue all night..... I essentialls have my
I've been wrestling with this seemingly simple re-direct task for hours now and for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.