Podcast Episode

275 – Controlling WordPress via the Command Line

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.

WP Review allows you to create reviews! Choose from Stars, Percentages or Points for review scores. Supports Retina Display, WPMU & Unlimited Color Schemes.

Listener Feedback

My friend Brian wrote in from Physician Assistant Exam Review and wondered how he could easily password protect several pages on his WordPress site with a simple password.

It turns out that you can password protect a parent page and it will protect all child pages as well.

Controlling WordPress via the Command Line

Your WordPress site can be controlled on the command line. It’s a tool that allows you to maintain WordPress without the standard GUI

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

Examples

# Install Son of Clippy
$ wp plugin install son-of-clippy --activate

# Upgrade Yoast SEO
$ wp plugin update wordpress-seo

# Remove TwentyThirteen
$ wp theme delete twentythirteen
# Export only posts from 2016
$ wp export --start_date=2016-01-01 --post_type=post

# Import WXR files
$ wp import my-wxr-file.xml
# Dump the database
$ wp db export my-backup.sql

# Import that database file
$ wp db import my-backup.sql

# Optimize the database
$ wp db optimize

# Repair the database
$ wp db repair
# Update production URLs for staging
$ wp search-replace example.dev example.com

# Replace all instances of "foo" with "bar" *only* in wp_options
$ wp search-replace foo bar wp_options

Call To Action

Take a look at WP CLI and see if it’s something you could add to your workflow to speed things up.

Full Transcript

Business Transcription is provided by GMR Transcription.

On today's episode we are going to talk about how we can control WordPress via the Command Line right here on Your Website Engineer Podcast, Episode No. 275.

Hello, everybody, welcome back to another episode of Your Website Engineer Podcast. My name is Dustin Hartzler and today we are going to be talking about the Command Line but, of course, I'm going to start off this episode with a few announcements and I've got a plugin that I want to share with you today. The first one is – the first piece of news is that WordPress 4.5 Beta 2 is now available. Of course the – since it is in Beta, the software is still under development so don’t put this on any of your live sites but you definitely want to check it out and play around with it, especially if you're developing deems or plugins or you just want to test some of your client's sites before you actually launch 4.5 in a month or so.

So, some of the cool things that have been added in 4.5 is they've added a horizontal rule as part of a shortcut inside the tiny MCE Editor and there's also a dismissible paste as text notice when you're using the tiny MCE Editor. There's another thing called selective refresh and this is support enabled for core themes and taglines, which allows you to shift click focus controls on PHP filters to apply in the preview. So, that's pretty cool. That's all in the customizer, which looks pretty cool, and then t

They fixed over – made over 100 changes since last week and they kind of signed off this blogpost, which I'll link you to it. It will be in the show notes but it is for the – there is a haiku of it in every one of these blogposts. It's like peer pressure time testing. All the cool kids do it. Help find all the bugs, so that is something that you may be interested in this week, so go on over and check it out. You can find that over at the wordpress.org site in the new section or you can just find the link to it in the show notes for Episode No. 275.

Another thing that I want to share with you this week – this is a bit old because it came out a few, maybe weeks ago and it is that the Woo Commerce and Stripe payment gateway is now available for free. This was an extension that used to be about $50.00 and they've paired up with the stripe company and it basically – oh, I guess it was – the previous version was priced at $79.00 for one license or for five licenses it was $99.00 and for up to 25 sets, it was up to $199.00. So, Woo Commerce and Stripe is using a new platform called Atlas, which allows foreign companies to incorporate as a U.S. company in Delaware and set up bank accounts. So, global payments are now going to be able to be accepted with Stripe, which is pretty cool. So, it basically now gives the ability for anyone who wants to use Stripe on your website, you can use that free plugin now on your WordPress site. So, you can find out more over at WooThemes.com. All right, and lastly, the WordPress plugin directory is getting a refresh, very similar to how the theme panel and the theme area got refreshed a few years ago. Now all those changes are coming to the theme area or to the plugin area, so we're going to see some changes coming.

I think the set date for that whole project is going to be done by July of 2016. So, we should see that in the near future. All right, moving on too is there is a plugin for that with more than 42,000 plugins in the WordPress repository. It's really hard to find one that may be perfect for your needs and so today I wanted to highlight a plugin called WP Review. WP Review gives you the ability to create reviews for your products or for your blogposts or you can use those pretty much for anything. You can choose stars or percentages or points for review.

