{"id":9180,"date":"2022-05-31T13:17:00","date_gmt":"2022-05-31T11:17:00","guid":{"rendered":"https:\/\/zeni.cz\/?p=9180"},"modified":"2024-07-10T10:55:09","modified_gmt":"2024-07-10T08:55:09","slug":"wp-cli-manage-wordpress-websites-better-and-faster","status":"publish","type":"post","link":"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/wordpress-development\/wp-cli-manage-wordpress-websites-better-and-faster\/","title":{"rendered":"WP-CLI \u2013 Manage WordPress Websites Better and Faster"},"content":{"rendered":"<div class=\"gb-container gb-container-977dbebd\">\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong><strong>WP-CLI allows you to manage websites using simple commands without needing to navigate trough many pages in WordPress administration. Whether you are a developer or administrator, WP-CLI can save you a lot of time and even get inaccessible website back to life.<\/strong><\/strong><\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-de57c996\">\n\n<h2 class=\"wp-block-heading\">What Is WP-CLI? <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">WP-CLI is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Command-line_interface\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">command line interface<\/a> for WordPress. That means that it defines commands, that you can execute on your server, that you can use to performs common actions. Tasks like <strong>managing posts, users, plugins, themes<\/strong> and<strong> website options<\/strong> can be <strong>achieved in<\/strong> the <strong>command<\/strong> line. We will have a look at some<strong> practical use cases<\/strong> in this article.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-05195e63\">\n\n<h2 class=\"wp-block-heading\">How Can You Use WP-CLI? <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">WP-CLI <strong>is not included<\/strong> in <strong>WordPress<\/strong> installation <strong>by default<\/strong>. Luckily a lot of WordPress hosting providers have already adopted this tool and made it accessible to its users. You can find out if this feature is included<strong> in details<\/strong> of your <strong>hosting plan<\/strong> or you can <strong>contact<\/strong> your <strong>hosting provider<\/strong>. WP-CLI is also <strong>included in<\/strong><a href=\"https:\/\/roots.io\/trellis\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"> Trellis<\/a> that we use to manage servers for our clients at <a href=\"https:\/\/zeni.cz\/en\/\">Zeni<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">If you find out that you <strong>do not have access to WP-CLI<\/strong> on your server, &nbsp;assuming you have <strong>SSH access to your server<\/strong> or you want play with <strong>WP-CLI locally <\/strong>on you computer, you can get it working using just <strong>few commands<\/strong>. On the <strong>OS X<\/strong> and <strong>Linux<\/strong> this can get you to fully working WP-CLI in no time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -O https:\/\/raw.githubusercontent.com\/wp-cli\/builds\/gh-pages\/phar\/wp-cli.phar\nchmod +x wp-cli.phar\nsudo mv wp-cli.phar \/usr\/local\/bin\/wp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">If you use <strong>Windows<\/strong>, or you are interested in <strong>different installing methods<\/strong> you can have a look at <a href=\"https:\/\/make.wordpress.org\/cli\/handbook\/guides\/installing\/#installing-on-windows\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">WordPress Documentation<\/a>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-95aff7c6\">\n\n<h2 class=\"wp-block-heading\">Speed up Time Consuming Tasks With WP-CLI <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">There are <strong>many administrative tasks<\/strong> in <strong>WordPress<\/strong>, that require you to perform <strong>multiple repetitive actions<\/strong> to get you to your goal. That might include a <strong>lot of page loads <\/strong>and <strong>clicks<\/strong> and take you <strong>couple of minutes<\/strong> in some case maybe hours. Thanks to <strong>WP-CLI<\/strong>, you can perform these <strong>tasks lot of easier<\/strong>. Especially<strong> bulk actions<\/strong> like for example managing multiple plugins or media can be <strong>speed up with WP-CLI<\/strong>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-54193db4\">\n\n<h3 class=\"wp-block-heading\">Install Multiple Plugins at Once <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Are you just starting with the fresh site and you want to <strong>install multiple plugins<\/strong> that you regularly use or you just received request to install few new plugins? <strong>Using WP-CLI<\/strong> this will only take you to write <strong>single command<\/strong>. Following command installs 2 plugins and activates them thanks to <code>activate <\/code>flag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp plugin install bbpress classic-editor --activate<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">This command also allows for <strong>passing zip archives<\/strong> with the plugins, for example here below. <strong>Local paths<\/strong> are supported too:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp plugin install https:\/\/example.com\/plugin.zip<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Using <code>wp plugin<\/code> command can <strong>achieve much more<\/strong>, for example <strong>updating all the plugins<\/strong> on your website. I highly recommend to have a detailed look at the <strong>documentation<\/strong> of <a href=\"https:\/\/developer.wordpress.org\/cli\/commands\/plugin\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">wp plugin command<\/a>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-e54702ce\">\n\n<h3 class=\"wp-block-heading\">Bulk Import Media <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">You can easily <strong>import multiple media<\/strong> using <strong>simple command<\/strong>. Here we are going to <strong>import <\/strong>all<strong> JPG filesin<\/strong> the <strong>user<\/strong> pictures<strong> folder<\/strong>. This will <strong>copy<\/strong> the files<strong> from<\/strong> the <strong>original<\/strong> folder and automatically <strong>register them as attachments<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp media import ~\/Pictures\/**\\\/*.jpg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">&nbsp;If you are interested in <strong>more media oriented commands<\/strong> you can have a look to command <a href=\"https:\/\/developer.wordpress.org\/cli\/commands\/media\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">wp media<\/a>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-9617cbe3\">\n\n<h3 class=\"wp-block-heading\">Managing Users <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><code>wp user <\/code>&nbsp;command helps with all of <strong>tasks related to managing users<\/strong>. Here is an example for <strong>creating user<\/strong> <code>daniel<\/code> with mail address <code>daniel@zeni.cz<\/code> and role administrator. I have used this exact command countless times when there was <strong>new project<\/strong> that i have worked on:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp user create daniel daniel@zeni.cz --role=administrator<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>Managing user passwords, roles, capabilities<\/strong> and <strong>more<\/strong> is very convenient thanks to WP-CLI. If you are interested in user management, here you can find more<a href=\"http:\/\/\" target=\"_blank\" rel=\"noreferrer noopener\"> <\/a>about command <a href=\"https:\/\/developer.wordpress.org\/cli\/commands\/user\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">wp user<\/a>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-e70cf75e\">\n\n<h2 class=\"wp-block-heading\">WP-CLI and Dealing With Technical Issues <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>WP-CLI<\/strong> is great servant when you are trying to <strong>deal with technical issues<\/strong> as we are going to see in next few lines.&nbsp;<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-0a183e06\">\n\n<h3 class=\"wp-block-heading\">Debugging Theme and Plugin Issues <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Imagine you have just activated faulty plugin, which causes <strong>WordPress administration to be inaccessible<\/strong>. <strong>WP-CLI can rescue you<\/strong> in this situation <strong>using single command<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp plugin deactivate faulty-plugin<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Similarly you can <strong>activate different theme<\/strong> using WP-CLI if you believe that current theme is causing trouble. Following <strong>example activates chosen theme<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp theme activate different-theme<\/code><\/pre>\n\n<\/div>\n\n<div class=\"gb-container gb-container-8e6f56ed\">\n\n<h3 class=\"wp-block-heading\">Manipulating Options <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Do you need to <strong>change values of some options<\/strong>? This can be useful when you want to <strong>debug an issue<\/strong> or when changing some options is not directly possible in the administration. In this example we are going to <strong>change current admin email<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp option update admin_email admin@zeni.cz<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>You can <strong>find more about managing options<\/strong> in the documentation of <a href=\"https:\/\/developer.wordpress.org\/cli\/commands\/option\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">wp option command.<\/a><\/strong><\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-6a6f4121\">\n\n<h3 class=\"wp-block-heading\">Managing Database <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">WP-CLI is also great for <strong>managing database<\/strong>. You can easily <strong>create backup<\/strong> of current database and <strong>save it to current user directory<\/strong> using following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp db export ~\/backup.sql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">This is extremely <strong>useful<\/strong> when you want to <strong>create export of the database<\/strong> for use on staging or local development environment or to <strong>create a backup<\/strong> before performing some <strong>risky action<\/strong> like updating database structure because of some plugin. If anything<strong> goes wrong<\/strong>, we can <strong>easilygo back<\/strong> using this<strong> command<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp db import ~\/backup.sql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">If you are planning to experiment we <strong>highly recommend to not to do it on<\/strong> your customer facing <strong>production environment<\/strong>. That is what <strong>staging server can be used<\/strong> for. You can <strong>learn what staging<\/strong> environment <strong>is <\/strong>in our article <a href=\"https:\/\/zeni.cz\/en\/blog\/seven-reasons-to-use-staging-environment-for-your-project-on-wordpress\/\">Seven reasons to use staging environment for your project on WordPress<\/a>. If you are interested in <strong>creating staging environment<\/strong>, you can learn about this in our article &nbsp;<a href=\"https:\/\/zeni.cz\/en\/blog\/how-to-create-staging-enviroment\/\">How to create staging environment?<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Another useful feature is ability to<strong> quickly enter <\/strong>your database command line interface<strong> to execute SQL <\/strong>using following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp db cli<\/code><\/pre>\n\n<\/div>\n\n<div class=\"gb-container gb-container-4251597c\">\n\n<h3 class=\"wp-block-heading\">Replace All the Occurrences of a Value <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Imagine<strong> site moved to different URL<\/strong>, or you want to <strong>use<\/strong> your<strong> production database on<\/strong> the <strong>staging <\/strong>server. <strong>After <\/strong>you<strong> import database<\/strong> to new server you are quickly going to find out that <strong>some of the links are not working<\/strong> and that <strong>media files are not visible<\/strong>. That is because unfortunately WordPress uses absolute URLs in many cases. To <strong>fix<\/strong> this issue <strong>WP-CLI <\/strong>have very <strong>useful command<\/strong> known as wp search-replace. With this command <strong>you can replace<\/strong> all the <strong>old URLs<\/strong> with<strong> new ones <\/strong>very simple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp search-replace https:\/\/old-website.old https:\/\/new-website.new --dry-run<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">The <code>dry-run<\/code> flag on the end is very useful here because it <strong>runs <\/strong>the <strong>entire search\/replace operation<\/strong> with showing the report, but no changes are saved into the database. It is definitely a good idea to run this command in dry run mode first and after reviewing the report running it without dry run mode.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-1412849c\">\n\n<h2 class=\"wp-block-heading\">Generate Dummy Content With WP-CLI <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">This one is <strong>very useful for developers<\/strong>, who need some <strong>mocked content<\/strong> on the site for development purposes. For example, you can generate <strong>tens of posts, users, terms<\/strong> and <strong>comments <\/strong>using <strong>just few commands<\/strong>. Here are few examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp user generate --role=subscriber --count=50\nwp post generate --count=200 --post_type=post\nwp post generate --count=20 --post_type=page\nwp term generate category --count=10\nwp comment generate --count=10 --post_id=123<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>This will give you<\/strong> a site with 50 new users, 200 posts, 20 pages, 10 categories and our selected post already have 10 comments on it. This <strong>simulates active sit<\/strong>e, that have been around for some time and we did not need to do anything manually. We can start working on <strong>developing <\/strong>WordPress theme or plugin<strong> more easily<\/strong>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-fb600673\">\n\n<h2 class=\"wp-block-heading\">There Is a Lot More to Achieve <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">We have introduced <strong>many use cases<\/strong> of the<strong> WP-CLI<\/strong> in this article, but that is far from all that is possible using this tool. To became familiar with all the possibilities, there is<strong> no better source than<\/strong><a href=\"https:\/\/developer.wordpress.org\/cli\/commands\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"> WordPress documentation<\/a>. I hope that WP-CLI will help you to become better developer or administrator.<\/p>\n\n<\/div>","protected":false},"excerpt":{"rendered":"<p>WP-CLI allows you to manage websites using simple commands without needing to navigate trough many pages in WordPress admininstration. Whether you are a developer or administrator, WP-CLI can save you a lot of time and even get inaccessible website back to life.<\/p>\n","protected":false},"author":8,"featured_media":7464,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","footnotes":""},"categories":[312],"tags":[178,52,211],"class_list":["post-9180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress-development","tag-navody-a-tipy-3-en","tag-zeni-en","tag-zeni-team-3","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"author_meta":{"display_name":"Bruno Dirb\u00e1k","author_link":"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/author\/brunhylda\/"},"featured_img":"https:\/\/zeni.cz\/app\/uploads\/2022\/05\/EN-300x200.png","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/.\/wordpress-development\/\" class=\"advgb-post-tax-term\">WordPress Development<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">WordPress Development<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/.\/wordpress-development\/\" class=\"advgb-post-tax-term\">N\u00e1vody a tipy<\/a>","<a href=\"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/.\/wordpress-development\/\" class=\"advgb-post-tax-term\">zeni<\/a>","<a href=\"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/.\/wordpress-development\/\" class=\"advgb-post-tax-term\">zeni team<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">N\u00e1vody a tipy<\/span>","<span class=\"advgb-post-tax-term\">zeni<\/span>","<span class=\"advgb-post-tax-term\">zeni team<\/span>"]}},"comment_count":"0","relative_dates":{"created":"Posted 4 years ago","modified":"Updated 2 years ago"},"absolute_dates":{"created":"Posted on 31.5.2022","modified":"Updated on 10.7.2024"},"absolute_dates_time":{"created":"Posted on 31.5.2022 13:17","modified":"Updated on 10.7.2024 10:55"},"featured_img_caption":"WP-CLI \u2013 Manage WordPress Websites Better and Faster","series_order":"","_links":{"self":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts\/9180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/comments?post=9180"}],"version-history":[{"count":5,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts\/9180\/revisions"}],"predecessor-version":[{"id":12401,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts\/9180\/revisions\/12401"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/media\/7464"}],"wp:attachment":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/media?parent=9180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/categories?post=9180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/tags?post=9180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}