保存字$ P $与阿贾克斯PSS设置的API选项, [英] Saving wordpress settings api options with ajax,

查看:129
本文介绍了保存字$ P $与阿贾克斯PSS设置的API选项,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在摔跤这个问题相当长的一段时间了。 我有一个选项页的主题,和一个选项注册。

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.

我一直在试图获得通过AJAX的每个用户presses保存按钮时更新的选项,这里是我的code。

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

记者:

       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();

PHP的:

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' );

我在Firebug中看到的响应是0林不知道我在这里失踪,我都试图与一些变化,但似乎没有任何工作。

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.

推荐答案

找到了一种方法,通过使用AJAX API设置时,保存​​设置。

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

这是我在做我的code的主要错误是,我使用了错误的URL路径。

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

而不是使用标准的 ajaxurl 这是你做的话preSS Ajax调用时会ussaly用什么的;我们使用您的设置的API形式,这是 options.php 的行动电话。

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

由于我们使用这个URL路径,没有必要对一个PHP函数来处理请求的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.

因此​​,我们只需要处理的js函数看起来像这样在我的实例。

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.

注意:这里只有一个preculiarity,我注意到了。完成 POST 请求,并显示出成功的警报后,页面使得 GET 请求的网页版本的选择网页,其中有参数&安培;设置 - 更新=真添加到URL的末尾。

Note: There is only one preculiarity 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.

这篇关于保存字$ P $与阿贾克斯PSS设置的API选项,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