Skip to main content

Restrict Taxonomy Access

PropertyValue
descriptionRestrict access to protected taxonomies.
tagslib, php, wp
rating

Overview

Restrict public access to selected taxonomy archives. The snippet checks whether the current request is for one of the configured taxonomy slugs, then redirects visitors who do not have the required capability to the WordPress login URL.

Replace the placeholder taxonomy slugs in $protected_taxonomies with the taxonomies for the site. By default, the access check uses current_user_can( 'edit_posts' ).

Variants

<?php
/**
* Restrict access to protected taxonomies
*
* Replace:
* <taxonomy_slug> -> Taxonomy slug
*
* Examples:
* internal_category
* client_doc_type
*/
add_action( 'template_redirect', function(): void {

/** @var string[] $protected_taxonomies */
$protected_taxonomies = [
'<taxonomy_slug>', // Example: internal_category
'<taxonomy_slug>', // Example: client_doc_type
];

if ( is_tax( $protected_taxonomies ) ) {

if ( ! current_user_can( 'edit_posts' ) ) {
wp_redirect( wp_login_url() );
exit;
}
}

});