Help solve the problem. You need to create a separate entity (new type of post) when you activate the plugin. Let the new entity be called new_product.
When a plug-in is activated, a new capability should be created, let new_cap, and add this capability to the role of Administrator. A new section with a new type (new_product) can only be seen by the administrator with a new capability.
How to create a new capability? Is add_cap () suitable for this?
In theory, add_cap () adds only existing capabilities, and how do you need to create and add a new capability?

    1 answer 1

    That's right, add_cap () will not work. In order to create new features and user roles, the easiest way is to use the User Roles and Capabilities plugin.

    You can add such code in functions.php

    function custom_add_cap() { $custom_cap = 'kagg_cap'; $min_cap = 'activate_plugins'; // Смотрите роли в codex $grant = true; foreach ( $GLOBALS['wp_roles']->role_objects as $role_obj ) { if ( (! $role_obj->has_cap( $custom_cap )) && ($role_obj->has_cap( $min_cap )) ) { $role_obj->add_cap( $custom_cap, $grant ); } } } add_action( 'init', 'custom_add_cap' ); 

    At the initialization stage, a function is called that adds a new feature. For example, the new feature is called kagg_cap. The function checks all existing roles, compares it with a certain minimum capability (in the example, activate_plugins), and adds a new feature to all roles where there is a minimal one.

    A list of roles and capabilities , as well as a table of compliance, can be found in codex.

    Check that the roles and capabilities are set, you can code in functions.php

     function action_footer(){ var_dump($GLOBALS['wp_roles']->role_objects); var_dump($GLOBALS['wp_roles']->role_objects['administrator']); var_dump($GLOBALS['wp_roles']->role_objects['administrator']->has_cap('activate_plugins')); var_dump(current_user_can( 'administrator')); var_dump(current_user_can( 'activate_plugins')); var_dump(current_user_can( 'kagg_cap')); var_dump(current_user_can( 'kagg1_cap')); } add_action( 'wp_footer', 'action_footer' ); 

    Since we did not create kagg1_cap, the last line will print false.

    • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash