Ajax is not working for logged out users

admin2025-06-04  1

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
Add a comment  | 

2 Answers 2

Reset to default 4
function 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'] );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749037903a315828.html

最新回复(0)