$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Ajax is not working for logged out users|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Ajax is not working for logged out users

matteradmin9PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far