需要有关AJAX登录的帮助才能调用函数中的php。php根据用户上限(角色)处理重定向

时间:2019-02-08 作者:Gareth Thomas

我到处都找不到我问题的答案。

我正在尝试制作一个登录重定向脚本,用户登录后(取决于他们的角色)将转到某个页面。我目前在函数中有一个工作片段。php-从wp登录调用它可以很好地工作。php。然而,我已经使用AJAX创建了一个模式弹出窗口,因为我无法让AJAX在我的函数中调用该函数。php

functions.php snippet

function admin_login_redirect( $url, $request, $user ){
    //is there a user
    if( $user && is_object( $user ) && is_a( $user, \'WP_User\' ) ) {
        //is user admin
        if( $user->has_cap( \'administrator\' ) ) {
            //go do admin stuff
            $url = admin_url();
            //but wait there\'s more
        }
    }
    return $url;
}
add_filter(\'login_redirect\', \'admin_login_redirect\', 10, 3 );

function affiliate_login_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, \'WP_User\' ) ) {
        if( $user->has_cap(\'yith_affiliate\') && strpos($_REQUEST[\'redirect_to\'], \'gf_entries\') == false )  {
            //please god work
            $url = home_url() . \'/affiliate-dashboard/\';
            //but waittt there\'s more
        } else {
            //damnit all
            if( $user->has_cap(\'yith_affiliate\') && isset($_REQUEST[\'redirect_to\']) && strpos($_REQUEST[\'redirect_to\'], \'gf_entries\') !== false) {

            $url = $_REQUEST[\'redirect_to\'];

            }
        }
    }
    return $url;
}
add_filter(\'login_redirect\', \'affiliate_login_redirect\', 10, 3 );
现在我的AJAX确实在一定程度上起到了作用,为了测试,我将其发送到了谷歌。com,这将当前所有用户发送到谷歌。

AJAX snippet

if ( LRM_Pro.redirect_urls.after_login && "login" == action ) {
    window.location.href = "https://www.google.com";
所以我想得到的是AJAX窗口。地方href调用my login redirect函数,根据用户cap(角色)处理所有重定向。

任何帮助都将不胜感激!

1 个回复
最合适的回答,由SO网友:Gareth Thomas 整理而成

因此,我通过引导现有窗口实现了我想要的。地方href指向一个新创建的php文件,我将其放在名为的根目录中。登录名。php。我将分享下面的代码,因为它在使用jquery方法登录时很好地处理了用户角色

.login.php

<?php
require_once(\'wp-load.php\');

function user_role_check_modal( $role, $user_id = null ) {

  $user = wp_get_current_user();

    if ( empty( $user ) )
    return false;

  return in_array( $role, (array) $user->roles );
}

if ( user_role_check_modal( \'administrator\' )):
   header("Location: /wp-admin/");
   exit();

endif;

if ( user_role_check_modal( \'customer\' )):
   header("Location: /my-account/");
   exit();

else:

   header("Location: /my-account/");
   exit();

endif;
希望这对将来可能需要解决的人有所帮助