File Field Type upload_dir

Support General File Field Type upload_dir

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #37397
    Paul TsungPaul Tsung
    Participant

    Hi,

    Is there a way to change the upload_dir for the file field type using the custom post object id or the meta object ID as sub-folders?

    Like /uploads/{custom_post_object_id}/images

    Thanks.
    Paul

    #37403
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Yes, it is possible. You can add the post ID to the custom upload dir when registering the file field. For example:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    function your_prefix_function_name( $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'  => __( 'Post Meta', 'your-text-domain' ),
            'id'     => 'post-meta',
            'fields' => [
                [
                    'name'                     => __( 'File', 'your-text-domain' ),
                    'id'                       => 'file',
                    'type'                     => 'file',
                    'upload_dir'               => WP_CONTENT_DIR . '/' . $post_id . '/invoices/',
                ],
            ],
        ];
        return $meta_boxes;
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.