0

I'm using an wordpress theme and I want to load content through AJAX inside a page.

I've added this inside my functions.php :

function my_ajax_files()
{

     wp_localize_script( 'function', 'my_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
     wp_enqueue_script( 'function', get_template_directory_uri().'/my_js_stuff.js', 'jquery', true);

}
add_action('template_redirect', 'my_ajax_files');

function get_my_comments() {


    global $current_user;
    get_currentuserinfo();

    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';


    $args = array(
    'user_id' => $current_user->ID, // use user_id
    'post_type' => 'debate'
     );
    $comments = get_comments($args);


    echo '<ol class="commentlist">';

              wp_list_comments(
  array(
    'per_page' => 10, //Allow comment pagination
    'reverse_top_level' => false //Show the latest comments at the top of the list
  ), 
  $comments
);

     echo '</ol>';  

}

add_action("wp_ajax_nopriv_get_my_comments", "get_my_comments");
add_action("wp_ajax_get_my_comments", "get_my_comments");

and this file my_js_stuff.js contains this :

jQuery(document).ready( function() {
 jQuery( "#target" ).click(function() {
 jQuery.ajax({
 url: my_ajax_script.ajaxurl,
 data: ({action : 'get_my_comments'}),
 success: function() {
 jQuery("#content").append(data);  
 }
 });
  });
  });

Inside the page where I want to get the content from the function I have added a div with an id of target, but when I click on it nothing happens, I've checked also the console and I don't have any errors.

What is wrong ?

  • 1
    my_js_function is never used. Why is that there at all? – s_ha_dum Dec 6 '13 at 15:18
  • You're right, I've forgot about that, now it's removed. – agis Dec 6 '13 at 16:08
  • Probably I do need to retrieve the data on success but I don't know how... – agis Dec 6 '13 at 16:10
  • @Alecs you can't localize a script you haven't yet enqueued, reverse the order of those two calls. – Milo Dec 9 '13 at 15:56
  • 1
    success: function() should be success: function(data) – Milo Dec 9 '13 at 16:40
1

Is this the correct way to do it ?

This is not the correct way to do this.

What you are doing, first, doesn't make sense conceptually. When you load that page (assuming it loaded correctly) you would be loading the entire WordPress page, not just the comments. That is, you will load <html> all the way through </html>. There is no point to that. All you want are the comments.

Second, WordPress provides a mechanism to centralize AJAX requests. It is called the AJAX API and once you get the hang of it you will never try anything else. Use that.

There are numerous questions on this site about using the AJAX API, including this one by me.

Do some research. Try to do this right. Then edit the question with particular problems that you are having.

Edit:

I took a look at your edit and...

You need die() at the end of your Ajax callback, per the Codex.

If my_ajax_script.ajaxurl is not set correctly nothing will work. Look at the source of the page and confirm that.

  • I've edited my question with more relevant info, but I still can not make it to work, can you please take a look over my question ? Thanks! – agis Dec 6 '13 at 14:31
  • I've added die(); inside the function get_my_comments() and regarding the my_ajax_script.ajaxurl I've looked the the source code and I've found just this <script type='text/javascript'> /* <![CDATA[ */ var MyAjax = {"ajaxurl":"http:\/\/thehue.co\/wp-admin\/admin-ajax.php"}; /* ]]> */ </script> – agis Dec 9 '13 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.