Wordpress admin-ajax results in Error 302 redirect

前端 未结 4 1796
孤独总比滥情好
孤独总比滥情好 2021-02-19 20:11

I have customized a plugin to make some ajax calls to admin-ajax.php and it works great. I copied the code over to another site and it no longer works for users who are not logg

4条回答
  •  无人及你
    2021-02-19 21:07

    I had the exact same problem and this worked for me:

    function mt_redirect_admin() {
    
    if ( ! current_user_can( 'manage_options' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
    wp_redirect( site_url() ); 
    exit;
    }
    }
    
    add_action( 'admin_init', 'mt_redirect_admin', 1 );
    

提交回复
热议问题