WordPress.org

Ready to get started?Download WordPress

Forums

Using ajax with shortcode (1 post)

  1. little_kostas
    Member
    Posted 1 year ago #

    Heres an easy for you all!
    I want to use next gen gallery using ajax to load the shortcode of my page. I use some links inside the page so than when i click them to load the new next gen shortcode without reloading the page.

    Things i have done:
    1. I have added the following shortcode in the body of my webpage:

    <div id="gallery"><?php echo do_shortcode("[slideshow id=1 w=800 h=100]"); ?></div>

    2.Inside a page i have added the following links:

    <a href="#" onClick="show(1);" id="gallery">Click to show Gallery 1</a>
    <a href="#" onClick="show(2);" id="gallery">Click to show Gallery 2</a>

    3.The ajaxcode:

    <script type="text/javascript">
    // var site = $site;
    function show(num){
    	var gallery_id = num;
    	var xmlhttp;
    	if (window.XMLHttpRequest)
    	  {// code for IE7+, Firefox, Chrome, Opera, Safari
    	  xmlhttp=new XMLHttpRequest();
    	  }
    	else
    	  {// code for IE6, IE5
    	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	  }
    	xmlhttp.onreadystatechange=function()
    	  {
    	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	    {
    	    document.getElementById("gallery").innerHTML=xmlhttp.responseText;
    	    }
    	  }
    	xmlhttp.open("POST","wp-content/themes/twentytwelve/ajax_info.php",true);
    	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	xmlhttp.send("gallery=" + gallery_id);
    	gallery.innerHTML="requesting...";
    }
    </script>

    4.The ajax info file contains the following:

    <?php echo do_shortcode("[slideshow id=".$_POST['gallery']." w=800 h=100]"); ?>

    But i get a php error that the shortcode is not recognised.
    Any solution/ideas for fixing that?? Or any other approach??

    Thank you in advance!
    Kostas

Topic Closed

This topic has been closed to new replies.

About this Topic