【问题标题】:Redirect by user role from a specific admin url to another url按用户角色从特定管理员 url 重定向到另一个 url
【发布时间】:2021-05-26 03:20:44
【问题描述】:

如果我使用 is_front_page 作为当前页面/url,此代码可以正常工作,因为它会获取我的主页并将用户按角色重定向到特定路径,但如果我尝试获取它则不起作用wp 管理仪表板的特定当前 url,我尝试使用 get_home_url(path),但它不起作用:


add_action( 'template_redirect', 'role_based_redirect' );
function role_based_redirect() {
    if( get_home_url('/wp-admin/current-url') !== false ) { // this doesn't work
        $user = wp_get_current_user();
        if ( in_array( 'author', (array) $user->roles ) ) {
            wp_redirect( home_url( '/wp-admin/other-url' ) );
            exit;
        }
    }
}

提前谢谢你。

【问题讨论】:

    标签: php wordpress function redirect roles


    【解决方案1】:

    template_redirect 钩子用于您网站的前端。对于管理方面,您可以使用不同的钩子。比如init、admin_init等

    f.e.

    add_action( 'admin_init', 'role_based_redirect' );
    function role_based_redirect() {
        if( strpos($_SERVER["REQUEST_URI"],'wp-admin/current-url') !== false ) { 
            $user = wp_get_current_user();
            if ( in_array( 'author', (array) $user->roles ) ) {
                wp_redirect( home_url( '/wp-admin/other-url' ) );
                exit;
            }
        }
    }
    

    【讨论】:

    • 它有效,谢谢我在当前 url 之前使用了带有斜杠的 '/wp-admin/current-url'
    猜你喜欢
    • 2011-04-14
    • 1970-01-01
    • 2018-05-29
    • 2015-10-28
    • 2019-04-09
    • 1970-01-01
    • 2022-10-09
    • 1970-01-01
    • 2022-11-14
    相关资源
    最近更新 更多