I'm trying to add a user_role inside the ATUM stock management plugin. Currently it's only visible to admins, while I also want to add shop_managers. But I just can't succeed in adding it successfully -except if I add it directly in the plugin.
This is where the filter is placed inside the ATUM plugin.
namespace Atum\Components;
class AtumCapabilities {
private function __construct(){
$admin_roles = (array) apply_filters( 'atum/capabilities/admin_roles', [ get_role( 'administrator' ) ] );
foreach ( $admin_roles as $admin_role ) {
if ( $admin_role instanceof \WP_Role ) {
foreach ( $this->capabilities as $cap ) {
$admin_role->add_cap( $cap );
}
}
}
}
}
And this is the code I have been using. I also tried variations of this, changing the priority or including this in the "init" hook.
add_filter('atum/capabilities/admin_roles', function($roles) {
$roles[] = get_role('shop_manager');
return $roles;
});
Does anyone know how I can add get_role('shop_manager') to the array inside the atum/capabilities/admin_roles filter?
Thanks
I'm trying to add a user_role inside the ATUM stock management plugin. Currently it's only visible to admins, while I also want to add shop_managers. But I just can't succeed in adding it successfully -except if I add it directly in the plugin.
This is where the filter is placed inside the ATUM plugin.
namespace Atum\Components;
class AtumCapabilities {
private function __construct(){
$admin_roles = (array) apply_filters( 'atum/capabilities/admin_roles', [ get_role( 'administrator' ) ] );
foreach ( $admin_roles as $admin_role ) {
if ( $admin_role instanceof \WP_Role ) {
foreach ( $this->capabilities as $cap ) {
$admin_role->add_cap( $cap );
}
}
}
}
}
And this is the code I have been using. I also tried variations of this, changing the priority or including this in the "init" hook.
add_filter('atum/capabilities/admin_roles', function($roles) {
$roles[] = get_role('shop_manager');
return $roles;
});
Does anyone know how I can add get_role('shop_manager') to the array inside the atum/capabilities/admin_roles filter?
Thanks
Share Improve this question asked Jul 7, 2020 at 15:08 photogenicphotogenic 211 silver badge7 bronze badges 4 |1 Answer
Reset to default 0I added the capabilities now through this code. I'm not happy with my solution and it would be great if someone could find a proper way to add to the filter. I would be honestly interested to know how to make it properly work.
add_action( 'admin_init', 'jp_add_atum_caps_to_shop_manager');
function jp_add_atum_caps_to_shop_manager() {
if(!user_can( 217 , 'atum_read_inbound_stock')) {
$capabilities = array(
// Purchase price caps.
'edit_purchase_price',
'view_purchase_price',
// Inbound Stock caps.
'read_inbound_stock',
// Out Stock Threshold caps.
'edit_out_stock_threshold',
// Settings caps.
'manage_settings',
'edit_visual_settings',
// ATUM menus caps.
'view_admin_menu',
'view_admin_bar_menu',
// Other caps.
'export_data',
'view_statistics',
);
// Add the ATUM prefix to all the capabilities.
$capabilities = preg_filter( '/^/', 'atum_' , $capabilities );
$admin_role = get_role( 'shop_manager' );
foreach ( $capabilities as $cap ) {
$admin_role->add_cap( $cap );
}
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742279698a4414257.html
add_cap()
should only be run once. Perhaps try deactivating and reactivating ATUM while your filter code is active. – Jacob Peattie Commented Jul 7, 2020 at 15:13