0

I want to load some dynamic data with jQuery after document load. For example, let it be tag cloud:

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#sidebar-tags').load('<?php echo bloginfo('template_url'); ?>/tag_cloud.php');
});
</script>

And in tag_cloud.php something like this:

<? wp_tag_cloud(''); ?>

And ofcoz it doesn't work, "Internal server error". How can i make this idea work?

Solved:

Followed Plamen Nikolov's link, came up with this:

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery.ajax({
         type : "post",
         dataType : "json",
         url : "/wp-admin/admin-ajax.php",
         data : ({action: "get_tag_cloud"}),
         success: function(response) {
            alert(response);}
         })
});
</script>

and in functions.php:

add_action('wp_ajax_get_tag_cloud', 'get_tag_cloud');
add_action('wp_ajax_nopriv_get_tag_cloud', 'get_tag_cloud');
function get_tag_cloud() {
    echo json_encode(wp_tag_cloud('echo=0'));
    die();
}

It almost works: if I check it with Firebug, I can see the response, but the alert doesn't pop up.

1
  • try executing the URL directly in browser. The error is in that page. Commented Nov 12, 2012 at 10:29

3 Answers 3

1

To send a XHR request (Ajax) you must stick to the recommended way, using the proper hooks:

See the examples here: WordPress Ajax hooks

Sign up to request clarification or add additional context in comments.

Comments

0

The code functionality in tag_cloud.php is wrong and it responses internal server error. In order to develop wordpress widget you can have a look at following tutorial

http://wp.tutsplus.com/tutorials/plugins/creating-a-wordpress-network-widget

Also be sure that, you are calling correct url in jquery load function.

Comments

0
$("#sidebar").html($.get('<?php echo bloginfo("template_url"); ?>/tag_cloud.php'); 

You have to either escape single-quotes around template_url or use double-quotes. Read your server logs for further debugging.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.