Skip to content

Custom Capability

Loïc Antignac edited this page Sep 5, 2022 · 1 revision

(adding, removing custom capabilities)

Once the the plugin is created, the declaration(s) can be added:

<?php
/**
 * Plugin Name:       Example Custom Capability
 * Author:            My Name
 * Text Domain:       wax-custom-content
 * Domain Path:       /languages
 */
defined( 'ABSPATH' ) || exit;

// If you already have an autoload (for example if you are on Bedrock) you can remove these 3 lines, otherwise leave them.
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
	require __DIR__ . '/vendor/autoload.php';
}

use Webaxones\Core\Entities\Entities;

Webaxones\Core\Library::init( 'webaxones-content' );

/**
 * Add Custom capability "can_edit_blocks" to "administrator" role
 */
$declarations[] = [
	'entity'   => 'Webaxones\Core\Role\Capability',
	'labels'   => [],
	'settings' => [
		'slug'        => 'can_edit_blocks',
		'target_type' => 'role', /* user */
		'target'      => 'administrator', /* '3' */
		'action'      => 'add', /* remove */
	],
];

Entities::process( $declarations );

Settings

As for all entities, settings starts with the slug key which identifies the entity and must be unique.
Roles can be added, updated or removed.
There is therefore an action key in settings which can have the following values: add, remove, update.

target_type accept 2 values: role or user, to be able to add capability to a WP_Role or a WP_user.
in the User case, the user ID must be entered in target in string format:

'target_type' => 'user',
'target'      => '5',  
Clone this wiki locally