Immagine caricata non visibile nel custom post type

16 ott 2013, 16:07:11
Visualizzazioni: 2.29K
Voti: 1

Sto scrivendo un plugin che dovrebbe permettere agli utenti di creare un custom post type chiamato "reseller" e allegare un'immagine. L'immagine viene caricata nella cartella uploads e appare nella libreria media con una miniatura vuota. L'immagine inoltre non è visibile nell'elenco dei "reseller" nell'interfaccia di amministrazione del custom post type.

Qualcuno riesce a vedere cosa ho dimenticato? Le funzioni rilevanti iniziano dalla riga 362 qui:

https://gist.github.com/codecowboy/80fd6363c58558a74c9e

Sezione rilevante:

   public function wps_reseller_cpt() {
        $labels = array(
            'name'               => _x( 'WPS Resellers', 'post type general name' ),
            'singular_name'      => _x( 'WPS Reseller', 'post type singular name' ),
            'add_new'            => _x( 'Add New', 'book' ),
            'add_new_item'       => __( 'Add New Reseller' ),
            'edit_item'          => __( 'Edit Reseller' ),
            'new_item'           => __( 'New Reseller' ),
            'all_items'          => __( 'All Resellers' ),
            'view_item'          => __( 'View Reseller' ),
            'search_items'       => __( 'Search Resellers' ),
            'not_found'          => __( 'No resellers found' ),
            'not_found_in_trash' => __( 'No Reseller found in the Trash' ),
            'parent_item_colon'  => '',
            'menu_name'          => 'Resellers'
        );
        $args = array(
            'labels'        => $labels,
            'description'   => 'Contiene i nostri reseller e i dati specifici dei reseller',
            'public'        => true,
            'menu_position' => 50,
            'supports'      => array( 'title', 'editor','image', 'thumbnail', 'custom-fields' ),
            'has_archive'   => false,
        );
        register_post_type( 'wps-reseller', $args );
    }

    public function create_reseller_profile_form(){
        require_once plugin_dir_path( __FILE__ ) . 'views/public-create_reseller_profile_form.php';
    }

    public function intercept_reseller_profile_form() {
        if ( !empty( $_POST['wps-reseller-user-submission'] ) {
            $this->wps_reseller_process_form();
        } else {
            return $template;
        }
    }

    public function wps_reseller_process_form() {
        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 ( wp_verify_nonce( $_POST['reseller_user_form'], 'add_reseller_form') &&
            !empty( $_POST['reseller-title'] ) &&
            !empty( $_POST['reseller-description'] ) &&
            !empty( $_FILES['reseller-image']) )
        {
            $file = $_FILES['reseller-image'];
            $uploads = wp_upload_dir();
            $new_reseller_profile_data = array(
                'post_status' => 'draft',
                'post_title' => $_POST['reseller-title'],
                'post_content' => $_POST['reseller-description'],
                'post_type' => 'wps-reseller'
            );

            $file_errors = $this->wps_parse_file_errors($file);
            $upload_overrides = array( 'test_form' => FALSE );
            if($file_errors['error'] == 0) {
                if($new_reseller_id = wp_insert_post( $new_reseller_profile_data )){
                    $uploaded_file = wp_handle_upload( $file, $upload_overrides );

                    $wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );
                    $attachment = array(
                        'post_mime_type' => $wp_filetype['type'],
                        'post_title' => preg_replace('/\.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
                        'post_content' => '',
                        'post_author' => '',
                        'post_status' => 'inherit',
                        'post_type' => 'attachment',
                        'post_parent' => $new_reseller_id,
                        'guid' => $uploads['baseurl'] . $uploads['subdir'] . '/' . $file['name']
                    );
echo $uploads['baseurl'].'<br />';
echo $file['name'].'<br />';
var_dump($uploads); exit;

                    $attachment_id = wp_insert_post( $attachment );
                    $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );

                    wp_update_attachment_metadata( $attachment_id,  $attach_data );
                    set_post_thumbnail( $new_reseller_id, $attachment_id );
                }
            }
        }
    }

    protected function wps_parse_file_errors($file){
        define('MAX_UPLOAD_SIZE', 200000);
        define('TYPE_WHITELIST', serialize(array(
            'image/jpeg',
            'image/png',
            'image/gif'
        )));

        $result = array();
        $result['error'] = 0;

        if($file['error']){
            $result['error'] = "Nessun file caricato o c'è stato un errore durante il caricamento!";
            return $result;
        }
        $image_data = getimagesize($file['tmp_name']);

        if(!in_array($image_data['mime'], unserialize(TYPE_WHITELIST))){
            $result['error'] = 'La tua immagine deve essere jpeg, png o gif!';
        }elseif(($file['size'] > MAX_UPLOAD_SIZE)){
            $result['error'] = 'La tua immagine era di ' . $file['size'] . ' byte! Non deve superare ' . MAX_UPLOAD_SIZE . ' byte.';
        }
        return $result;
    }

Domande:

  1. Perché la miniatura è vuota nella libreria media? Ecco cosa viene caricato:

  1. Come faccio a far apparire l'immagine nell'interfaccia di amministrazione?

Se aggiungo file alla libreria usando il media manager, le miniature e le immagini appaiono come previsto.

Se è rilevante, si tratta di un'installazione multisite.

3
Commenti

Per favore pubblica solo il codice rilevante qui. ... lo sai.

kaiser kaiser
16 ott 2013 16:55:47

@kaiser lo so? fatto.

codecowboy codecowboy
16 ott 2013 17:19:03

Dopo 25 domande assumiamo che tu lo sappia :)

kaiser kaiser
16 ott 2013 17:52:36
Tutte le risposte alla domanda 1
0

Perché non provi questo codice per il tuo allegato

 $uploaded_file = wp_handle_upload( $file, $upload_overrides );
 $attachment = array(
        'post_mime_type' => $uploaded_file['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
        'post_content' => '',
        'post_author' => '',
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_parent' => $new_reseller_id,
        'guid' => $uploaded_file['file']
    );
 $attachment_id = wp_insert_attachment( $attachment, $uploaded_file['file'] );
 $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );

 // aggiorna i metadati dell'allegato
 wp_update_attachment_metadata( $attachment_id,  $attach_data );
 //Imposta come thumbnail
 set_post_thumbnail ($new_reseller_id, $attachment_id );

Per recuperare l'immagine in evidenza del post

se sei all'interno del loop, allora

  the_post_thumbnail

ti restituirà l'immagine in evidenza. Altrimenti usa

   get_the_post_thumbnail ( $new_reseller_id )
17 ott 2013 13:18:05