最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Redirect all users to dashboard unless custom post type

matteradmin9PV0评论

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

Post a comment

comment list (0)

  1. No comments so far