P
P
Pavel Novikov2018-07-24 11:11:23
WordPress
Pavel Novikov, 2018-07-24 11:11:23

Why doesn't AJAX work intermittently in Wordpress?

Good afternoon.
I created a custom post type in WordPress, for this custom post type I wrote a paginator in Ajax. The paginator code in the functions.php file looks like this:

add_action('wp_ajax_paginate_action', 'paginator_callback');

function paginator_callback()
{

    $args = [
        'posts_per_page' => $_POST['limit'],
        'offset' => $_POST['offset'],
        'category' => '',
        'category_name' => '',
        'orderby' => 'date',
        'order' => 'DESC',
        'include' => '',
        'exclude' => '',
        'meta_key' => '',
        'meta_value' => '',
        'post_type' => 'blog_post',
        'post_mime_type' => '',
        'post_parent' => '',
        'author' => '',
        'author_name' => '',
        'post_status' => 'publish',
        'suppress_filters' => true
    ];

    $posts = get_posts($args);
    print_r(renderView($posts)); //функция кастомная, отвечающая за вывод загруженных постов в HTML
    wp_die();
}

function renderView($posts)
{
    $view = '';
    foreach ($posts as $post) {
        $postMeta = get_post_meta($post->ID);
        $timeToRead = $postMeta['time_to_read'][0];
        $image_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail');
        $single_post = '
            <div class=" vc_col-lg-4 vc_col-md-4 vc_col-sm-6 vc_col-xs-12 blog-post-masonry masonry-block post-offset norebro-card-wrapper grid-item" data-lazy-item="true">
                <div class="blog-grid grid-1 boxed">
                    <header>
                        <a href="https://studiosoftware.com/blog/billing-model-choose-company-discuss-fp-tm-fbsc/">
                            <img src="' . $image_src[0] . '" alt="' . null . '">
                        </a>
                        ' . getPostTags($post) . '
                    </header>
    
                    <div class="content">
                        <h3>
                            <a href="https://studiosoftware.com/blog/' . $post->post_name . '/">
                                ' . $post->post_title . '			
                             </a>
                        </h3>
                        <p>
                            ' . getPostPreview($post) . '…		
                        </p>
                    </div>
                    
                    <footer>
                        <span class="data">' . getPostDateTime($post) . '</span>
                        <strong>—</strong>
                        <span class="data">' . $timeToRead . ' min. to read</span>
                    </footer>
                </div>
                <div class="clear"></div>
            </div>
        ';

        $view .= $single_post;

    }

    return ' <div class="norebro-masonry" data-lazy-container="true" style="position: relative;">' . $view . '</div>';
}


I wrote it according to the documentation from the VP website. At the same time, the paginator does not work periodically, and the following error appears in the console
POST https://my-site.com/wp-admin/admin-ajax.php 400()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Novikov, 2018-07-24
@paulfcdd

The solution to the problem turned out to be ridiculously banal. Since Ajax requests in the VI go to admin-ajax.php, respectively, the WordPress firewall (or whatever is responsible for security) allows only authorized users to send requests to the server. In order for unauthorized users to send requests to the server, one more hook should be connected:
and then requests are sent without problems

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question