Custom Fields
Support MB Custom Table How to save image data using custom tables?

  • Creator
  • #10669
    Resolved makeagency


    There isn't any documentation covering each of the fields using custom tables and was wondering how to store image data?


Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
  • #10679
    Anh Tran


    Each custom field value is saved in a corresponding column. The field ID = column name.

    What image data do you want to store?



    So for example:

    MB_Custom_Table_API::create( 'my_custom_table', array(
        'address' => 'TEXT NOT NULL',
        'phone'   => 'TEXT NOT NULL',
        'email'   => 'TEXT NOT NULL',
    ) );

    Would we use 'TEXT NOT NULL' for an image?

    Anh Tran

    I got it. You still can save image in the database by encoding it. Use a tool like this, or base64_encode function to convert the image file into a text string. Then you can save it. However, you need to handle outputting the image by decoding it, because the value stored is the encoded data, not the real image.


    Hello, I think, related with this post:
    I save in the custom table of users named "usuarios", a avatar image of this user:

    $meta_boxes[] = array(
        //'title' => 'Custom avatar',
        'id'        => 'avatar',
            'type'  => 'user', // Specifically for user
        'storage_type'  => 'custom_table',    // Important
            'table'         => 'usuarios', // Your custom table name 
            'fields' => array(
                'name'            => 'Subir/Upload Avatar',
                'id'              => 'avatar',
                'type'            => 'image_advanced',
                'max_file_uploads' => 1,

    The value of the image saved is: a:2:{i:0;s:0:"";i:1;i:734;}

    It's ok? Or I have to make the econding? How can I make this encoding?

    To get the image in a post, I have to make this function:

    1.- First I get the row of the user in $r
    2.- Take the value of the image: $resultado = $r['avatar'];
    3.- $resultado = 'img src="'.$r['avatar'].'" /> '; // don't put here the initial < because don't show the code the post...

    But this don't run, don't know how to save and get the image to show in frontend...

    In the documentation:

    $images = rwmb_meta( 'info', array( 'limit' => 1 ) ); --> I modify the field 'info' for my image field: $r['avatar']
    $image = reset( $images );
    $izquierda .= '<img src="'.$image['url'].'" />';

    But don't run...

    Can you help me please?


    Anh Tran

    Hi Sergio,

    The original post was about storing image data, e.g. storing full image in the DB.

    In your case, you are using image_advanced field, which stores image IDs in the DB, which is the default behavior of the plugin and is totally fine. The data is automatically serialized, that's why you see a:2:{i:0;s:0:””;i:1;i:734;}.

    The reason your helper doesn't work is because you register the meta box for users. So, you need to set the object_type for the rwmb_meta function, like this:

    $images = rwmb_meta( 'info', array( 'object_type' => 'user', 'limit' => 1 ) );


    Hello, sorry for mixing different threads...
    I do not just work:
    I try:

    global $wpdb;
    $r = $wpdb->get_row($wpdb->prepare("SELECT * FROM $bbdd WHERE ID = $ID"), ARRAY_A );
    $images = rwmb_meta('info', array( 'object_type' => 'user', 'limit' => 1 ) );


    $images = rwmb_meta('avatar', array( 'object_type' => 'user', 'limit' => 1 ) );


    $images = rwmb_meta( $r['avatar'] , array( 'object_type' => 'user', 'limit' =>> 1 ) );

    and in the 3 options it return null...

    I can't get the $image['url']; after $image = reset( $images );

    what am I doing wrong? (I use custom table "usuarios" related with "users" and the field name or id of the advanced image is "avatar").


    Anh Tran

    Hi Sergio,

    I know why. You need to pass the user ID to the function as the 3rd param. By default, the function takes the current post ID. So the code should be:

    $images = rwmb_meta( 'avatar', ['object_type' => 'user', 'limit' => 1], get_current_user_id() );

    Perfect!!! It run well. Thanks!

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