Feature Request: Add 'capabilities' condition

Support MB Include Exclude Feature Request: Add 'capabilities' conditionResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #19009
    Drake CooperDrake Cooper
    Participant

    Hi,
    Could you please add a 'capabilities' condition? user_role is too limiting and I'd be much nicer if I didn't have to use the 'custom' condition and write a function for this.
    Thanks!

    #19017
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Thank you for the idea but I think no need to add the parameter capabilities because each role has its own capabilities and you can add a capability to a role easily with the function add_cap()

    For more information, please follow the documentation
    https://docs.metabox.io/extensions/mb-user-profile/#upload-files--images
    https://wordpress.org/support/article/roles-and-capabilities/

    #19036
    Drake CooperDrake Cooper
    Participant

    I'm very familiar with roles and capabilities. I use custom roles and caps on most sites as I build large ones with many editing users. Because I have many roles, it's easier to do a capability check than a role check. For example, I have a settings page which only certain roles can edit: administrator, webmaster, seo admin, analytics admin. Instead of adding all those roles, just doing a manage_options cap check works better.

    I looked at the source for this extension and was easily able to add support for this:

    In file class-mb-include-exclude.php
    Line 75, add , 'capability' to the array.
    Line 253, insert

    
      /**
       * Check by current user capability.
       *
       * @param array|string $capabilities List of user capabilities. Array or CSV.
       *
       * @return bool
       */
      protected static function check_capability( $capabilities ) {
        $user = wp_get_current_user();
        $capabilities = array_map( 'strtolower', self::csv_to_array( $capabilities ) );
    
        foreach ( $capabilities as $capability ) {
          if ( $user->has_cap( $capability ) ) {
            return true;
          }
        }
    
        return false;
      }

    Then in your meta box config, add:

          'include' => array(
            'capability' => 'manage_options',
          ),
    
    #19043
    Anh TranAnh Tran
    Keymaster

    That's a good idea, Drake. I'll add to the plugin.

    Updated: Added to version 1.0.11.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.