It's built for retina display, unlimited color schemes are built right in, and you can use this for products. You can use this as, you know, rating system for – maybe you are doing a review type website and you are reviewing different products or services. You can add now different star ratings for the different things. So, this is a free plugin. All the WordPress repository –it's got more than 80,000 downloads and it is compatible right with your WordPress site running version 4.4.2. All right, and I got a little bit of feedback from one of my friends, Brian, over from physicianassistantexamreview.com.

He wanted to know how he could easily password-protect a page and then multiple other pages using the same password. I thought about it for a while and then he ended up emailing me and sent me the information that – I just didn't realize this, so I wanted to pass this tidbit on to you as well. But basically if you create a parent page and then you list all of your – you make a bunch of child pages underneath the parent page and the parent page is password protected, then once somebody logs in with that password, then all the pages underneath it will be password-protected but you can only access them with that same password.

So, you only need one password for all of the pages, which works really, really well. So, there may be some reason that you would want to do this. Maybe you have, like in Brian's case, he has a course that he's selling and he didn't want to get into all the technical difficulties of setting up a membership course and all that good stuff. So, he basically said once somebody pays, he sends them the password and then they go to a certain URL with the password and from there, they just log in once and then they can see ALL the content for all the pages.

If they happen to bookmark a page, four or five pages deep, and they come back the next time, they'll ask for the password there and then they get directly to that page, which is pretty cool. So, I thought that was worth noting and passing along so that is the listener feedback of the week. All right, today we are going to talk about WordPress WP-CLI or it's just called WP-CLI, and it's basically a neat way to command and control WordPress without using the Editor – without using the WordPress Dashboard, which I think is kind of neat.

Now, it does take a unix-like environment, so that's either OSX or Linux. There is limited Windows support and for Windows environments but I just wanted to point this out that it's really, really neat. I've been using it over the last several months but I saw a presentation at Word Camp Dayton this weekend and it was just like oh, I think I should just talk about it and explain it to let you know that there are options out there when it comes to controlling WordPress websites. Using the command line interfaces a lot quicker and sometimes it's really, really good for doing multiple things very, very quickly.

So, let's kind of dive in first to talk a little bit about what it is and how it works. Then we'll go ahead and I'll kind of walk through some of the commands and some of the things that you can do. Obviously, it's not going to do due diligence doing an all audio podcast for this but I think that just talking it may peak your interest level and your intrigue and maybe you'll want to go ahead and check it out. Now, you can find out all the information and a lot more information over at wp-cli.org. But the first thing is what is it? It's basically just WordPress on the command line.

You just open up your terminal and you can start typing and it just kind of works. It's an extensible tool for installing and maintaining WordPress. You can do a lot of really – I mean, basically, anything that you can do within the WordPress dashboard, you can do that with WP-CLI. You don't need the standard gooey; you don't need to, you know, you don’t have to be able to – you know, you don't have to wait for a page reload to do all those things. Like, it just works really, really fast and so you would put in your commands and we'll talk about those just in a second. It's far easier to use, by using WordPress CLI it is far easier to perform bulk operations. It's better to – you get better visibility for that WordPress internals.

So, if you want to know more about the Cron – what's happening with the Cron jobs – if you want to see what the caches are – if you want to see the transients that are taking place or where the transients are getting stored, you can see all that right from the command line. This is pretty helpful because now you can do everything via the command line and you don't have to have two interfaces because if you have the WordPress dashboard, you have that open and you can get to a lot of information there. But if you want even more information you have to also open another tab and get into the database and then try to look at all the database information.

But with WP-CLI you can actually get a lot of that information that's in the database all from the command line. Another really cool thing is it's not subject to timeouts the way browsers would be, so sometimes when you're doing an operation, you know, maybe you’re trying to import 5,000 new products or you're trying to add 5,000 new blogposts or something. You're trying to do something that would be labor-intensive for a browser to do; it's normally going to time out. But with CLI, it's not subject to timeouts and it will just run and run and run and process in the background, which is really pretty cool. It's easier on file system operations.

