Podcast Episode

347 – Quickly Work On Your Site with WP-CLI

Announcements

Is there a plugin for that?

With more than 50,000 plugins in the WordPress repository, it’s hard to find the perfect one. Each week, I will highlight an interesting plugin form the repository.

For more great plugins, download my 50 Most Useful Plugins eBook.

  • Keyring Social Importers is a plugin that will allow you to pull in content from your social sites and create posts on your WordPress site.

Quickly Work On Your Site with WP-CLI

Your WordPress site can be controlled on the command line. It’s a tool that allows you to maintain WordPress without the standard GUI. You can find out more at wp-cli.org

Some features of using the command line:

  • Far easier to perform bulk operations
  • Better visibility into WordPress internals
  • Cron, cache, transients, etc.
  • CLI is not subject to timeouts the way a browser would be
  • Easier filesystem operations
  • Able to script (allthethings)

We can use WP CLI to:

  • Deploy WordPress core updates
  • Install/activate/update/delete themes + plugins
  • Troubleshoot issues with WP_Cron, transients, cache, etc.
  • Schedule maintenance routines
  • Data migrations and transformations

Practice these things with WP-CLI:

Fun with posts:
- create 200 dummy posts on your site
- list all of the post_ids on your site as a csv
- change the title on one of your posts to something interesting
- create and save a text file with some fun text in it
- set the contents of the post with the new title to the contents of the text file 
- create a tag called "important"
- add this tag to the post with the new title

Themes/Users:
- install twentyeleven
- activate twentyeleven on your site
- search for a .org theme with "news" in the name of the theme
- install one that looks good
- activate it
- disable and delete twentyeleven

Fun with important plugins:
- Install and activate jetpack
- Install woocommerce
- issue a command that will tell you how many plugins are installed
- list all installed plugins
- deactivate and reactivate woocommerce
- update jetpack

Images:
- create a folder of a few images on your computer
- import them to your media library
- regenerate thumbnails for those images

Ka-boom!:
- export a copy of your database
- delete your entire database (!!)  your site is now totally broken
- restore your database from the file that you exported

Export/Import:
- export a wpcom site
- import that to your local site

Thank You!

Thank you to those who use my affiliate links. As you know I make a small commission when someone uses my link and I want to say thank you to the following people. For all my recommended resources, go to my Resources Page

Full Transcript

Business Transcription is provided by GMR Transcription.

On today’s episode, we are going to talk about how we can quickly work on our website by using WP-CLI. Right here on Your Website Engineer podcast, episode number 347.

Hello everybody, welcome back to another episode of Your Website Engineer podcast. My name is Dustin Hartzler. Today we’re going to explore the wonderful world of WP-CLI. We’ll talk all about it in a second. I’ve got one announcement and one plugin to share with you.

The announcement of the week – it’s been kind of a slow news week when it comes to WordPress this week, but this is coming out of Automattic over at The WordPress.com Blog. But there is a brand-new editor for the WordPress mobile apps. At first glance the new Aztec editor may look like the old editor, but it doesn’t work exactly the same way. There’s been some new improvements, some things like the overall user experience is now smoother and snappier and images can now be inserted much quicker. The spellcheck now actually works reliably. There’s Undo and Redo tools. That means you can easily fix your mistakes, or move between different versions of your text when writing and rewriting. You can now use dictation, so you can draft your thoughts with your voice, and there’s full support for accessibility technologies on iOS VoiceOver and Android’s TalkBack.

So, there’s a lot of cool things that are built into this, and it is in the latest version or version 8.0 and higher on iOS or 7.8 in Andriod. When you install that version, it’s going to ask you to try out this Aztec editor in beta form. So, you have the ability to play around with it. It is in beta, so that means it’s not 100 percent ready for everyone, but it works really well and I’m really excited to use it. I’ve been blogging a lot over at dustin.blog and I use – primarily if I’m using some sort of image, I will use it straight from my phone because it’s just handy to upload images straight from my phone. And it is very snappy, especially if you’re copying and pasting text, and you wanna upload an image and you wanna do some other things.

