WordPress AJAX中的UPDATE_OPTION

时间:2015-04-05 作者:Oleg

我有一个例子:

For example, this code will register a callback called "wpa_49691":

add_action( \'wp_ajax_wpa_49691\', \'wpa_49691_callback\' );
add_action( \'wp_ajax_nopriv_wpa_49691\', \'wpa_49691_callback\' );
function wpa_49691_callback() {
    // Do whatever you need with update_option() here.
    // You have full access to the $_POST object.
}
然后,发布所需的任何数据,只需在对象中指定操作:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        var data = {
            action: \'wpa_49691\',
            my_var: \'my_data\'
        };

        jQuery.post( ajaxurl, data, function(response) {
            // handle response from the AJAX request.
        });
    });
</script>
此脚本将把您的数据发布到服务器。在服务器回调中,您将看到$\\u POST[\'my\\u var\']=“my\\u data”。

我认为代码是有用的。但我不知道怎么做……)我有很多问题:1。。。。只需在对象中指定操作-在何处以及如何指定此操作?如何使用函数wpa\\u 49691\\u callback()?函数wpa\\u 49691\\u callback()?是php还是js函数?例如,单击时调用JS脚本?

请给我一个工作示例。。

1 个回复
SO网友:Phil Johnston

第一块代码是PHP,第二块是javascript。

下面是一个完整的示例:

在PHP(主题的functions.PHP文件或插件文件)中:

//First enqueue your javascript in WordPress
function your_prefix_enqueue_scripts(){

    //Enqueue your Javascript (this assumes your javascript file is located in your plugin in an "includes/js" directory)
    wp_enqueue_script( \'your_unique_js_name\', plugins_url(\'js/yourjavascriptfile.js\', dirname(__FILE__) ), array( \'jquery\' ) );

    //OR (simpler but not recommended)  
    wp_enqueue_script( \'your_unique_js_name\', \'http://domain.com/myjavascriptfile.js\', array( \'jquery\' ) );

    //Here we create a javascript object variable called "youruniquejs_vars". We can access any variable in the array using youruniquejs_vars.name_of_sub_variable
    wp_localize_script( \'your_unique_js_name\', \'youruniquejs_vars\', 
        array(
            //To use this variable in javascript use "youruniquejs_vars.ajaxurl"
            \'ajaxurl\' => admin_url( \'admin-ajax.php\' ),

        ) 
    );  

}
add_action( \'wp_enqueue_scripts\', \'your_prefix_enqueue_scripts\' );
然后在PHP中,创建ajax回调函数:

//This is your Ajax callback function
function your_ajax_callback_function_name(){

    //Get the post data 
    $my_var = $_POST["my_var"];

    //Do your stuff here - maybe an update_option as you mentioned...

    //Create the array we send back to javascript here
    $array_we_send_back = array( \'test\' => "Test" );

    //Make sure to json encode the output because that\'s what it is expecting
    echo json_encode( $array_we_send_back );

    //Make sure you die when finished doing ajax output.
    die(); 

}
add_action( \'wp_ajax_\' . \'wpa_49691\', \'your_ajax_callback_function_name\' );
add_action( \'wp_ajax_nopriv_\' . \'wpa_49691\', \'your_ajax_callback_function_name\' );
然后在javascript文件中执行以下操作:

jQuery(document).ready(function($){

    /**
     * When your ajax trigger button is clicked 
     * (if the button\'s class is .my-button)
     *
     */
    $( document ).on( \'click\', \'.my-button\', function(event){

        event.preventDefault();

        // Use ajax to do something...
        var postData = {
            action: \'wpa_49691\',
            my_var: \'my_data\',
        }

        //Ajax load more posts
        $.ajax({
            type: "POST",
            data: postData,
            dataType:"json",
            url: youruniquejs_vars.ajaxurl,
            //This fires when the ajax \'comes back\' and it is valid json
            success: function (response) {

                alert( response.test );

            }
            //This fires when the ajax \'comes back\' and it isn\'t valid json
        }).fail(function (data) {
            console.log(data);
        }); 

    });

});

结束