【问题标题】:AJAX overlay if user session expires Wordpress frontend如果用户会话过期,则 AJAX 覆盖 Wordpress 前端
【发布时间】:2018-02-09 12:23:08
【问题描述】:

如果wp-admin 中的用户会话到期,用户已注销并且页面仍处于打开状态,则 WordPress 会覆盖模式登录。

如何注册用户会话到期/从另一个位置注销并从前端启动覆盖?

目前,我们通过 Javascript 使用按钮 on('click',...) 调出登录表单,登录和注销操作在 functions.php

中处理
$('a#show_login').on('click', function(e){
    $('body').prepend('<div class="login_overlay"></div>');
    $('form#login').fadeIn(500);
    $('div.login_overlay, form#login a.close').on('click', function(){
        $('div.login_overlay').remove();
        $('form#login').hide();
    });
    e.preventDefault();
});

有没有办法实时识别用户会话到期,然后启动覆盖?我已经寻找其他参考和文档,但找不到解决方案来实现后端发生的事情的WordPress。理想情况下,能够在前端使用相同的钩子/操作(如果它们存在?)将是完美的解决方案。

如果不是,可能是 AJAX 和复制后端为超时会话所做的操作的组合。

这里是启动当前覆盖表单的函数

function ajax_login_init(){

wp_register_script('ajax-login-script', get_template_directory_uri() . '/assets/js/ajax-login-script.js', array('jquery') ); 
wp_enqueue_script('ajax-login-script');

wp_localize_script( 'ajax-login-script', 'ajax_login_object', array( 
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'redirecturl' => home_url(),
    'loadingmessage' => __('Sending user info, please wait...')
));

// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}

// Execute the action only if the user isn't logged in
if (!is_user_logged_in()) {
    add_action('init', 'ajax_login_init');
}

以及后端过期会话的示例

请注意:我不想使用插件来实现这一点。

【问题讨论】:

    标签: javascript php ajax wordpress


    【解决方案1】:

    我在这里找到了解决方案https://wordpress.stackexchange.com/questions/223721/interim-login-form-on-frontend

    function login_session_expired() {
    // we only care to add scripts and styles if the user is logged in.
    if ( is_user_logged_in() ) {
    
        // add javascript file
        wp_register_script( 'wp_auth_check', '/wp-includes/js/wp-auth-check.js' , array('heartbeat'), false, 1);
        wp_localize_script( 'wp_auth_check', 'authcheckL10n', array(
            'beforeunload' => __('Your session has expired. You can log in again from this page or go to the login page.'),
            'interval' => apply_filters( 'wp_auth_check_interval', 1 * MINUTE_IN_SECONDS ), // default interval is 3 minutes
        ) );
        wp_enqueue_script ('wp_auth_check');
    
        // add css file
        wp_enqueue_style( 'wp_auth_check','/wp-includes/css/wp-auth-check.css', array( 'dashicons' ), NULL, 'all' );
    
        // add the login html to the page
        add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 );
    }
    }
    add_action( 'wp_enqueue_scripts', 'login_session_expired' );
    
    // make sure the stylesheet appears on the lightboxed login iframe
    function login_session_expired_styles() {
        wp_enqueue_style( 'wp_auth_check','/wp-includes/css/wp-auth-check.css', array( 'dashicons' ), NULL, 'all' );
    }
    add_action( 'login_enqueue_scripts', 'login_session_expired_styles' );
    

    【讨论】:

      猜你喜欢
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多