admin管理员组

文章数量:1435859

I am trying to redirect any visitors to the dashboard unless they land on a specific custom post type (fruits).

So the following URLs would all redirect to the dashboard...




But the following would work as usual..




I have this so far...

function redirect_to_backend() {
    if (!is_singular( 'fruits' )) {
         wp_redirect( admin_url(), 301 );
        exit();
    }
}
add_action( 'init', 'redirect_to_backend' );

But this is not working, anyone point me in the right direction?

I am trying to redirect any visitors to the dashboard unless they land on a specific custom post type (fruits).

So the following URLs would all redirect to the dashboard...

https://www.example/contact
https://www.example/sales
https://www.example/blog

But the following would work as usual..

https://www.example/fruits/apple
https://www.example/fruits/grape
https://www.example/fruits/banana

I have this so far...

function redirect_to_backend() {
    if (!is_singular( 'fruits' )) {
         wp_redirect( admin_url(), 301 );
        exit();
    }
}
add_action( 'init', 'redirect_to_backend' );

But this is not working, anyone point me in the right direction?

Share Improve this question edited Mar 19, 2019 at 16:25 fightstarr20 asked Mar 19, 2019 at 16:20 fightstarr20fightstarr20 1,1358 gold badges26 silver badges47 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

The earliest hook that is_singular() will work on is wp. replace init with wp in your add_action call

本文标签: Redirect all users to dashboard unless custom post type