file field path with MB Group
- This topic has 5 replies, 2 voices, and was last updated 2 years, 8 months ago by
Long Nguyen.
-
AuthorPosts
-
August 21, 2022 at 11:03 AM #37951
Paul Tsung
ParticipantHi,
I am having issues getting the path from a file field when using it within a clone group.
I went through the documentation for working with sub-field within a group. I see it mentions to use get_attached_file() to get more info for file field. The get_attached_file() function requires an attachment ID. But the post meta is storing the value in an array like this.
a:1:{i:0;a:2:{s:36:"_index_deliverable_video_file_upload";s:19:"_file_6301a522a9e28";s:29:"deliverable_video_file_upload";a:1:{i:0;s:108:"http://lwpdigicomp.local/wp-content/videos/419/sample.mp4";}}}
So what is the ID I should use? And is this the right way to get the meta object for the file field?
Thanks.
August 22, 2022 at 9:23 PM #37968Long Nguyen
ModeratorHi Paul,
If you use the field type
file
and save the file uploaded in a custom folder, the value saved in the database is the file URL, not the file ID. Please get more details on this documentation https://docs.metabox.io/fields/file/#upload-to-a-custom-folderAugust 23, 2022 at 1:10 AM #37976Paul Tsung
ParticipantHi Long,
Thank you for getting back to me.
It's interesting because it looks like I get the path key when I use a custom folder with the file field when not using it within a group.
So does that mean that if I am using it in a group, I can only get the URL and need to derived the path from the URL? The reason I need the path is because I want to do some processing on the file after it is uploaded to the server.
Thanks.
August 23, 2022 at 10:14 PM #37989Long Nguyen
ModeratorHi Paul,
Not only the subfield in a group but also the top field. If you set to upload the file to a custom folder, the value saved in the database is the file URL, not the file ID like upload to the WordPress standard folder.
August 24, 2022 at 2:17 AM #38001Paul Tsung
ParticipantHi Long,
Maybe I am not understanding correctly. But I created a test field group with this code.
add_filter( 'rwmb_meta_boxes', 'create_watch_fields' ); function create_watch_fields( $meta_boxes ) { $post_id = null; if ( isset( $_GET['post'] ) ) { $post_id = intval( $_GET['post'] ); } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = intval( $_POST['post_ID'] ); } $meta_boxes[] = [ 'title' => 'Watch Details', 'id' => 'watch-details', 'post_types' => 'watch', 'fields' => [ [ 'name' => 'Name', 'id' => 'watch-name', 'type' => 'text', ], [ 'name' => 'Watch Video', 'id' => 'watch-video', 'type' => 'file', 'upload_dir' => WP_CONTENT_DIR . '/watchvideos/' . $post_id . '/', 'max_file_uploads' => 1, 'force_delete' => true, ] ] ]; return $meta_boxes; };
In the postmeta table, I see the meta_value with the URL only.
And then I use rwmb_meta( 'watch-video' ) to get the meta object.
There is a path key in the return object with the custom path.
{"http:\/\/lwpdigicomp.local\/wp-content\/watchvideos\/417\/sample.mp4":{"icon":"
","name":"sample.mp4","path":"\/Users\/paultsung\/Local Sites\/lwpdigicomp\/app\/public\/wp-content\/watchvideos\/417\/sample.mp4","url":"http:\/\/lwpdigicomp.local\/wp-content\/watchvideos\/417\/sample.mp4","title":"sample","edit_link":""}}
So is this not suppose to happen when I use a custom upload folder for the file field?
Thanks.
August 24, 2022 at 11:29 PM #38023Long Nguyen
ModeratorHi,
This is supposed to happen when you upload the file to a custom folder. You will get a custom path (URL) of the file when getting the field value. Please read more on the documentation https://docs.metabox.io/fields/file/#upload-to-a-custom-folder
Unlike the normal case, where files are added to the WordPress Media Library, files uploaded to custom folders are not available in the Media Library. Thus, the data saved in the custom fields is file URL, not attachment ID.
-
AuthorPosts
- You must be logged in to reply to this topic.