Like, it’s really nice that I can upload an image and then I can change the tags and the categories, and then I can set some different things up while it’s all uploading and it doesn’t slow that down. So, that’s nice and that’s what I use for dustin.blog, like I said. And I use the WordPress editor pretty much every day or the WordPress app almost every day. You know, just digging in, looking at stats, looking at things. And the editor – this new feature is really cool. So, go ahead and check that out if you’re interested in managing your website from your mobile device.

The plugin of the week I wanna share with you this weekend – there’s more than 50,000 plugins in the WordPress repository that are free, plus all the premium ones that are out there, but the one I’m gonna talk about today is called Keyring Social Importers. And this is a plugin by the lead of Jetpack, Beau Lebens. And this package will – it’s a whole bunch of social importers, and it allows you to have the ability to pull in your content that gets created on other sites and republish it on your WordPress site. So, rather than leaving your other platforms in control of everything that you’ve put on the web, you can host it yourself with WordPress. And so, there’s importers for Delicious, Fitbit, Flickr, Foursquare/Swarm, Instagram, Instapaper, Jetpack/WordPress.com, Moves, Nest Cameras, Pinterest, TripIt, and Twitter. And so, those are everything that you can pull in.

You can look-up all that information, and there’s a lot of things you can pull in with each of these. So, like for example, on Fitbit it’s very basic, but you can import your data and it creates a simple summary post, or you can just create a summary post that only says about a statement about how many steps you took that day. With, let’s see, Instagram you can pull each photo in from your Instagram account and upload it to your media library. So, that’s really nice. Just tons of things you can do to pull in information from those other sites. So, you can even do – with Twitter, you can do every tweet will be downloaded as an individual post. And so, if you wanted to save and archive all of your content on a WordPress site, you can go ahead and do that with the plugin called Keyring Social Importers. You can find a link for this in the show notes for episode number 347, or you can just search for “Keyring Social Importers” on the WordPress repository.

Alright, today we are going to dive-in to talk about how we can quickly work with and manage our WordPress websites with this little technology called WP-CLI. We’ll talk about what it is in a second, but I just wanted to say that I really like using WP-CLI and I use it pretty much daily. I like to update my websites – especially the plugins, that’s the main thing I use if for. I will update the plugins via the command-line, and then I push those changes to my version control and then the version control gets pushed out to the live sites. And so, I do all of this to manage it because it gives me a headless WordPress, if you will. I don’t have to log in to a dashboard, and then click to the plugins area, and then click from plugins to updates, and all those steps. It just makes it really simple and really quick for me to open a terminal window, and type in some commands and then automatically be off to the races and doing things on my WordPress website.

So, let’s talk about what it is. WP-CLI is a command-line tool, that’s what the C-L-I stands for, and it’s specifically made to manage your WordPress websites through the command-line. With a few simple commands, you can manage WordPress without ever needing to login to your admin and navigate through the pages. So, we’re gonna talk about a few things – about how you can do, how you can get set up and what not. Some of the things you may use WP-CLI for: it’s easier to do bulk operations. If you wanted to delete a bunch of comments, you can do that all from the command-line. If you wanted to just do some bulk things, you know approve comments, or – I’m trying to think of other things you wanna do. You can do a lot of things very quickly via the command-line. It’s better visibility into the WordPress internals, so you can see kind of a little bit more about what’s going on behind the scenes with WordPress.

You can update and flush the CRON, the cache, the transients, you can do a bunch of stuff with that and just kind of really making sure that your installation is clean. Your WP-CLI session is never subject to timeouts, like browsers do sometimes. For an example, maybe you’re deleting spam posts or spam comments on your website, and you open up the list and you see 1,000 at a time. And you click the button to select all and then you hit “delete”. Well, most likely all thousand aren’t gonna delete because you’re gonna run into some sort of timeout issue when using it in the browser. But with WP-CLI you can just kind of – it just forces its way through all that and can delete all of those extra comments. You’re able to script things.

So, with WP-CLI if you wanted to quickly and easily create a new post with the title of something, and put some generic content in there and then create a new user – I’m thinking of an example of having to setup a classroom website, and so every student that’s in the class has the exact same website. Well, you can write this little script that just, one right after another, throws out WP-CLI commands. And it’s first gonna say, “Okay, we’re gonna download 2017, and we’re gonna remove all the extra themes that are in there. And then we’re going to delete the Hello Dolly plugin. And then we’re gonna install these three plugins, and then we’re gonna do this, this, and this.” So, you can just script this whole thing out. And so, you create the first site, and then you create another site, and another site, and all you have to do is run the command once, and it just does all the things, which is really, really cool.

So, you can also use WP-CLI to deploy WordPress core updates or theme updates or plugin updates. You can troubleshoot issues with the CRON. You can schedule maintenance routines. You can do data migrations and transformations. You can do a lot of things with WP-CLI.

So, let’s go ahead and talk a little bit about it. The requirements of WP-CLI are – there’s just three basic requirements. You have to be using PHP 5.3 or later, that’s pretty standard. Most WordPress hosting companies are already using WordPress 5.5 or 5.6. You have to be using WordPress 3.4 or later, which that was released many, many moons ago so you’re probably using that. And then you have to be using a UNIX-like environment like Linux. So, you can run WP-CLI right there on your MAC or your PC, or you can run it and communicate to your server in a cloud, wherever that may be, through an SSH connection.

I’m not gonna go ahead and talk about WP-CLI, how to install it because it’s kind of a pain to talk about in an audio podcast. But there’s some steps involved that I will link to in the show notes, just a couple commands that you run via the terminal or SSHN, then you can run them from your web post. And once you get it set up, then there’s just some basic commands that you can go ahead and do. Another nice thing that you can do with WP-CLI, and it’s not super helpful because most of the time you already have WordPress set up, but you can use the command “wp core install” and you can just go ahead and install it and there’s a bunch of other parameters that you can pass to it. You can pass your URL, you can pass your title, you can pass the username, the admin password, and the admin email address. You can pass all this information and boom; your webhost will setup a WordPress just like that with those details that you have set up.
You can install themes right from the WordPress repository. So, you’d go “wp themes install 2010” or 2012 or 2018 or whatever theme that you wanna install, as long as it’s on the WordPress repository, you can install it right with the command-line interface. Another thing that I use regularly is installing plugins right from the command-line. Again, these have to be in the WordPress repository, but it takes almost zero time to get a plugin installed. So, example, we wanna add WooCommerce to our website. Just type in “wp plugin install woocommerce”, hit enter, it’s going to show you a little bit of downloading, it’s there and then done. And then you type “wp plugin activate woocommerce” and it’s automatically installed. I use this pretty much daily when I’m troubleshooting tickets in the WooCommerce support area, and it’s the plugins that I’ve never used before.

And I just go ahead and I’ll type in – I’ll load them up to my site and then I activate them all in the same go. And then I can start navigating through the dashboard and through the settings panel without having to go and activate the plugin and install it manually and whatnot. So, you can do all that. You can also update all of your versions of plugins and whatnot from the WP-CLI. So, you can say “wp core update”. You can do “wp core update” and then “-- version”. You can say I wanna update to version 4.0 or update to version 3.9 and go back a few versions. You can also do “wp plugin update woocommerce” if WooCommerce is outdated, or if you wanna do all of them in one fell swoop, you can do “wp plugin update -- all”. And that will just update all of the plugins in your WordPress installation. So, that makes it super handy.

There’s a ton of the commands that you can go through – I’ll just highlight some of them here and some of the things you can do with them. But, let’s see, some of the other ones are “wp cap” and you can manage user capabilities. So, you can setup different administrator levels within the users inside of WordPress. You can do “wp comment” and that manages comments, so you can go and you can approve comments, or you can delete comments. There’s “wp export”, which will allow you to export WordPress content to a WXR file. We’ve got “wp help” and that gives you command-line interface information, like on how to do things. “wp import”, you can import content from that WXR file. You’ve got “wp media” so you can manage your attachments and all of the media in your media library. “wp menu” will allow you to list, and create, assign, and delete menus. “wp option” will allow you to manage all your options.

