最新消息: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)

permalinks - Programmatically Restrict WordPress from using certain URLs or sub-directories

matteradmin10PV0评论

Is there a way via functions.php or a plugin or .htaccess to programmatically prevent WordPress from using certain URLs or sub-directories?

For example:

website/games/*  
website/eat

Is there a way via functions.php or a plugin or .htaccess to programmatically prevent WordPress from using certain URLs or sub-directories?

For example:

website/games/*  
website/eat
Share Improve this question edited Jan 22, 2019 at 20:57 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 22, 2019 at 17:46 AlexnlAlexnl 3071 gold badge6 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The easiest way in WP would be to use wp_unique_post_slug hook.

It takes 6 params:

apply_filters( 'wp_unique_post_slug', string $slug, int $post_ID, string $post_status, string $post_type, int $post_parent, string $original_slug )

So you can target given URL and modify the slug would conflict.

Let's say you want to prevent WP from using website/eat address. All you need to do is to disable eat as slug value.

add_filter( 'wp_unique_post_slug', 'prefix_wp_unique_post_slug', 2, 6 );
function prefix_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
    if ( 'eat' == $slug ) {
        // change it to something else
        return $slug .'-2';
    }
    return $slug;
}
Post a comment

comment list (0)

  1. No comments so far