简体   繁体   中英

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.

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.

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:

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". Because if I log in, I am first getting to dashboard...

Hope someone can help :)

Use this action '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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