【问题标题】:wordpress url redirect hook | PHP |wordpress url 重定向挂钩 | PHP |
【发布时间】:2021-01-05 00:45:46
【问题描述】:

我正在开发我的 wordpress 网站,但我很少遇到一种情况......我想在成功登录后将用户重定向到特定页面,但我不知道如何使用 wordpress 重定向挂钩 下面是我放在函数文件中的 wordpress 钩子...我在网上找到的代码:-

/**
* WordPress function for redirecting users on login based on user role*/
function wpdocs_my_login_redirect( $url, $request, $user ) {

$urllinkin = 
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
    if ( $user->has_cap( 'administrator' ) ) {
        $url = admin_url();
    } elseif() {
        $url = home_url( '/members-only/' );
    }
}
return $url;
}

add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );

目前我在锚标记中使用此代码重定向到登录页面,登录到特定页面后,登录正常,但重定向不起作用

<?php echo wp_login_url(get_permalink()); ?>"> this code give the url :- http://192.168.1.50/jobifylocal/my-profile/?redirect_to=http://192.168.1.50/jobifylocal/job/clinical-psychologist/

嘿,我根据需要编辑了代码.. 你评论了,但它仍然没有重定向

function wpdocs_my_login_redirect( $url, $request, $user ) { 
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if ( $user->has_cap( 'administrator' ) ) {
    $url = admin_url();

} elseif ( $user->has_cap( 'candidate' ) ) {
             $variable_two = $_GET['redirect_to'];
        if(!empty($variable_two)){
            $url =  $variable_two;
        }

   // $url = home_url( '/members-only/' );
}
}
return wp_redirect($url);
}
 add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    $urllinkin = 从您的代码中删除它。

    这里还有登录重定向的代码(Check official docs),

    function my_login_redirect( $redirect_to, $request, $user ) {
        //is there a user to check?
        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
            //check for admins
            if ( in_array( 'administrator', $user->roles ) ) {
                // redirect them to the default place
                return $redirect_to;
            } else {
                return home_url();
            }
        } else {
            return $redirect_to;
        }
    }
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
    

    对于重定向,你也可以使用wp_redirect默认函数。

    这是您的更新代码,

    function wpdocs_my_login_redirect( $url, $request, $user ) { 
      if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        if ( $user->has_cap( 'administrator' ) ) {
            $url = admin_url();
        } else {
            $url = home_url( '/members-only/' );
        }
      }
      return $url;
    }
    add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );
    

    【讨论】:

    • 但是如何重定向到特定页面
    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 2021-11-08
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多