简体   繁体   English

Wordpress,登录后将用户直接重定向到自定义帖子编辑屏幕

[英]Wordpress, redirect a user directly to a custom post edit screen after login

currently I got this: 目前我得到这个:

function redirect_companies()
{
    if ( current_user_can( 'ca_company' ) )
    {
        $screen = get_current_screen();

        if ( $screen->post_type != 'unternehmen' && $screen->id != 'profile' )
        {
            global $current_user;

            $current_users_posts = get_posts(
                                        array(
                                            'post_type' => 'unternehmen',
                                            'author' => $current_user->ID
                                        )
                                    );

            if ( count( $current_users_posts ) > 1 )
            {
                $redirect = admin_url( 'edit.php?post_type=unternehmen' );
            }
            else
            {
                $redirect = get_edit_post_link( $current_users_posts[0]->ID );
            }

            wp_redirect( $redirect, 301 );
        }
    }
}
add_action('current_screen', 'redirect_companies');

What it should do: A user with role 'ca_company' logs into wordpress backend and instantly gets redirected to either the overview screen of the custom post type posts of "unternehmen" or, if only one post by this user exists, to the edit screen of that one post. 应该执行的操作:角色为“ ca_company”的用户登录到wordpress后端,并立即重定向到“ unternehmen”的自定义帖子类型帖子的概述屏幕,或者如果该用户只有一个帖子,则重定向到编辑屏幕那个职位

Also, it should perform this redirect routine, if the user is trying to access any page that is not from post type "unternehmen" and is not the user-profile-edit screen. 另外,如果用户试图访问不是来自帖子类型“ unternehmen”的页面,也不是用户配置文件编辑屏幕,则它应该执行此重定向例程。

I successfully tested this when I already was logged in as auch user and then trying to access for example the dashboard. 当我已经以其他用户身份登录,然后尝试访问例如仪表板时,我已经成功测试了此功能。 This works. 这可行。

But if I completely log out of WP and then log in again, wordpress is performing this: 但是,如果我完全退出WP,然后再次登录,则wordpress将执行以下操作:

http://i.stack.imgur.com/M60aJ.png http://i.stack.imgur.com/M60aJ.png

... and then my browser is telling me, that there is a redirecting error. ...然后我的浏览器告诉我,发生重定向错误。 Infinite redirecting loop. 无限重定向循环。 But why? 但为什么? Why does it even go into that "if" where I check for post type "unternehmen". 为什么它甚至进入我检查帖子类型“ unternehmen”的“ if”。 Because if I log in, I am first getting to dashboard... 因为如果我登录,我首先要进入仪表板...

Hope someone can help :) 希望有人可以帮助:)

Use this action 'add_action('wp_login', 'do_anything');'. 使用此操作'add_action('wp_login','do_anything');'。 And in callback function you can give link to wp_redirect('link') where you want to redirect your screen. 并且在回调函数中,您可以提供指向wp_redirect('link')的链接,以在其中重定向屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM