Sending shortcode string to admin-ajax
-
AJAX and PHP aren’t my strengths, but I’m attempting to create a call that swaps out an existing plugin gallery with a new one. It works for the most part, but the gallery it returns is wrong, same as if the string inside of the shortcode is empty.
Could I get an extra set of eyes on this to make sure it isn’t some syntax error?
First the inital shortcode is rendered into the html. Clicking a link sends a new parameter to a js function in the header:
<div id="vidGalleryContent"> [tubepress mode="playlist" resultsPerPage='20' output='searchResults' searchProvider='youtube' playlistValue='PLMffcFdm_7-HT1HiVkNEguRtiYkhgq0TC'] </div> <a href="#" onclick="swapVidGallery('PLMffcFdm_7-Hi6NibMNpEuc8rlqbBNxC8')">Change gallery</a>Then the header script puts the parameter string inside a new shortcode string and sends it off to functions.php:
function swapVidGallery(vidID){ var newGalleryParameter = "[tubepress resultsPerPage='20' output='searchResults' searchProvider='youtube' mode='playlist' playlistValue='"+vidID+"']"; jQuery.ajax({ type:"POST", url: 'http:\/\/mysite.com\/wp-admin/admin-ajax.php', data: {action: 'newVidGallery', 'newGalleryShortcode':newGalleryParameter}, success: function(result){ jQuery('#vidGalleryContent').html(result); console.log(result); } }); };… And finally functions.php renders the shortcode with do_shortcode() and sends the html back to the page:
add_action('wp_ajax_newVidGallery', 'newVidGallery_function'); add_action('wp_ajax_nopriv_newVidGallery', 'newVidGallery_function'); function newVidGallery_function() { $newVidGalleryPOST = $_POST['newGalleryShortcode']; echo do_shortcode($newVidGalleryPOST); die(); }At least that’s what’s *supposed* to be happening. What’s going wrong?
And help is appreciated. ~gyz
The topic ‘Sending shortcode string to admin-ajax’ is closed to new replies.