Support » Plugin: CMB2 » Images not saving in file_list field type

  • newbars

    (@newbars)


    Hello, i create a field type file_list called portfolio where users can upload their work

    I have create a form so user fron the front end can upload the photos but data isnt saving up

    The code im using is

    foreach ( $otherfiles['name'] as $key => $value ) {
            if ( $otherfiles['name'][ $key ] ) {
                $file = array(
                    'name' => $otherfiles['name'][ $key ],
                    'type' => $otherfiles['type'][ $key ],
                    'tmp_name' => $otherfiles['tmp_name'][ $key ],
                    'error' => $otherfiles['error'][ $key ],
                    'size' => $otherfiles['size'][ $key ],
                );
    
                $attachment_id = media_handle_upload( 'trabajos_empresa', $post_id );
                update_post_meta( $post_id,'_studiare_portafolio', $attachment_id );
                var_dump($attachment_id);
                if ( is_wp_error( $attachment_id ) ) {
                    // There was an error uploading the image.
                    echo 'Error adding file';
                } else {
                    var_dump($attachment_id);   
                }
            }
        }

    `
    The code upload the photo to my media but isn’t attach to the field called portfolio so in the single page is showing that field blank

    I have to use another code for upload?

    • This topic was modified 3 months ago by newbars.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    As far as I’m aware, this looks like it’d be enough. Perhaps my biggest worry is if it’s making it into the media library in the first place. After that, it’s a question of which post it’s getting associated with and in this case, which post is receiving this _studiare_portafolio meta data.

    Have you confirmed that there’s meta key/value pairs of any sort in your usermeta table? Is it possible that you’re saving to the wrong $post_id ?

    All in all, it looks like it’s more of a general post upload issue rather than something specific to CMB2, but I’ll help out where possible still.

    Thread Starter newbars

    (@newbars)

    Hello, yes the photos are making into the media library

    The post making to _studiare_portafolio meta data. is:

    <input type="file" name="trabajos_empresa[]" id="trabajos_empresa" aria-required="true" multiple="multiple">

    I have other fields created with cmb2 and all save the data fine except this one, in this form i save too the featured image so i don’t know if that is making confict, here i show you the complete code for save the post

    // Add the content of the form to $post as an array
        $post = array(
            'post_author'    => get_current_user_id(),
            'post_title'    => $_POST['nombre_empresa'],
            'post_content'  => $_POST['descripcion_empresa'],
            'post_status'   => 'pending',   // Could be: publish
            'post_type' 	=> 'empresa' // Could be: 'page' or your CPT
        );
    
    	$post_id = wp_insert_post($post);
    
        wp_set_post_terms($post_id, array($_POST['cat_servicios']), 'categorias_servicios', true);
        wp_set_post_terms($post_id, array($_POST['no_empleados']), 'no_de_empleados', true);
        wp_set_post_terms($post_id, array($_POST['tempresa']), 'tamano_empleados_empresa', true);
        wp_set_post_terms($post_id, array($_POST['cl_empresa']), 'clientes_empresa', true);
        wp_set_post_terms($post_id, array($_POST['dep_empresa']), 'departamentos', true);
        wp_set_post_terms($post_id, array($_POST['mun_empresa']), 'municipios', true); 
      
        //Informacion adicional de la empresa
        update_post_meta($post_id, '_studiare_fundacion_empresa', $_POST['creacion_empresa']);
        update_post_meta($post_id, '_studiare_nit_empresa', $_POST['nit_empresa']);
    	update_post_meta($post_id, '_studiare_pweb_empresa', $_POST['pagina_empresa']);
        update_post_meta($post_id, '_studiare_certificaciones_empresa', $_POST['certificaciones_empresa']);
        update_post_meta($post_id, '_studiare_areas_negocio_empresa', $_POST['areas_empresa']);
        update_post_meta($post_id, '_studiare_productos_empresa', $_POST['pd_empresa']);
        update_post_meta($post_id, '_studiare_calle_empresa', $_POST['direccion_empresa']);
        update_post_meta($post_id, '_studiare_contacto_empresa', $_POST['nombre_contacto']);
        update_post_meta($post_id, '_studiare_correo_empresa1', $_POST['correo_elec1']);
        update_post_meta($post_id, '_studiare_correo_empresa2', $_POST['correo_elec2']);
        update_post_meta($post_id, '_studiare_tlf_empresa1', $_POST['nro_tlf1']);
        update_post_meta($post_id, '_studiare_tlf_empresa2', $_POST['nro_tlf2']); 
    
    	// For Featured Image
    	if( !function_exists('wp_generate_attachment_metadata')){
    		require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    		require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    		require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    	}
        
        if ( isset( $_FILES['post_image'] ) ) {
            $attach_id = media_handle_upload( 'post_image', $post_id );
        
            if ( $attach_id && ! is_wp_error( $attach_id ) ) {
                set_post_thumbnail( $post_id, $attach_id );
            }
        }
    
        $otherfiles = $_FILES['trabajos_empresa'];
        if ( isset( $_FILES['trabajos_empresa'] ) ) {
            foreach ( $otherfiles['name'] as $key => $value ) {
                if ( $otherfiles['name'][ $key ] ) {
                    $file = array(
                        'name' => $otherfiles['name'][ $key ],
                        'type' => $otherfiles['type'][ $key ],
                        'tmp_name' => $otherfiles['tmp_name'][ $key ],
                        'error' => $otherfiles['error'][ $key ],
                        'size' => $otherfiles['size'][ $key ],
                    );
                    $_FILES['trabajos_empresa'] = $file;
                    if ( is_wp_error( $attachment_id ) ) {
                        echo $attachment_id->get_error_message();
                    } else {
                        $attachment_id = media_handle_upload( 'trabajos_empresa', $post_id );
                        update_post_meta( $post_id,'_studiare_portafolio', $attachment_id );
                    }
                }
            }  
        }else{
            echo 'Agrega las fotos ';
        }
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hard to say exactly why it’s failing. I assume you’re getting successful attachment IDs that should be saving to _studiare_portafolio.

    I would probably move $attachment_id = media_handle_upload( 'trabajos_empresa', $post_id ); outside that else statement towards the end of your last reply. the if portion is always going to return false because $attachment_id isn’t set yet. It’s just null.

    Nitpicky detail, but wouldn’t hurt to sanitize your $_POST values up at the top. As is, you’re taking whatever gets submitted, which could definitely be a security risk.

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