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

admin2025-06-04  2

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;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748985052a315391.html

最新回复(0)