The Complete Guide To The WordPress Media Library
The Complete Guide To The WordPress Media Library
It also has some limited photo-editing capabilities, so you can make adjustments as needed
right from your dashboard.
To access your Media Library, simply click on Media in your admin sidebar at any time:
The other three settings are in the sidebar. The first enables you to change the size of
the image by scaling it. Simply enter your desired height or width, and the other field will auto-
populate. Then click on the Scale button:
To crop your image so that it has a specific aspect ratio or dimensions, you can use
the Aspect Ratio and Selection fields under Image Crop:
Upgrading to WordPress Media Library Folders Pro provides additional integration with
the NextGEN Gallery plugin and Advanced Custom Fields. It also enables you to create
categories and tags to further organize your files.
Another functionality WordPress lacks is the ability to replace media files with new
versions. Since these types of files can be quite large, you don’t want to simply keep adding
new ones whenever you need to update your content.
This means that after uploading a new file, you’ll need to locate the old version and
delete it. You can save yourself some time by directly replacing old images with Enable Media
Replace by ShortPixel:
Real Media Library is another folders and files manager worth checking out to make
your file management a breeze.
Thanks to it, you’re able to take care of plenty of files such as pictures, videos, and
documents directly in WordPress. Nice thing to know: Real Media Library is available in 12
languages. If the free version isn’t enough for your needs, they also provide a buy the PRO
version with more features.
The wp_postmeta table stores any kind of metadata, such as the attached file URL,
image dimensions and mime types, and Exchangeable Image File format (EXIF) and
International Press Telecommunications Council (IPTC) metadata.
You may occasionally need to add custom metadata to attachments, such as the name
of the author of a document, an associated URL, or the location where a photo was taken.
Adding meta fields to attachments is a bit different from adding meta fields to posts, and
requires specific hooks and functions.
First, you’ll need to add all necessary custom fields to the Edit Media screen. You can
accomplish this task by filtering the available attachment fields through
the attachment_fields_to_edit filter in wp-admin/includes/media.php:
// https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata
$media_author = get_post_meta( $post->ID, 'media_author', true );
$form_fields['media_author'] = array(
'value' => $media_author ? $media_author : '',
'label' => __( 'Author' )
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'media_hacks_attachment_field_to_edit', 10, 2 );
The function has two arguments: the $form_fields array of form fields and
the $post object. First, get_post_meta retrieves the existing ‘media_author’ value, then
a ‘media_author’ element is added to the $form_fields array.
Finally, the callback returns $form_fields (see the code on Gist).
This will display a new field on the Edit Media page, which you can access by selecting the
relevant image from your Media Library and clicking on the Edit more details link:
The next step is saving the user input. You can accomplish this by hooking a new
function to the edit_attachment action:
$media_author = $_REQUEST['attachments'][$attachment_id]
['media_author'];
This function keeps just one argument: the $attachment_id of the current media file.
First, the function checks to see whether a valid value for the custom meta field has been sent.
Then it registers the value thanks to the update_post_meta function (see the code on Gist).
Now, you can retrieve the ‘media_author’ value thanks to the get_post_meta function:
if( $metadata ) {
$exif_data = array(
'aperture' => 'Aperture',
'camera' => 'Camera',
'created_timestamp' => 'Timestamp',
'focal_length' => 'Focal Length',
'iso' => 'ISO',
'shutter_speed' => 'Exposure Time',
'orientation' => 'Orientation' );
$exif = $metadata[$key];
$form_fields[$key] = array(
'value' => $exif ? $exif : '',
'label' => __( $value ),
'input' => 'html',
'html' => "ID][$exif]' value='" . $exif . "' />
);
}
}
}
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'media_hacks_attachment_fields_to_edit', 10, 2 );
Moreover, while the default post_status parameter of the $query object is set
to “publish”, the default attachment post_status is set to “inherit”. This means that no
attachments will be shown in archives unless we explicitly set the query post_status to “inherit”
or “any” (see WP_Query Type Parameters for further information).
With that being said, in order to show image archives, you have to define two functions.
The first function filters the arguments of a specified post type, and sets the
attachment has_archive property to true:
The second function sets custom values for the post_mime_type and post_status query
variables:
if( is_post_type_archive('attachment') ){
$query->set('post_mime_type', 'image/jpeg');
$query->set( 'post_status', 'inherit' );
}
}
}
add_action( 'pre_get_posts', 'media_hacks_pre_get_posts' );
The function is hooked to the pre_get_posts action hook, which is fired after the query
has been created, but before it’s executed. The $query object is passed by reference, not by
value, which means that any changes to the current instance of $query will affect the
original $query object.
For this reason, it’s important to verify which query you’re going to change (see this code
on Gist). Then if you enter https://yourdomain.com/?post_type=attachment in your browser,
you should see an archive of JPEG images: