Powered By Blogger
Showing posts with label ajax. Show all posts
Showing posts with label ajax. Show all posts

Monday, August 28, 2017

get template result in a variable by wp-admin-ajax OR get load template data in a variable

function.php :

function prefix_ajax_activity_custom() {

  $pageNumber = $_POST['pnumber'];
  $activitlist = 'include='.$the_act_list.'&max=20&per_page=20&page=2';
    if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $activitlist) ) :    
      while ( bp_activities() ) : bp_the_activity();
        ob_start();
        locate_template( array( 'buddypress/activity/entry.php' ), true, false );
        $var .= ob_get_contents();
        ob_end_clean();
      endwhile;
    endif;

    wp_send_json_success($var);
}

add_action( 'wp_ajax_activity_custom', 'prefix_ajax_activity_custom' );


script.js:

function activitycustom( pnumber ) {

jQuery.post(
ajaxurl, 
{
'action': 'activity_custom',
   'pnumber': pnumber,
},
function(response){
       var response = response.data;
$( ".ajax" ).append( response );
}
);

}


ref : https://stackoverflow.com/questions/5817726/wordpress-save-get-template-part-to-variable

Tuesday, March 28, 2017

call wordpress function in jquery ajax

https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29


jQuery.post(
    ajaxurl, 
    {
        'action': 'add_foobar',
        'data':   'foobarid'
    }, 
    function(response){
        alert('The server responded: ' + response);
    }
);
function.php
add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );

function prefix_ajax_add_foobar() {
    // Handle request then generate response using WP_Ajax_Response

    // Don't forget to stop execution afterward.
    wp_die();
}