Soporte » WordPress Avanzado » como subir imagen destacada desde formulario externo

  • raulbarroso26

    (@raulbarroso26)


    Como podría subir una entrada con imagen destacada desde formulario externo? he logrado que la entrada se publique pero sin la imagen.

    en function.php tengo este código:

    function my_update_attachment($f,$pid,$t='',$c='') {
        wp_update_attachment_metadata( $pid, $f );
        if( !empty( $_FILES[$f]['name'] )) { //New upload
          require_once( ABSPATH . 'wp-admin/includes/file.php' );
    
          $override['action'] = 'editpost';
          $file = wp_handle_upload( $_FILES[$f], $override );
    
          if ( isset( $file['error'] )) {
            return new WP_Error( 'upload_error', $file['error'] );
          }
    
          $file_type = wp_check_filetype($_FILES[$f]['name'], array(
            'jpg|jpeg' => 'image/jpeg',
            'gif' => 'image/gif',
            'png' => 'image/png',
          ));
          if ($file_type['type']) {
            $name_parts = pathinfo( $file['file'] );
            $name = $file['filename'];
            $type = $file['type'];
            $title = $t ? $t : $name;
            $content = $c;
    
            $attachment = array(
              'post_title' => $title,
              'post_type' => 'attachment',
              'post_content' => $content,
              'post_parent' => $pid,
              'post_mime_type' => $type,
              'guid' => $file['url'],
            );
    
            foreach( get_intermediate_image_sizes() as $s ) {
              $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
              $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
              $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
              $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
            }
    
            $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
    
            foreach( $sizes as $size => $size_data ) {
              $resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
              if ( $resized )
                $metadata['sizes'][$size] = $resized;
            }
    
            $attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);
    
            if ( !is_wp_error( $id )) {
              $attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
              wp_update_attachment_metadata( $attach_id, $attach_meta );
            }
    
            return array(
              'pid' =>$pid,
              'url' =>$file['url'],
              'file'=>$file,
              'attach_id'=>$attach_id
         );
            // update_post_meta( $pid, 'a_image', $file['url'] );
          }
        }
    }

    y en la pagina donde se publica el formulario este:

    if($_SERVER['REQUEST_METHOD'] == 'POST'):
    
            $titulo 	= $_POST  ['titulo'];
            $precio 	= $_POST  ['precio'];
            $descripcion    = $_POST  ['descripcion'];
            $url 		= $_POST  ['url'];
            $imagen_post    = $_FILES  ['imagen'];
            $autor		= get_current_user_id();
    
            $platos_id = wp_insert_post(array
                    (
                    'post_title'  	=> $titulo, 
                    'post_type'	=> 'platos', 
                    'post_content'  => $descripcion,
                    'post_author'	=> $autor,
                    'post_status'   => 'pending',
                    )
                  );
                  $watch = wp_insert_post($platos_id);
                    $att = my_update_attachment('watch_image',$watch);
                    update_field( "imagen", $att['attach_id'],$watch );
    
                    //Actualizar campos personalizados - función plugin empleado
                    update_field( "field_5f5d3380b5a8d", $url, $platos_id );
                    update_field( "field_5f5d339fddd49", $precio, $platos_id );
    
                    my_update_attachment($f,$pid,$t='',$c='');

    he tratado de hacerlo con ACF pero nada.

    • Este debate fue modificado hace 1 mes por Jose Luis. Razón: Etiquetar código
Viendo 1 respuesta (de un total de 1)
Viendo 1 respuesta (de un total de 1)
  • Debes estar registrado para responder a este debate.