admin管理员组文章数量:1130349
I am trying to load more content with ajax. the script does not return anything when I am logged out.
this is my php code
<?php
function get_blog_items()
{
$offset = (int)intval($_POST['offset']);
$args = array(
'posts_per_page' => 4,
'post_type' => 'post',
'offset' => $offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'template-parts/list', 'blog' );
endwhile;
endif;
wp_reset_postdata();
}
function add_ajax_actions() {
add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' );
}
add_action( 'admin_init', 'add_ajax_actions' );
And my javascript
$(document).ready(function($) {
var loadbuttonBlogItems = $('#loadmore-blog-items');
loadbuttonBlogItems.on('click', function() {
$(this).addClass('loading');
$.ajax({
url: ajax_object.ajaxurl,
type: 'POST',
dataType: 'html',
data: {
action: 'get_blog_items',
offset: $('body > div.page-wrap > section.blog-listing > ul').children().length
},
})
.done(function(data) {
console.log(data);
$('.blog-listing ul').append($.parseHTML(data))
})
.fail(function() {
console.log("error occured while loading more blog items");
})
.always(function() {
console.log("ajax call finished");
$(loadbuttonBlogItems).removeClass('loading');
});
})
});
I am trying to load more content with ajax. the script does not return anything when I am logged out.
this is my php code
<?php
function get_blog_items()
{
$offset = (int)intval($_POST['offset']);
$args = array(
'posts_per_page' => 4,
'post_type' => 'post',
'offset' => $offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'template-parts/list', 'blog' );
endwhile;
endif;
wp_reset_postdata();
}
function add_ajax_actions() {
add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' );
}
add_action( 'admin_init', 'add_ajax_actions' );
And my javascript
$(document).ready(function($) {
var loadbuttonBlogItems = $('#loadmore-blog-items');
loadbuttonBlogItems.on('click', function() {
$(this).addClass('loading');
$.ajax({
url: ajax_object.ajaxurl,
type: 'POST',
dataType: 'html',
data: {
action: 'get_blog_items',
offset: $('body > div.page-wrap > section.blog-listing > ul').children().length
},
})
.done(function(data) {
console.log(data);
$('.blog-listing ul').append($.parseHTML(data))
})
.fail(function() {
console.log("error occured while loading more blog items");
})
.always(function() {
console.log("ajax call finished");
$(loadbuttonBlogItems).removeClass('loading');
});
})
});
Share
Improve this question
edited Jan 17, 2017 at 0:03
Rick Groen
asked Jan 16, 2017 at 23:56
Rick GroenRick Groen
632 silver badges6 bronze badges
2 Answers
Reset to default 4function add_ajax_actions() {
add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' );
}
add_action( 'admin_init', 'add_ajax_actions' );
admin_init only runs when the admin area is initialised. Logged out people can't access wp-admin, so add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' ); never runs. Move the add_action call out of that function and remove the add_ajax_actions function and it should work.
Have you considered using the REST API instead?
Add this for login and logout then Ajax will work for both cases.
if ( is_user_logged_in() ) {
add_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
add_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
I am trying to load more content with ajax. the script does not return anything when I am logged out.
this is my php code
<?php
function get_blog_items()
{
$offset = (int)intval($_POST['offset']);
$args = array(
'posts_per_page' => 4,
'post_type' => 'post',
'offset' => $offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'template-parts/list', 'blog' );
endwhile;
endif;
wp_reset_postdata();
}
function add_ajax_actions() {
add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' );
}
add_action( 'admin_init', 'add_ajax_actions' );
And my javascript
$(document).ready(function($) {
var loadbuttonBlogItems = $('#loadmore-blog-items');
loadbuttonBlogItems.on('click', function() {
$(this).addClass('loading');
$.ajax({
url: ajax_object.ajaxurl,
type: 'POST',
dataType: 'html',
data: {
action: 'get_blog_items',
offset: $('body > div.page-wrap > section.blog-listing > ul').children().length
},
})
.done(function(data) {
console.log(data);
$('.blog-listing ul').append($.parseHTML(data))
})
.fail(function() {
console.log("error occured while loading more blog items");
})
.always(function() {
console.log("ajax call finished");
$(loadbuttonBlogItems).removeClass('loading');
});
})
});
I am trying to load more content with ajax. the script does not return anything when I am logged out.
this is my php code
<?php
function get_blog_items()
{
$offset = (int)intval($_POST['offset']);
$args = array(
'posts_per_page' => 4,
'post_type' => 'post',
'offset' => $offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'template-parts/list', 'blog' );
endwhile;
endif;
wp_reset_postdata();
}
function add_ajax_actions() {
add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' );
}
add_action( 'admin_init', 'add_ajax_actions' );
And my javascript
$(document).ready(function($) {
var loadbuttonBlogItems = $('#loadmore-blog-items');
loadbuttonBlogItems.on('click', function() {
$(this).addClass('loading');
$.ajax({
url: ajax_object.ajaxurl,
type: 'POST',
dataType: 'html',
data: {
action: 'get_blog_items',
offset: $('body > div.page-wrap > section.blog-listing > ul').children().length
},
})
.done(function(data) {
console.log(data);
$('.blog-listing ul').append($.parseHTML(data))
})
.fail(function() {
console.log("error occured while loading more blog items");
})
.always(function() {
console.log("ajax call finished");
$(loadbuttonBlogItems).removeClass('loading');
});
})
});
Share
Improve this question
edited Jan 17, 2017 at 0:03
Rick Groen
asked Jan 16, 2017 at 23:56
Rick GroenRick Groen
632 silver badges6 bronze badges
2 Answers
Reset to default 4function add_ajax_actions() {
add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' );
}
add_action( 'admin_init', 'add_ajax_actions' );
admin_init only runs when the admin area is initialised. Logged out people can't access wp-admin, so add_action( 'wp_ajax_nopriv_get_blog_items', 'get_blog_items' ); never runs. Move the add_action call out of that function and remove the add_ajax_actions function and it should work.
Have you considered using the REST API instead?
Add this for login and logout then Ajax will work for both cases.
if ( is_user_logged_in() ) {
add_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
add_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
本文标签: Ajax is not working for logged out users
版权声明:本文标题:Ajax is not working for logged out users 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749037879a2306671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论