PHP code example of wpboilerplate / wpb-access-control
1. Go to this page and download the library: Download wpboilerplate/wpb-access-control library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wpboilerplate / wpb-access-control example snippets
use WPBoilerplate\AccessControl\AccessControlManager;
// File scope — available to all hooks below via `use ( $manager )`.
$manager = new AccessControlManager( 'my_plugin_access_control_providers' );
> $manager = null;
> add_action( 'plugins_loaded', function () use ( &$manager ) {
> $manager = new AccessControlManager( 'my_plugin_access_control_providers' );
> } );
> // All subsequent hooks must also use `&$manager`.
>
add_action( 'rest_api_init', function () use ( $manager ) {
$manager->register_rest_api();
} );
/**
* Plugin Name: My Plugin
*/
use WPBoilerplate\AccessControl\AccessControlManager;
// 1. Require Composer autoloader.
_plugin_access_control_providers' );
// 3. Expose the REST API.
add_action( 'rest_api_init', function () use ( $manager ) {
$manager->register_rest_api();
} );
// 4. Register an admin settings page and capture its hook suffix.
$settings_hook = null;
add_action( 'admin_menu', function () use ( &$settings_hook ) {
// add_submenu_page() returns the hook suffix needed in admin_enqueue_scripts.
$settings_hook = add_submenu_page(
'options-general.php', // parent menu slug
'My Plugin Settings', // page title
'My Plugin', // menu title
'manage_options', // capability
'my-plugin-settings', // menu slug
function () {
echo '<div class="wrap">';
echo '<h1>My Plugin Settings</h1>';
// 5. Mount point — the React component attaches here automatically.
echo '<div id="wpb-access-control"></div>';
echo '</div>';
}
);
} );
// 6. Enqueue the built React UI assets only on the settings page.
add_action( 'admin_enqueue_scripts', function ( string $hook ) use ( &$settings_hook ) {
if ( $hook !== $settings_hook ) {
return;
}
$asset_file =
$allowed = $manager->user_has_access(
get_current_user_id(), // int — 0 = unauthenticated
'my-namespace', // string — your plugin's namespace
'my-resource' // string — the specific resource key
);
if ( ! $allowed ) {
wp_die( 'Access denied.', 403 );
}
// Capture the hook suffix when registering the page.
$page_hook = add_submenu_page( /* … */ );
add_action( 'admin_enqueue_scripts', function ( string $hook ) use ( $page_hook ) {
// Only load on the page where you need it.
if ( $hook !== $page_hook ) {
return;
}
$asset_file = wp-api-fetch', 'wp-element']
$asset_file['version'],
true
);
wp_enqueue_style(
'wpb-ac-ui',
plugin_dir_url( __FILE__ )
. 'vendor/wpboilerplate/wpb-access-control/assets/build/index.css',
[],
$asset_file['version']
);
// Pass configuration to the component via window.wpbAcConfig.
wp_localize_script( 'wpb-ac-ui', 'wpbAcConfig', [
'namespace' => 'my-namespace',
'resourceKey' => 'my-resource',
'restApiRoot' => get_rest_url(),
'nonce' => wp_create_nonce( 'wp_rest' ),
// Optional overrides:
'title' => 'Access Control',
'description' => 'Control which users may access this feature.',
'saveLabel' => 'Save Access Control',
] );
} );
add_action( 'my_plugin_settings_page', function () {
echo '<div id="wpb-access-control"></div>';
} );
use WPBoilerplate\AccessControl\Database\Rule\RuleQuery;
$query = new RuleQuery();
// Read the current rule.
$rule = $query->get_rule( 'my-namespace', 'my-resource' );
// → ['key' => 'wp_role', 'value' => ['editor', 'author']]
// → ['key' => '', 'value' => []] when no rule is set
// Save a rule (inputs are sanitized internally).
$query->set_rule( 'my-namespace', 'my-resource', 'wp_role', ['editor', 'author'] );
// Allow everyone.
$query->set_rule( 'my-namespace', 'my-resource', 'everyone', [] );
// Clear a rule (reverts to "no restriction configured").
$query->clear_rule( 'my-namespace', 'my-resource' );
// Plugin uninstall — delete all rows for your namespace.
$query->purge_namespace( 'my-namespace' );