Skip to main content

Assign Custom Capability

PropertyValue
descriptionAdd custom post type capabilities to the Administrator role.
tagslib, php, wp, oop
rating

Overview

Assign a full custom post type capability set to the Administrator role.

Replace <cap_singular> and <cap_plural> with the capability bases for the CPT before using the snippet. For example, a book CPT that uses a plural capability base of books would use capabilities such as edit_book, read_book, edit_books, and publish_books.

Run this only after the role and capability names are final. The snippet grants capabilities on init; it does not remove capabilities if the placeholders are changed later.

Variants

<?php
/**
* Add custom post type capabilities to Administrator role
*
* Replace:
* <cap_singular> → singular capability base
* <cap_plural> → plural capability base
*
* Example:
* book / books
* product / products
* client_doc / client_docs
* dev_doc / dev_docs
* internal_post / internal_posts
*/
add_action( 'init', function (): void {

$role = get_role( 'administrator' );

if ( ! $role ) {
return;
}

/** @var string[] $caps */
$caps = [

// Meta caps (singular)
'edit_<cap_singular>',
'read_<cap_singular>',
'delete_<cap_singular>',

// Primitive caps (plural)
'edit_<cap_plural>',
'edit_others_<cap_plural>',
'publish_<cap_plural>',
'read_private_<cap_plural>',

'delete_<cap_plural>',
'delete_private_<cap_plural>',
'delete_published_<cap_plural>',
'delete_others_<cap_plural>',

'edit_private_<cap_plural>',
'edit_published_<cap_plural>',

'create_<cap_plural>',
];

foreach ( $caps as $cap ) {
$role->add_cap( $cap );
}

} );