如何将变量传递给wp_ajax操作?

时间:2015-04-14 作者:Finne

我正在编写一个插件,从外部API提取数据并将其插入bbPress主题。下面是一些示例代码:

function bbp_ajax() {
    $topic_id = bbp_get_topic_id();
    $the_issue_key = get_post_meta( $topic_id, \'bbp_jira_issue_field\', true);
    $nonce = wp_create_nonce( \'theNonce\' );

    // jQuery AJAX Code, actions bbpAjax...
}
add_action ( \'bbp_template_before_replies_loop\', \'bbp_ajax\');

function get_ajax_content() {
    check_ajax_referer( "theNonce" );
    $api_url = get_option( \'jifbs_url\' );
    $fullurl = $api_url.$the_issue_key;

    $username = get_option( \'jifbs_username\' );

    // Then I pull in the data with cURL and echo it. But I need $the_issue_key to complete the API auth
}

add_action( \'wp_ajax_bbpAjax\', \'get_ajax_content\' );
add_action( \'wp_ajax_nopriv_bbpAjax\', \'get_ajax_content\' );
我不知道怎么通过$the_issue_key 进入get_ajax_content 作用我尝试将其设置为全局(抖动),但它总是返回null。

$the_issue_key = get_post_meta( $topic_id, \'bbp_jira_issue_field\', true); 在get\\u ajax\\u content函数中运行返回null。

是否有特定于wp_ajax 我可能需要知道的行动——或者我是否遗漏了一些非常明显的东西?

1 个回复
最合适的回答,由SO网友:Phil Johnston 整理而成

您需要将变量的值放到页面上,以便javascript可以使用ajax将其传递回PHP。Ajax函数是一个完全独立的“页面加载”,所以即使您将其设置为全局,它也无法工作。最好的方法是使用“wp\\u localize\\u script”(https://codex.wordpress.org/Function_Reference/wp_localize_script)

下面是一个完整的示例,说明了如何做到这一点:

//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\' ),
                //To use this variable in javascript use "youruniquejs_vars.the_issue_key"
                \'the_issue_key\' => $the_issue_key,

            ) 
        );  

    }
    add_action( \'wp_enqueue_scripts\', \'your_prefix_enqueue_scripts\' );

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

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

        //Do your stuff here

        //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_\' . \'your_ajax_callback_function_name\', \'your_ajax_callback_function_name\' );
    add_action( \'wp_ajax_nopriv_\' . \'your_ajax_callback_function_name\', \'your_ajax_callback_function_name\' );
然后在javascript文件中执行以下操作:

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

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

        event.preventDefault();

        // Use ajax to do something...
        var postData = {
            action: \'your_ajax_callback_function_name\',
            the_issue_key: youruniquejs_vars.the_issue_ke,
        }

        //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);
        }); 

    });

});

结束

相关推荐

在footer.php中实现函数的正确方式是什么

我有一个Wordpress网站,主题是我创建的“Twenty15”。我添加了一段PHP代码,该代码根据显示的模板更改页脚背景。这是我的代码部分:/************************/ /***** Footer Color *****/ /************************/ $this_template = get_current_template(); //echo $this_template; switch ($thi