You're able to script things if you need to do something very, very regularly. This is part of the presentation that I didn't quite understand but if you had to do something more than once, you could definitely write a script to do it over and over again. So, maybe it's like you install WordPress and then the script that you run is to download these four plugins and then to remove these two default plugins and maybe you download the 2016 theme and you activate it. Like, you could set up all these steps if you wanted to for multiple websites and you could just go ahead and run a script and then you could continue to tweak that script.

Maybe if you're a WordPress developer and you always set up your theme with the, you know, you're starting a brand new website and you set it up with the same theme, the same framework, the same this, the same that, you write a script, and you can go ahead and do that instantly just as you download it and get everything set up, which is pretty, pretty cool. So, you can use WP-CLI to deploy WordPress Core update, which is pretty cool. You just go in, you navigate into your – you can do this either locally and that's where I've spend most of my time with WP-CLI.

I can actually just use terminal to navigate right to the root folder of my desktop server installation. I can get in there and I can say WP core update and it will go ahead and update core, which is pretty cool. You can install, activate, update, delete the themes and plugins, which is pretty nice. You can do all kinds of troubleshooting with the WP underscore Cron, transients cache – you can do maintenance routines, you can schedule things right in there, you can do data migrations and transformations. It's really pretty cool.

So, let's look at some of these commands that you can do. So, the first one is built in, like there's lots that are built in. So, maybe you wanted to install the plugin Son of Clippy. This is just kind of a funny plugin that, you know, that's out there that just kind of made – it's a joke basically and it brings Clippy who was very famous for helping people use Microsoft Word and you can add this as a plugin to your WordPress site if you really wanted to. But to do this, what you do is you go to the terminal, you navigate to the main folder for your website and then you would type wp plugin install and then son-of-clippy and you're basically putting whatever permalink is for the plugin.

So, if it’s like the Yoast SEO plugin it would be WordPress-SEO. If it's the As Heard On plugin, it would be As-Heard-On-plugin. Then if you type after that, so you've got wp plugin install son-of-clippy. Then if you type dash dash activate (--activate), it's going to automatically activate that right as you go. You can also do WP theme delete 2013 and, of course, if you wanted to do this in a script, you'd write a script to automatically do this so that you wouldn't have to manually do all of these things.

So, you can do this right from the command line. Something else that you can do is maybe you want to add a new – maybe you want to add a new user to your website. You could say WP user create dustin, and then put Dustin's email address, and then you can say dash dash rule equals (--rule=) admin, or rule equals editor, or things like that. You can just do that and it makes it very, very simple. If you wanted to change the rule later, you could say WP user set rule dustin admin and you can just go ahead and do that. That's all built into the WP Command Line. You can also do, which this is a pretty good functionality piece, because this is stuff that normally times out on you regularly.

But maybe you want to export a post or maybe you want to export all the content from your database. Basically, like you would be doing the same thing as by using the tools and export something that's sitting in your WordPress dashboard. You would go WPexport and then you could say dash dash max file size equals five (--maxfilesize=5). That will export all of your files and post types in five megabyte chunks so that you're not making them too big and they won't be able to be imported. That's pretty cool.

Or you could say – if you wanted to export posts only from 2016 you could say WPexport start day, or it's dash, dash start underscore date equals 2016 dash zero one dash zero one (--start_date=2016-01-01) and then you can say dash dash post type equals post (--posttype=post). If you wanted to just do posts from that time period, which is pretty, pretty cool. Then if you want to import your files, you can do all that via the Command Line. WPimport and then the name of the file dot xml. You can import everything right to your WordPress database and get it set up on your site.

Again, this is stuff that times out very, very easily and we always have problems with this at WordPress.com when somebody is trying to import 50,000 posts and all these images and everything else. Like, most of the time we have to have our engineers do this with the Command Line and they can just do it very, very quickly and very, very easily. You can also do stuff with the database so maybe you need a database dump and you can say WPDB export and then you give the name of the file, which is pretty cool. Or maybe you want to import that database.

You would say WPDB import and then the name of the file. It’s really cool because you don't actually have to give it any of your database credentials. It actually will go and look in the WP-config file and give you the information that will pull it up so you don't actually need to go into PHP my admin or whatever type of user interface you're using to access your database. You can also optimize the database table, so you know a lot of times when we add new plugins, if we add new things, like there's a lot of – sometimes when we delete things; we're leaving extra spaces in the database when it removes different things. That's what optimizing is. It’s getting rid of stuff. It's kind of cleaning things up and it's making things work much quicker. You can also repair the database by using WPDB repair.

Another thing that you can do and this is really, really cool. I learned this in the Word Camp presentation and I will probably be using this for, you know, in some way, shape, or form. I'm definitely going to be trying to use this in the next week or two. You can basically do a search and replace within your WordPress database and it handles things with serialized data. So, what that means is basically if you ever try to use your – if you've ever moved your database and you've done a find and replace to change your website URL from like YWE.dev, which would be my development site to yourwebsiteengineer.com.

Try to import that, you're going to break all of your widgets. You're going to have to reconfigure all of those, all your menus, and stuff. You're going to have to reconfigure those because all that information is stored in what's called a serialized data. So, it's basically taking them – it looks at YWE.dev and that's seven characters, counting all of them – counting the period as well. So, when you replace it with yourwebsiteengineer.com, that's many more characters than the seven and so if you don't do a serialized data replace, then what happens is everything breaks that's based on those and all widgets and menus and stuff like that are based on serialized data.

So, if you wanted to, you could go WPsearch-replaceexample.devexample.com and you could just go ahead and if that's your development site – you know, for me if I was to move my database or something like, I would look for WYE.dev and then I would replace it with yourwebsiteengineer.com. You can also do replace all instances of Foo with bar in just the WP options staple if you want, so you would do WPsearch-replaceFoobarWP_options and that will replace all that text to right within the WP options staple if you really wanted to focus on one specific area, which would be really pretty neat.

So, there's tons more commands. If you head on over to WP-CLI.org/commands, you're going to find out all kinds of different things. I primarily use it to update plugins and that's probably the main thing that I use but you could also use it to manage comments. You can update core. You can – let's see – I'm looking here. You can export – we already talked about. You can import – we talked about that. You can manage attachments if you wanted to. You could list or create, assign, and delete menus. You can manage your options.

You can manage your plugins. You can manage post types. You can change your rewrite rules. You can do search and replace like we said. You can manage your sidebars. You can do site-wide operations. You can do stuff that's super admin access only. You can manage terms. You can manage users. You can manage themes. You can manage sidebar widgets. There's tons and tons of stuff that you can do just right out of the box with WP-CLI. Now, you can also do some cool things like write your own scripts. This is kind of what that whole presentation was all about. It was creating your own scripts.

I'm not going to go over this in a podcast episode because there was just too much – too many visuals to really look at. There was like a lot of really cool things you could do. You could add this into your own plugin so that people could manage – I mean, all of this stuff that we talked about is built right into WordPress core but your plugins and your themes – they don't have this capability until the programmer has developed it and added those to the plugin, or whatnot. So, that is pretty much what I wanted to share with you today.

WP-CLI is kind of a more advanced tool and it is something that you really have to kind of play with and look around and just kind of explore. I know that I've spent the last couple of years thinking oh, I should really understand and learn the WP-CLI and just get a better understanding of how it works. It is taking a little bit of time and I can see some true value, especially if you are doing a lot of saving things in version control like I am. My whole talk this weekend was on how I do things locally and then I push those changes live via Github and then a service called deployed@h2.

I'm planning on redoing the – I recorded the version and it didn't turn out the greatest because of the visuals and the overall classroom size that we were in. So, I'm planning on redoing that and just kind of highlighting my process on how I can actually make all – do all the edits that need right in the command line, so I can actually go in with the command line. I can change files in the command line. I can do all the stuff in the command line and then I can save it together and I can push it. I can deploy changes to my website without ever looking at the WordPress dashboard, without ever opening up my FTP Editor and things like that. So, there's lots of really cool things in this process that I've worked on for the last year or so just trying to figure out the best way, the most ideal way for me, personally, to do work and to push changes live.

I’m going to share that also in an upcoming episode as well. So, that's basically what I wanted to share with you today. All about WP-CLI. We can do some awesome things with it and it can help you manage things very, very quickly. I can see if you wanted to update lots of plugins and maybe you have multiple sites, you open up multiple ins of the terminal and you can say WP plugin list and it will show you all the plugins and which ones need updated. You can quickly run through all those commands and you can update all of those plugins very, very simply.

We do a lot of things and so I urge you to go out and check out WP-CLI and see if it's a fit for you and your development style. That's all I've got for this week. Take care.

Leave a Reply