functions - Using AJAX on frontend: Synchronous XMLHttpRequest deprecation except for admin role

admin2025-06-03  0

Everything is happening in single.php. I have to validate if user has subscribed to the webinar and show login button accordingly. It's on the frontend.

I'm doing this on document ready:

<?php if(is_user_logged_in()): ?>
    var data = {
        webinar: <?php echo get_the_ID(); ?>,
        user: <?php echo get_current_user_id(); ?>,
        action: 'enter_webinar',
    }

    $.ajax({
        url : '/wp-admin/admin-ajax.php',
        type : "get",
        data: data,
        success: function(r) {
            $('#placeholder').html(r);
        }
    });
<?php endif; ?>

Div with ID placeholder is empty by default. This is what I have in functions.php:

add_action( 'wp_ajax_enter_webinar', 'enter_webinar' );
add_action( 'wp_ajax_nopriv_enter_webinar', 'enter_webinar' );
function enter_webinar() {
    // conditional statment happens here
    // if condition is true, send this via AJAX:
    <a href="#" class="btn btn-primary btn-block">Enter webinar</a>
<?php
    wp_die();
}

When I login as an admin user, everything works perfectly: AJAX request is sent, conditional statements work correctly, the data is retrieved and a login button is show in placeholder div. For other roles, homepage of the website is displayed inside the placeholder div as an iframe and Chrome displays this error message in console log:

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

What am I doing wrong here?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748881755a314502.html

最新回复(0)