Add AJAX to Wordpress
$(document).ready(function () {
// Hall of fame call
if ( $('body').hasClass('page-template-page-club-fame') ) {
get_fame();
}
$('#load_more_fame').click(function(){
$('.loading').fadeIn();
get_fame();
});
// Recipes call
if ( $('body').hasClass('page-template-page-club-recipes') ) {
get_recipes();
}
$('#load_more_recipes').click(function(){
$('.loading').fadeIn();
var type = $('#club_recipes_header #filters li.active').attr('data-term');
get_recipes(type);
});
$('#club_recipes_header #filters li').click(function(){
var button = $(this);
var type = '';
if ( button.hasClass('active') ) {
button.removeClass('active');
$('#club_recipes_header #filters li.all').addClass('active');
type = 'all';
} else {
$('#club_recipes_header #filters li').removeClass('active');
button.addClass('active');
type = button.attr('data-term');
}
var reload = true;
get_recipes(type, reload);
});
// Youtube video overlay
$('.video_overlay.youtube').click(function(e){
$(this).fadeOut(function(){
$(this).next('iframe')[0].src += "&autoplay=1";
e.preventDefault();
});
});
// Vimeo video overlay
var iframe = document.getElementById('vimeo_iframe');
var player = $f(iframe);
$('.video_overlay.vimeo').click(function(){
$(this).fadeOut(function(){
player.api("play");
});
});
});
})(jQuery);
/* AJAX */
wp_register_script(’ajax_js', get_template_directory_uri() . '/js/ajax.js', false, '1.0.0' );
wp_enqueue_script(‘ajax_js');
wp_localize_script(‘ajax_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php')));
// Ajax call
add_action('wp_ajax_load_recipes', 'load_recipes');
add_action('wp_ajax_nopriv_load_recipes', 'load_recipes');
function load_recipes(){
$offset = $_POST['offset'];
$limit = $_POST['limit'];
$type = $_POST['type'];
$type = preg_replace('/\s+/', '', $type);
$reload = $_POST['reload'];
$args = array('post_type' => 'recipes', 'post_status' => 'publish', 'posts_per_page' => $limit, 'offset' => $offset, 'order' => 'DESC', 'orderby' => 'date');
if ( isset($type) && ($type != 'all') ) {
$args['tax_query'] = array(array('taxonomy' => 'types','field' => 'term_id','terms' => array($type)));
}
$the_query = new WP_Query($args);
$all = $the_query->post_count;
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
/* Content goes here */
endwhile;
else :
echo '<h2>No posts</h2>';
endif;
exit;
}
// Ajax for Club Free Recipes
function get_recipes(type, reload){
var offset = parseInt($('#load_more_recipes').attr('data-offset'));
var limit = parseInt($('#load_more_recipes').attr('data-limit'));
if ( typeof(type) === 'undefined' ) {
type = 'all';
}
if ( reload === true ) {
offset = 0;
}
$.ajax({
type : 'POST',
url : ajax_object.ajax_url,
data : {
action : 'load_recipes',
offset : offset,
limit : limit,
type : type,
reload : reload
},
success : function (response) {
if ( $(response).length < limit ) {
$(".button_holder").fadeOut();
} else {
$(".button_holder").fadeIn();
}
var x = 0;
$(response).each(function(){
var $this = $(this);
x = x + 100;
if ( reload === true ) {
$('#club_recipes_content #recipes_wrapper').html('').append(function () {
setTimeout(
function () {
$this.hide().appendTo('#club_recipes_content #recipes_wrapper').fadeIn('slow');
},
x
);
}
);
} else {
$('#club_recipes_content #recipes_wrapper').append(function () {
setTimeout(
function () {
$this.hide().appendTo('#club_recipes_content #recipes_wrapper').fadeIn('slow');
},
x
);
}
);
}
});
offset = offset + limit;
limit = limit + 4;
$('#load_more_recipes').attr('data-offset', offset);
$('.loading').fadeOut();
}
});
}