简体   繁体   中英

How to pass a php variable to an Wordpress AJAX call?

I have embeded a post (CPT) by a shortode. So I get the output of this post on a page, where I want to filter the content of this embeded post by AJAX. That would work, if I could send the ID of this embeded post in the ajax call.

I get the ID of the embeded post from a shortcode [documentlist listid="2126"].

$atts = shortcode_atts(
        array(
            'listid' => '',
        ), $atts
    );
$posts = array(
    'post_type' => 'documentlist',
    'page_id'         => $atts['listid'],
);

So I have this $atts['listid'] variable. How can I add this to the following ajax code (which is in an diffeent scripts.js file)?

/*The ajax call*/
jQuery(function($){
    $('#filter #documenttypefilter, #filter #applicationareasfilter').change(function(){
        var filter = $('#filter');
        var serializedFilter = filter.serialize();
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(),  // form data
            type:filter.attr('method'), // POST

            success:function(data){
                $('#response').html(data); // insert data
            }
        });
        return false;
    });
});

Thanks so much for your help!

I found something close to here: How to pass php variable to wordpress AJAX handler

Get your PHP variable as below

var php_var = "<?php echo $atts['listid']; ?>";

Now use it in your AJAX call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM