photo credit: Marco Raaphorst via photopin cc

Add a checkbox to the media library in WordPress

So recently I had the situation where I needed to add a checkbox to the images in my media library in WordPress. The main purpose of it was to mark an image to be visible in the image gallery. On the frontend I would filter the images according to this flag. In this article I’ll explain how I did this.

Add a checkbox to the media library items

The first thing to do is add the checkbox element to the media library items. This can be simply done with the attachment_fields_to_edit -filter. In my functions.php  I added to following filter:

Save the checkbox

Now we have our checkbox when we open/edit a media library item. The next thing to do is save it’s value when the form is submitted. This can be easily done with the attachment_fields_to_save -filter. Add the following to your functions.php -file:

Use it in your template

The last thing to do is to use this filter in your template. The most basic way to do this is to put the following in your template file:

This of course is the most basic example of how you could implement this in your template. A more DRY way of doing this is by putting everything except the foreach() -loop in a separate function and only include the foreach() -loop in your template.

Visitors give this article an average rating of 4.9 out of 5.

How would you rate this article?

3 thoughts on “Add a checkbox to the media library in WordPress”

  1. erehr says:

    Can checkbox like that can be added but to gallery creator after selecting images ?

  2. marko says:

    I get whole bunch of orange errors. warning array_merge, undefinex index: post_type in post.php etc.

  3. pht says:

    Hi Giel, thanks for interesting post.
    There is a mistake in function add_filter(“attachment_fields_to_save”), please add return $post in the end of function because you are using filter hook

Leave a Reply