Custom database with custom upload_dir for images problem

Support MB Custom Table Custom database with custom upload_dir for images problem

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #43042
    Rob van TuinRob van Tuin
    Participant

    Hi,

    I'm using a Meta-box custom table for one of my post-types which is working fine for most of it, the problem I have is with retrieving the image data.

    For the images, I use a custom upload_dir I'm not able to get the image data, I get the database field data 'a:1:{i:0;s:81:"https://wp-plugin.test/wp-content/uploads/events/images/main/test-image.jpg";}' but I can not get it decoded. It works fine if I use the media folder.

    function get_event_meta($event_id){
    	global $wpdb;
    	$table = EVENT_TABLE_NAME;
    	$event_id = get_the_ID();
    	$event_query = $wpdb->prepare("SELECT * FROM $table WHERE id=%s;", $event_id );
    	$event = $wpdb->get_row( $event_query, ARRAY_A );
    	$event['main_image'] = rwmb_meta( 'image_main', EVENT_TABLE_ARG, $event_id);
    	return $event;
    }

    Any insight on what I do wrong?

    #43064
    PeterPeter
    Moderator

    Hello Rob,

    What is "decode" in this case? You can simply follow the documentation below to get the field value that is stored in a custom table.
    https://docs.metabox.io/extensions/mb-custom-table/#getting-field-value

    #43069
    Rob van TuinRob van Tuin
    Participant

    Decode is trying to convert this string 'a:1:{i:0;s:81:"https://wp-plugin.test/wp-content/uploads/events/images/main/test-image.jpg";}' into normal variables.

    I followed the documentation as you can see in the code
    $event['main_image'] = rwmb_meta( 'image_main', EVENT_TABLE_ARG, $event_id); but it always comes back with an empty array.

    #43076
    PeterPeter
    Moderator

    Hello,

    You can use the WordPress function maybe_unserialize() to decode the value
    https://developer.wordpress.org/reference/functions/maybe_unserialize/
    https://stackoverflow.com/questions/25766378/wordpress-show-unserialize-data-in-a-smart-way

    Or use the helper function:

    $args = [
        'storage_type' => 'custom_table',
        'table'        => EVENT_TABLE_NAME,
    ];
    $value = rwmb_meta( 'image_main', $args, $event_id );
    echo $value;
    #43121
    Rob van TuinRob van Tuin
    Participant

    Yes, thank you, the maybe_unserialize option works, but the helper function does not work for images, it works for other cloneable fields fine.

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