Ajaxing function in widget class

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');
        }
    });

});

1 Answer
1

If this is in a widget class, you need…

add_action('wp_ajax_my_func', array($this,'my_func'));

… not just…

add_action('wp_ajax_my_func', 'my_func');

However, I suspect there to be more problems than just that due to the context you are using this in. But without more code, that is all I’ve got.

Based on new information in the question…

Both your function definitions and the add_action code is nested inside the widget method of your Widget. That method only runs when the widget displays on the front end, so nothing inside it is going to execute for a AJAX request. You need to pull that code out of the widget method and then hook it in the Widget’s constructor, something like this:

public function __construct() {
  // other constructor stuff

  add_action('wp_ajax_loadMoreP',     array($this,'loadMoreP'));
  add_action('wp_ajax_nopriv_loadMoreP',    array($this,'loadMoreP'));
}

//ajax func.
public 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();
}

That is pseudo-code really. It is completely untested and probably won’t work without being adapted to your particular widget.

Also, Please don’t use query_posts.

Leave a Comment