Custom Fields
Re-Envisioned
Support MB User Meta Private Hidden Admin Note for User meta - only Admin, Manager can see per User

  • Creator
    Topic
  • #30604
    Resolved justdoit123
    Participant

    I want to add a custom field "Note" per user (i use MB custom table)

    User cannot see this custom field "Note", only Admin, Manager can see and edit note per User.

    How can i do that in MB ?

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #30617
    Long Nguyen
    Moderator

    Hi,

    You can use the extension MB Include Exclude to load a meta box for a/some predefined roles. Please get more details here https://docs.metabox.io/extensions/meta-box-include-exclude/

    #30621
    justdoit123
    Participant

    my php is limited. can you give me some more example ??

    in this sample code below, i don't know how to assign this to specific metabox ID ??

    thanks

    add_filter( 'rwmb_meta_boxes', 'prefix_include_exclude_demo' );
    function prefix_include_exclude_demo( $meta_boxes ) {
        $meta_boxes[] = array(
            'title'   => 'Include Meta Box',
    
            // Register this meta box for posts matched below conditions
            'include' => array(
                // With all conditions below, use this logical operator to combine them. Default is 'OR'. Case insensitive. Optional.
                'relation'        => 'OR',
    
                // List of post IDs. Can be array or comma separated. Optional.
                'ID'              => array( 1, 2 ),
    
                // List of post parent IDs. Can be array or comma separated. Optional.
                'parent'          => array( 3, 4 ),
    
                // List of post slugs. Can be array or comma separated. Optional.
                'slug'            => array( 'contact', 'about' ),
    
                // List of page templates. Can be array or comma separated. Optional.
                'template'        => array( 'full-width.php', 'sidebar-page.php' ),
    
                // List of categories IDs or names or slugs. Can be array or comma separated. Optional.
                'category'        => array( 1, 'Blog', 'another' ),
    
                // List of tag IDs or names or slugs. Can be array or comma separated. Optional.
                'tag'             => array( 1, 'fun' ),
    
                // Custom taxonomy. Optional.
                // Format: 'taxonomy' => list of term IDs or names or slugs (can be array or comma separated)
                'location'        => array( 12, 'USA', 'europe' ),
                'os'              => array( 'Windows', 'mac-os' ),
    
                // List of parent categories IDs or names or slugs. Can be array or comma separated. Optional.
                'parent_category' => 'Parent',
    
                // List of parent tag IDs or names or slugs. Can be array or comma separated. Optional.
                'parent_tag'      => 'Parent',
    
                // Parent custom taxonomy. Optional.
                // Format: 'parent_taxonomy' => list of term IDs or names or slugs (can be array or comma separated)
                'parent_location' => array( 12, 'USA', 'europe' ),
    
                // Check if current post/page is a child page
                'is_child'        => true,
    
                // List of user roles. Can be array or comma separated. Optional.
                'user_role'       => 'administrator',
    
                // List of user IDs. Can be array or comma separated. Optional.
                'user_id'         => array( 1, 2 ),
    
                // Custom condition. Optional.
                // Format: 'custom' => 'callback_function'
                // The function will take 1 parameter which is the meta box itself
                'custom'          => 'manual_include',
            ),
    
            'fields' => array(
                array(
                    'name' => 'Name',
                    'id'   => 'name',
                    'type' => 'text',
                ),
            ),
        );
    
        // 2nd meta box
        $meta_boxes[] = array(
            'title'   => 'Exclude Meta Box',
    
            // Don't register this meta box for posts matched below conditions
            'exclude' => array(
                'relation'  => 'OR',
                'ID'        => array( 1, 2 ),
                'parent'    => array( 3, 4 ),
                'slug'      => array( 'contact', 'about' ),
                'template'  => array( 'full-width.php', 'left-sidebar.php' ),
                'category'  => array( 1, 'News' ),
                'tag'       => array( 1, 'fun' ),
                'user_role' => array( 'administrator', 'editor' ),
                'user_id'   => array( 1, 2 ),
                'location'  => array( 12, 'USA', 'europe' ),
                'os'        => array( 'Windows', 'mac-os' ),
                'is_child'  => true,
                'custom'    => 'manual_exclude',
            ),
    
            'fields' => array(
                array(
                    'name' => 'Job',
                    'id'   => 'job',
                    'type' => 'text',
                ),
            ),
        );
    
        return $meta_boxes;
    }
    
    function prefix_include_exclude_manual_include( $meta_box ) {
        if ( $meta_box['title'] == 'Include Meta Box' )
            return true;
        return false;
    }
    #30637
    Long Nguyen
    Moderator

    Hi,

    Here is the sample code:

    function your_prefix_function_name( $meta_boxes ) {
        $meta_boxes[] = [
            'title'   => __( 'User Meta', 'your-text-domain' ),
            'id'      => 'user-meta',
            'type'    => 'user',
            'include' => [
                'relation'  => 'OR',
                'user_role' => ['editor'],
            ],
            'fields'  => [
                ...
            ],
        ];
    
        return $meta_boxes;
    }

    FYI, you can use the Builder to set the role as well, option Advanced Location Rules. Please read more on the documentation https://docs.metabox.io/extensions/meta-box-builder/#field-group-settings

    #30658
    justdoit123
    Participant

    do i need to add_filter or add_action with this function ?? thanks

    function your_prefix_function_name( $meta_boxes ) {
        $meta_boxes[] = [
            'title'   => __( 'User Meta', 'your-text-domain' ),
            'id'      => 'user-meta',
            'type'    => 'user',
            'include' => [
                'relation'  => 'OR',
                'user_role' => ['editor'],
            ],
            'fields'  => [
                ...
            ],
        ];
    
        return $meta_boxes;
    }
    #30661
    Long Nguyen
    Moderator

    Hi,

    It looks like the same question here https://metabox.io/support/topic/can-i-create-custom-field-for-user-meta-in-wp-admin-and-add-php-code-generated
    You can find the answer there.

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