Custom Fields
Re-Envisioned
Support MB User Meta "mb_user_meta & mb_rest_api" has problem

This topic contains 5 replies, has 2 voices, and was last updated by  Jiro 1 month ago.

  • Creator
    Topic
  • #13779
    Resolved
    Jiro
    Participant

    I want to use mb_user_meta & mb_rest_api.
    I purchased mb_user_meta. and I followed example code on https://docs.metabox.io/extensions/mb-user-meta/
    and I can see the proper page in admin and I can update the mb value on that page.

    but I can not update my user MB data with rest-api.

    Below is my Server code.

    add_action( ‘rwmb_meta_boxes’, ‘prefix_register_meta_boxes’ );
    function prefix_register_meta_boxes( $meta_boxes ) {
    $meta_boxes[] = array(
    ‘id’ => ‘personal’,
    ‘title’ => ‘Personal Information’,
    ‘type’ => ‘user’, // Specifically for user
    ‘context’ => ‘normal’,
    ‘priority’ => ‘high’,
    
        'fields' => array(
            array(
                'name'  => 'test mb for user',
                'desc'  => '',
                'id'    => 'test4',
                'type'  => 'string',
            ),
        )
    );

    and this is trying & result page from POSTMAN.

    fail update

    PS.
    meta-box version: 4.17.0
    mb_meta_user version: 1.2.3
    mb_rest_api: 1.3.2

    When I try with POSTMAN I logged in admin role user.
    I checked there is no problem to update other type(Post)’s MB data.

    Only problem in updating “user” type.
    I checked “activate” of my mb_user_meta plugin.

    I tried to find any errors from php-error.log or php-fpm-error.log.
    but didn’t find anything.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #13791

    Anh Tran
    Keymaster

    Hi Jiro, please change the code for meta boxes to:

    add_action( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    function prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            'id' => 'personal',
            'title' => 'Personal Information',
            'type' => 'user', // Specifically for user
            'fields' => array(
                array(
                    'name'  => 'test mb for user',
                    'desc'  => '',
                    'id'    => 'test4',
                    'type'  => 'text',
                ),
            )
        );
        return $meta_boxes;
    }

    Your code has 2 issues:

    • The type of the field should not be string, it’s text.
    • The function doesn’t return $meta_boxes.

    I’ve just made a test with Postman and here is the result:

    View post on imgur.com

    #13796

    Jiro
    Participant

    Thanks for your effort, but I found something strange.
    As your kindly support, that was not working with me.

    Instead of that in MB-REST-API…

    I found ‘$field’ has wrong value. in 198 line of class-mb-rest-api.php

    foreach ( $data as $field_id => $value ) {
                $field = rwmb_get_registry( 'field' )->get( $field_id, 'user' );
                $this->update_value( $field, $value, $object->ID );
            }

    and I change the code like below and everything works fine.

    foreach ( $data as $field_id => $value ) {
                $field = rwmb_get_registry( 'field' )->get( $field_id, 'user', 'user' );
                $this->update_value( $field, $value, $object->ID );
            }

    The code which I used for MB user meta is below.

    <?php
    add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    
    function prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            'title' => 'Personal Information',
            'type' => 'user', // Specifically for user
    
            'fields' => array(
                array(
                    'name'  => 'test mb for user',
                    'desc'  => '',
                    'id'    => 'test5',
                    'type'  => 'text',
                ),
            )
        );
        return $meta_boxes;
    }

    and when I print var_dump(rwmb_get_registry( ‘field’ )).
    it printed out as like below. as you can see there is “user” under the “user”.
    So strange, isn’t it?

    I never modify any Metabox source code in before includes (mb-rest-api, mb-user-meta,…)

    #13797

    Jiro
    Participant

    imgur is so strange in this moment. anyway,
    var_dump pritns…

    object(RWMB_Field_Registry)#4507 (1) {
      ["data":"RWMB_Field_Registry":private]=>
      array(3) {
        ["user"]=>
        array(1) {
          ["user"]=>
          array(1) {
            ["test5"]=>
            array(33) {
              ["size"]=>
              int(30)
              ["maxlength"]=>
    .......
    
    #13806

    Anh Tran
    Keymaster

    Hi Jiro, you’re right about the missing user in the update_user_meta call. I’ve just added that fixed.

    Regarding the var_dump, it’s not strange as the field registry (where all fields are stored) is structured like this:

    object_type
      specific_type
        field 1
        field 2

    For posts, object_type is post and specific_type is post type. For terms, it’s term and taxonomy. For users, as there’s no specific type, we use user and user.

    #13807

    Jiro
    Participant

    thanks Anh!

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.