wp_prepare_attachment_for_js example


    public function to_json() {
        parent::to_json();

        $value = $this->value();
        if ( $value ) {
            // Get the attachment model for the existing file.             $attachment_id = attachment_url_to_postid( $value );
            if ( $attachment_id ) {
                $this->json['attachment'] = wp_prepare_attachment_for_js( $attachment_id );
            }
        }
    }
}
'full' => array( 'url' => $this->setting->default ),
                    );
                }

                $this->json['defaultAttachment'] = $default_attachment;
            }

            if ( $value && $this->setting->default && $value === $this->setting->default ) {
                // Set the default as the attachment.                 $this->json['attachment'] = $this->json['defaultAttachment'];
            } elseif ( $value ) {
                $this->json['attachment'] = wp_prepare_attachment_for_js( $value );
            }
        }
    }

    /** * Don't render any content for this control from PHP. * * @since 3.4.0 * @since 4.2.0 Moved from WP_Customize_Upload_Control. * * @see WP_Customize_Media_Control::content_template() */
/* * This can still be pretty slow and cause timeout or out of memory errors. * The js that handles the response would need to also handle HTTP 500 errors. */
    wp_update_image_subsizes( $attachment_id );

    if ( ! empty( $_POST['_legacy_support'] ) ) {
        // The old (inline) uploader. Only needs the attachment_id.         $response = array( 'id' => $attachment_id );
    } else {
        // Media modal and Media Library grid view.         $response = wp_prepare_attachment_for_js( $attachment_id );

        if ( ! $response ) {
            wp_send_json_error( array( 'message' => __( 'Upload failed.' ) ) );
        }
    }

    // At this point the image has been uploaded successfully.     wp_send_json_success( $response );
}

/** * Handles uploading attachments via AJAX. * * @since 3.3.0 */
Home | Imprint | This part of the site doesn't use cookies.