I want to ajax function declared in function Widget in widget Class ,it have arguments belong to the widget i try this code
add_action('wp_ajax_my_func', 'my_func');
add_action('wp_ajax_nopriv_my_func', 'my_func');
but when call it by ajax it Returned nothing , i use console.log for data response it returned 0 ,when i moved the function to function.php it work.
it's possible to works in the widget class??
my widget function
public function widget( $args, $instance ) {
// outputs the content of the widget
extract( $args );
$title = $instance['title'];
$number = $instance['number'];
//ajax func.
function loadMoreP($number){
query_posts('showposts='.$number.'');
while(have_posts()) : the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a>'.the_post_thumbnail('thumbnail').'</li><br>';
endwhile;
wp_reset_query();
die();
}
add_action('wp_ajax_loadMoreP', array($this,'loadMoreP'));
add_action('wp_ajax_nopriv_loadMoreP', array($this,'loadMoreP'));
echo $before_widget;
echo $before_title .$title.$after_title;
echo '<ul class="LatestPosts">';
echo '</ul>';
echo '<br><br><input type="button" id="loadMoreW" value="Load More" data-offset="'.$number.'">';
echo $after_widget;
}
and the ajax code
$('#loadMoreW').on('click',function(){
$.ajax({
url: 'wp-admin/admin-ajax.php',
type: 'POST',
data: {
action : 'loadMoreP',
offset: $('#loadMoreW').data('offset')
},
success: function(data){
console.log($('#loadMoreW').data('offset'));
console.log(data);
$('#loadMoreW').data('offset', $('#loadMoreW').data('offset')+2);
$(data).appendTo('.LatestPosts');
}
});
});