Home » How to Write a Custom WP CLI Script to Remove Old Images and Attachments from WordPress Posts

How to Write a Custom WP CLI Script to Remove Old Images and Attachments from WordPress Posts


Since not many tutorials are out there on how to create a custom WP CLI command, I figured I’d share the one I’ve been working on for work as an example. This tutorial will show you how to write a custom WP CLI script to remove old images and attachments from WordPress posts (the image tags from the content as well as remove any attachments within a date range). Since this is a repurposed copy of something that was used on several production sites, this has been tailored to allow you to exclude top performing posts by post ID, as well as allowing you to target posts with other passable command-line options like before date, after date, post status, post type, etc.

An Introduction to WP CLI

If you aren’t familiar with WP CLI and manage your own server(s) that host WordPress sites, you should look into installing it here: http://wp-cli.org/. It will basically give you everything you need to administer your WordPress site (including sites on multisite) through the command-line interface. For a sysadmin that deals with WordPress installations, this can be worth its weight in gold as you can easily automate your WordPress related admin tasks with a simple BASH script or something of the sort.

Adding a Custom CLI Command

First we need to start by creating a PHP class that we can include from functions.php. Just so we’re clear on the bare-bones requirements of a custom WP CLI class, this is really all you need:

Obviously we’ll want to replace “YourSiteName” with whatever your site’s name is in StudleyCaps, and then replace “yoursitename” with the case-sensitive name of the command you’d want to run in the command-line. After that we’ll need to require that PHP class we just made by adding the following to functions.php:

This assumes that the filename of your PHP class is cli.php and it lives in your active theme’s directory.

Custom CLI Script to Remove Old Images (image tags and attachments)

The following is the Custom WP CLI script to remove old images and attachments from WordPress posts that I wrote for this example. It has a synopsis of how to run this through the command line with WP CLI, along with all the optional parameters that you can pass at the time you run the command.

This last code snippet is the entirety of a script I wrote that strips all image tags from within the post content (with regex) and removes any attachments from within a date range, post type, post status, etc., excluding any posts that were passed to be excluded. Due to the savage nature of this CLI script I’ve added a –live flag that is required for the script to do any actual deletions. There are several flags that allow you to pass the before and after dates (assigning a date range of posts to grab from), post statuses and types to include, post IDs to exclude, and pause between each page of posts. A synopsis of arguments for this command can be found in the comment block above the top function.

As always, if you have any questions, feel free to drop a comment below and I’ll do my best to respond as quick as possible 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *