Wordpress 使用 Wordpress 将 ajax 值传递给特定页面

问题描述:

我想将变量传递给特定页面.我找到了一个简单的例子,解释了如何在 wordpress 中使用 ajax.

I would like to pass a variable to a specific page. I found a simple example explaining how to use ajax with wordpress.

JavaScript:

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

// We'll pass this variable to the PHP function example_ajax_request
var fruit = 'Banana';

// This does the ajax request
$.ajax({
    url: ajaxurl,
    data: {
        'action':'example_ajax_request',
        'fruit' : fruit
    },
    success:function(data) {
        // This outputs the result of the ajax request
        console.log(data);
    },
    error: function(errorThrown){
        console.log(errorThrown);
    }
});  

});

functions.php

function example_ajax_request() {

// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {

    $fruit = $_REQUEST['fruit'];

    // Let's take the data that was sent and do something with it
    if ( $fruit == 'Banana' ) {
        $fruit = 'Apple';
    }

    // Now we'll return it to the javascript function
    // Anything outputted will be returned in the response
    echo $fruit;

    // If you're debugging, it might be useful to see what was sent in the $_REQUEST
    // print_r($_REQUEST);

}

// Always die in functions echoing ajax content
  die();
 }

add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );


   wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' =>   admin_url( 'admin-ajax.php' ) ) );

不幸的是我不能传递变量.我检查了代码并收到此错误:

Unfortunately I cannot pass the variable. I inspected the code and I get this error:

Error: ajax_object is not defined

您是否知道获得相同结果的另一种方法?

Do you maybe know another way to obtain the same result?

离你很近,但还缺少一些小东西……

You are very near, but there is some little things missing…

我在评论中的意思是,您需要在两者中使用 'ajax-script' 以这种方式使用它:

What I mean in my comment, is that you need to use it this way using 'ajax-script' in both:

add_action('wp_enqueue_scripts', 'add_js_scripts'); 
add_js_scripts(){
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
    wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
}

$_REQUEST 更改为 $_POST:

Changed $_REQUEST to $_POST:

function example_ajax_request() {

    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_POST) ) {

        $fruit = $_POST['fruit'];

        // Let's take the data that was sent and do something with it
        if ( $fruit == 'Banana' ) {
            $fruit = 'Apple';
        }

        // Now we'll return it to the javascript function
        // Anything outputted will be returned in the response
        echo $fruit;

        // If you're debugging, it might be useful to see what was sent in the $_POST
        // print_r($_POST);

    }

    // Always die in functions echoing ajax content
      die();

 }

添加了add_action('wp_ajax_nopriv_...):

add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' ); // <= this one
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );

对于您的 jQuery 脚本 script.js 文件,有 2 个重要的缺失小东西:

For your jQuery script script.js file, there is 2 important missing little things:

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

    /* We'll pass this variable to the PHP function example_ajax_request */
    var fruit = 'Banana';

    /* This does the ajax request */
    $.ajax({
        url: ajax_object.ajaxurl, /* <====== missing here */
        type : 'post', /*    <========== and missing here */
        data: {
            'action':'example_ajax_request',
            'fruit' : fruit
        },
        success:function(data) {
            /* This outputs the result of the ajax request */
            console.log(data);
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });  

});

现在应该可以了……

参考文献:

如何将 Ajax 与您的 WordPress 插件或主题一起使用?