Slide 1

Slide 1 text

Joe McGill MEDIA HANDLING IN 
 WORDPRESS, EXPLAINED WordCamp US 2017

Slide 2

Slide 2 text

Title of the Proposal WordCamp US 2016 Contributor Day

Slide 3

Slide 3 text

Currently photos are an attachment to a text post; but that kind of thinking lives in the older world of primarily text-based posts and when images were scarce. John Maeda • Design at WordCamp US 2016 make.wordpress.org/design/2016/12/04/design-at-wordcamp-us-2016 “ ”

Slide 4

Slide 4 text

Average Bytes Per Web Page November 2017 Other 20kb Video 792kb Fonts 112kb Scripts 457kb Stylesheets 89kb HTML 53kb Images 1815kb Total
 3378 kB Media Handling in WordPress Explained

Slide 5

Slide 5 text

WordPress 4.4 Responsive Images Use of SrcsetWDescriptor • chromestatus.com

Slide 6

Slide 6 text

The Mobile Test Upload a photo taken from your phone to a default install of WordPress on a basic $5/mo hosting plan really-cute-dog-photo.jpg HTTP error.

Slide 7

Slide 7 text

Media Handling in WordPress Explained Media Upload Flow 1. Media file uploaded HTTP Request 2. Upload handling async-upload.php • wp/v2/media 3. File and Post handling media_handle_upload()

Slide 8

Slide 8 text

1/Media File Uploading WordPress includes plupload.js, 
 a cross-browser file uploading API to deliver a consistent file uploading experience in the browser. Regardless of the method used, an upload is sent to WordPress in the form of an HTTP Post request.

Slide 9

Slide 9 text

2/Upload Handling WordPress generally handles media upload HTTP requests through async-upload.php or the wp/v2/media endpoint of the REST API. • Receive HTTP Request • Return either created attachment data or an error

Slide 10

Slide 10 text

3/File Processing WordPress processes the file data and saves the file data to the filesystem. For most uploads, this is done in wp_handle_upload(). Before saving the file, check: • User capabilities • Is filesystem is writable • The filetype is allowed Returns an array containing the file path, URL, and type on success.

Slide 11

Slide 11 text

4/Prepare and Save Post Data Attachments in WordPress are a combination of a WP_Post object with specialized post meta values. WordPress combines data passed in the upload request with data from the file itself to build post properties like title, content, and excerpt Once assembled, an attachment array is saved to the database using wp_insert_attachment() (or wp_insert_post() in the REST API).

Slide 12

Slide 12 text

PUBLIC SERVICE ANNOUNCEMENT: Media items may appear in the library before they are fully ready.

Slide 13

Slide 13 text

5/Generate Attachment Metadata WordPress creates additional attachment metadata in wp_generate_attachment_metadata(). Media types with support for attachment metadata: • Images • Audio • Video • PDF (Since WP 4.7)

Slide 14

Slide 14 text

5/Generate Attachment Metadata (Images) 1. Calculate image dimensions 2. Get file path relative to the uploads directory 3. Build an array of intermediate sizes to create by combining default image sizes with those defined by add_image_size() 4. Use wp_get_image_editor() to get a WP_Image_Editor for creating intermediate files. Default image editors use either ImageMagick or GD. 5. Pass the array of sizes to the editor's mulit-resize() method. Make the files, return the data. 6. Extract EXIF/IPTC data (aperture, credit, camera, copyright, shutter speed, ISO, etc.)

Slide 15

Slide 15 text

Example image attachment meta Media Handling in WordPress Explained array( 'width' 'height' 'file' // Includes upload path relative to upload_dir 'sizes' => array( 'thumbnail' => array( 'file', 'width, 'height', 'mime' ), 'medium' => array( 'file', 'width, 'height', 'mime' ), 'large' => array( 'file', 'width, 'height', ‘mime' ), // etc. ), 'image_meta' => array( 'aperture', 'credit', 'camera', 'caption', 'created_timestamp', 'copyright', 'focal_length', 'iso', 'shutter_speed', 'title', 'orientation', ‘keywords' ), )

Slide 16

Slide 16 text

5/Generate Attachment Meta (Audio/Video) 1. Read the file’s ID3 data 2. That’s it.
 
 well…
 
 unless…
 
 If the ID3 data includes a cover image, we need to process that file like an image upload and save it as the `post_thumbnail` for the audio or video attachment post.
 
 Neat.

Slide 17

Slide 17 text

5/Generate Attachment Meta (PDFs) 1. Load a WP_Image_Editor 2. Create a preview JPG from the first page of the PDF 3. Create intermediate sizes from the preview JPG

Slide 18

Slide 18 text

6/Save Attachment Metadata Once WordPress is finished generating all of the attachment metadata, it is saved to the database using the function wp_update_attachment_metadata() and the attachment ID is returned. Note: Additional metadata, like the `alt` attribute value, and context (for custom headers) may also be generated.

Slide 19

Slide 19 text

7/Prepare and Return the Response Once WordPress has finished processing the file, creating an attachment post, and saving all relevant metadata, it can prepare the response to send back to the client. Traditionally this will use wp_prepare_attachment_for_js() to create a JSON representation of the attachment. The REST API, returns a response which is similar, but not identical. Server errors return as an HTTP 503 error.

Slide 20

Slide 20 text

Review of WP Media Handling Systems 1. File Uploader (client) 2. Upload Handler (server) 3. File Handler (file system) 5. Data Handler (database) • Post data • Post metadata 6. Image Editor (intermediate sizes)

Slide 21

Slide 21 text

WordPress 5.0 (and beyond) What happens next is up to us, 
 but here are some ideas: 1. Asynchronous image generation 2. Make media a first-class data type (WP_Image) 3. Streamline integration for 3rd party services 5. Post-upload image optimization (ImageOptim) 6. Support modern formats (WebP, SVG) 7. ¯\_(ツ)_/¯ Media Handling in WordPress Explained

Slide 22

Slide 22 text

Joe McGill joemcgill • [email protected] THANK YOU.