Wordpress passing ajax value to a specific page using Wordpress

前端 未结 3 786
南方客
南方客 2020-12-03 20:04

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

JavaScript:

         


        
相关标签:
3条回答
  • 2020-12-03 20:27

    Everything is correct except you have to add ajax_object.ajax_url rather than ajaxurl in url: parameter of $.ajax function as

    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.ajax_url**,
        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);
        }
    });  
    
    });
    

    copy and paste above script in place of your $.ajax function If you want to see what is ajax_object.ajax_url then alert it inisde your code and it will alert path to your admin ajax file which is required for using ajax in wordpress

    0 讨论(0)
  • 2020-12-03 20:28

    You are using wp_localize_script wrong. In the PHP code, remove the wp_localize_script line.

    Optionally you can (and should) add the following

    // this line is for users who are not logged in
    add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
    
    0 讨论(0)
  • 2020-12-03 20:40

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

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

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

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

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

    This should work now…

    References:

    • Using AJAX With PHP on Your WordPress Site Without a Plugin

    • How to use Ajax with your WordPress Plugin or Theme?

    0 讨论(0)
提交回复
热议问题