Let’s see, “wp plugin” we talked about. “wp themes” we talked about. “wp post”, lots of things you can do with post. You can create a draft of a post right from within the command-line, which is super handy and really easy. “wp role”, you can manage user roles. You can – “wp search-replace” you can do search and replace strings within the database right from the command-line, which is super handy. “wp sidebar”, you can manage sidebars. There’s one called “wp superadmin” so this manages super admins on a WordPress multi-site installation. If you wanna manage your categories or tags or taxonomies or terms, they’ve got those, “wp taxonomy” and “wp term”. They also have “wp widget” to manage widget sidebars.

Each of these commands have a subset of commands. So, let’s go ahead and we’ll look at the WP post real quick. And these are all “wp post”, but it’s just a big mouthful to actually say that. So, under the “wp post” you can say “wp post create” to create a new post or “wp post delete” to delete an existing post. “wp post edit” to launch the system editor and edit post content. You can “wp post generate” to generate some posts. If you wanna generate 10 posts to kind of fill in your website, you can go ahead and do that. “wp post list” will get a list of all your posts. “wp post terms” so you can manage all the terms or the categories on your posts. And let’s see, “wp post update” and you can update one or more existing posts. So, the catchy part here is with this is you have to know the ID of the post. So, you might have to say “wp post update one, two, three” and then you can say to “post status to draft”.

So, you can update the existing post from a published post to a drafted post. Or you can say “wp post delete one, two, three” and it will delete the post with the ID of 123. So, it’s gonna take a little bit of getting used to, and I have a whole list of example things that I’m going to add in the show notes – of things that you can try to do in the command-line. Some of them – it’s just a fun exercise, I think there’s 10 or 20 things that you can do. Once you have WP-CLI installed, then you can go ahead and see – let’s try to remove the third post from the bottom in all of my posts. How do I do that? How do I do – just different little things, and you can learn how to do it during the command-line.

I find that doing some exercises helps to kind of build the muscles because I’m not using this every day, but I can see the immense value in just being able to do a lot of things – if you have to do a lot of system things within WordPress without having to navigate to a bunch of pages. It’s either that or open-up a bunch of tabs to do a bunch of things within WordPress. But I think WP-CLI can handle a lot of these things, and it’s a great tool to have in your developer tool belt.

As we wrap up the month here of July, when we talked about the advanced WordPress topic – we talked about how to customize WordPress to fit your needs with some custom post types. And we talked about the different template hierarchies. And hooks and filters was last week, and kind of diving into how those things work. And then today we had the WP-CLI. Next month we’re going to dive-in and talk about navigating the confusing world of websites, just a bunch of the different terms and technologies and things that we can talk about to just kind of make you more familiar about how all of the pieces of the puzzle work. Maybe you’ve just set up WordPress and that’s all you know, but we are definitely going to dive-in and talk about domains and different types of hosting and how to set up domain name records and all those things. That’s what’s coming in the month of August.

I’ve had fun this month, kind of diving back into the WordPress space, and we’re gonna continue to do that in the month of September, talking about different WordPress things. But next month, like I said, we’ll talk about just general website things and how we can use it to our advantage, especially if we’re trying to help out friends or family that are trying to build WordPress websites. So, that’s what I wanna share with you this week. Take care, and we’ll talk again soon. Bye, bye.

    • Fabian Reply

      Hi Dustin, thank you for this. I wanted to be able to use install wp-cli on a windows pc, wondering if you have a tutorial for this (from scratch) also, you mentioned that you have a test site and then move the files into the live site, could you please explain more on this like a tutorial. I am sorry if its a bit too much to ask for, but will be great to have this setup, even that I am not a developer, i would like to see if plugin updates do not brake my site before doing it to the live site, which has happened to me a few times already. Thank you.

      Jul 28, 2017
    • Mike England Reply

      Thanks for this podcast. It was pretty awesome to hear this plugin is capable of. I manage quite a few WordPress sites so this would be awesome to make those simple steps much faster.

      Aug 24, 2017
    • Pedro de Carvalho Reply

      hi, dark green background with grey text makes the transcript really impossible to read.

      Dec 27, 2017
      • Dustin Hartzler Reply

        Sorry about that! I’ve been making some updates and had some weird CSS causing the coloring issue. I’ve changed it back to the normal gray background and it should be much easier to view.

        Dec 28, 2017

Leave a Reply