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 ?
my_js_function
is never used. Why is that there at all? – s_ha_dum Dec 6 '13 at 15:18success
but I don't know how... – agis Dec 6 '13 at 16:10success: function()
should besuccess: function(data)
– Milo Dec 9 '13 at 16:40