{"id":14565,"date":"2017-02-09T13:39:21","date_gmt":"2017-02-09T12:39:21","guid":{"rendered":"https:\/\/www.oderland.se\/support\/artikel\/hur-anvander-jag-wp-cli\/"},"modified":"2021-06-21T12:40:55","modified_gmt":"2021-06-21T11:40:55","slug":"how-do-i-use-wp-cli","status":"publish","type":"ht_kb","link":"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-use-wp-cli\/","title":{"rendered":"How do I use WP-CLI?"},"content":{"rendered":"\n<p>WP-CLI is available on all our <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.com\/web-hosting\/\" target=\"_blank\">web hosting accounts<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.com\/servers\/\" target=\"_blank\">Managed Servers<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.com\/solutions\/agency\/\" target=\"_blank\">Agency<\/a> services. It lets you manage WordPress installations via terminal\/SSH.<\/p>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-info\">To be able to use WP-CLI, you must first access your account via SSH. We&#8217;ve written instructions <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-connect-to-my-web-hosting-account-using-ssh\/\" target=\"_blank\">here<\/a> describing how you get started with SSH.<\/p>\n\n\n\n<p>Once you&#8217;ve set everything up so you can access your account via SSH, you can continue reading this guide where we&#8217;ll go through a number of useful WP-CLI features. You may also use <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-connect-to-my-web-hosting-account-using-ssh\/#via-cpanel\" target=\"_blank\">the terminal available via cPanel<\/a> for this.<\/p>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-info\">WP-CLI has many more features than we can cover in this article. You find them all on its <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.wordpress.org\/cli\/commands\/\" target=\"_blank\">official website<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">To be able to use WP-CLI<\/h2>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-alert\">To be able to use WP-CLI, you must navigate to the <a href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/where-can-i-find-the-files-for-my-website\/\" target=\"_blank\" rel=\"noreferrer noopener\">document root<\/a> of the WordPress installation. If the installation is located in the public_html folder, you type <code>cd public_html<\/code> directly after logging on to you account via SSH to enter that folder. If the installation resides in another folder, you will need to type <code>cd path\/to\/your\/installation<\/code> instead (using the correct path).<br>You can use the command <code>pwd<\/code> to see the path to the folder where you are currently, and the command <code>ls -la<\/code> to list the contents of your current folder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic commands<\/h2>\n\n\n\n<p>Once you&#8217;ve navigated to the document root of your WordPress installation (<a href=\"#to-be-able-to-use-wp-cli\">as described above<\/a>), use any of the following commands to get information about the installation:<\/p>\n\n\n\n<p>See the version of WordPress that is being used:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp core version<\/pre>\n\n\n\n<p>Check if there are any updates available for the WordPress core:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp core check-update<\/pre>\n\n\n\n<p>See status for all installed plugins, which ones are activated, and if there are any available updates:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp plugin status<\/pre>\n\n\n\n<p>In the same manner as for plugins, you can check the status for all installed themes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp theme status<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Update WordPress core, plugins, and themes<\/h2>\n\n\n\n<p>After navigating to the document root of your WordPress installation (<a href=\"#to-be-able-to-use-wp-cli\">as described above<\/a>), you can use WP-CLI to update the WordPress core to the latest version. To do this, run these commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp core update\nwp core update-db<\/pre>\n\n\n\n<p>You can also use WP-CLI to update installed plugins to the latest version. If you want to update all plugins on your WordPress installation at once, use this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp plugin update --all<\/pre>\n\n\n\n<p>If you only want to update a specific plugin, instead use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp plugin update akismet<\/pre>\n\n\n\n<p>In this example, the plugin Akismet will be updated, swap out <code>akismet<\/code> to match the name of the plugin you want to update.<\/p>\n\n\n\n<p>In the same manner, you can update themes. Using this command, you can update all currently installed themes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp theme update --all<\/pre>\n\n\n\n<p>If you want to update a specific theme, run the following (instead of using <code>--all<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp theme update twentyeleven<\/pre>\n\n\n\n<p>Remember to swap out <code>twentyeleven<\/code> in the example for the name of the theme you wish to update.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Replace the site url used by WordPress<\/h2>\n\n\n\n<p>To change the domain a WordPress installation uses, <em>e.g.<\/em> swapping from test1.se to test2.se, can be quite a laborious process since the original domain (test1.se) will exist in a number of places all over the database used by the installation. To ensure that WordPress works properly, they all need to be replaced with the new domain, a task that can require vast amounts of time, was the job to be done manually. However, WP-CLI has a solution for this, making the process way faster!<\/p>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-danger\">Do not forget to navigate to the document root of the WordPress installation where you want to change site url. Do this before running the commands below. We describe <a href=\"#to-be-able-to-use-wp-cli\">above<\/a> how you navigate to the correct location.<\/p>\n\n\n\n<p>WP-CLI has a so called &#8220;search and replace&#8221; feature, which can go through your database, looking for the original domain (test1.se) and replacing them with the new domain (test2.se). To use it, simply run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp search-replace ':\/\/test1.se' ':\/\/test2.se'<\/pre>\n\n\n\n<p>If you execute this command, WP-CLI will replace all occurrences of test1.se in the database with test2.se. If you add <code>--dry-run<\/code> at the end of the command, the feature will be run but not perform any changes. You will, however, get a report of the changes that could be done:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp search-replace ':\/\/test1.se' ':\/\/test2.se' --dry-run<\/pre>\n\n\n\n<p>Remember to swap out test1.se and test2.se for the correct domains for your case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Change from http to https in URLs<\/h2>\n\n\n\n<p>If you recently switched from http to https on your website, but browsers still warn that the website is not secure due to mixed content, this may be because some resources (such as images) still load via http.<\/p>\n\n\n\n<p>You can then use the &#8220;search and replace&#8221; feature described above to fix this. Follow the instructions <a href=\"#replace-the-site-url-used-by-wordpress\">above<\/a>, but instead of replacing <code>:\/\/test1.se<\/code> with <code>:\/\/test2.se<\/code>, make sure that you search for <code>http:\/\/test2.se<\/code> and replace it with <code>http<strong>s<\/strong>:\/\/test2.se<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-alert\">Please note that the method described above will not correct links in files belonging to the website, just the database will be fixed. You may have to edit files on the site as well, which can be done <em>e.g.<\/em> via the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-use-the-file-manager-in-cpanel\/\" data-type=\"ht_kb\" data-id=\"4886\" target=\"_blank\">File Manager<\/a> accessible via cPanel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create an administrator account<\/h2>\n\n\n\n<p>Sometimes, it may happen that WordPress erases the permissions for an administrator account. When this has happened, you&#8217;ll see an error message when you try to log in using that admin account, instead of reaching wp-admin.<\/p>\n\n\n\n<p>Once you&#8217;ve navigated to the document root of the WordPress installation (<a href=\"#to-be-able-to-use-wp-cli\">see above<\/a>), you can create a new admin user via WP-CLI using this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wp user create USERNAME EMAILADDRESS --role=administrator<\/pre>\n\n\n\n<p>Swap out <code>USERNAME<\/code> in the command above with the username you want the new admin user to have, and swap <code>EMAILADDRESS<\/code> to the email address for the new user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use WP-CLI in cronjobs<\/h2>\n\n\n\n<p>If you want to use WP-CLI in <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-set-up-cron-jobs\/\" data-type=\"ht_kb\" data-id=\"9393\" target=\"_blank\">cronjobs<\/a> on your web hosting account, you need to call the command this way<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/usr\/local\/bin\/php \/usr\/local\/bin\/wp X<\/pre>\n\n\n\n<p>instead of just typing <code>wp<\/code> in the beginning. This is necessary because cron runs in another environment than the one you end up in when you connect to the account via SSH. Replace <code>X<\/code> in the command above with the rest of the command that should follow <code>wp<\/code> if you were to run the WP-CLI command via SSH.<\/p>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-info\">If you want to use a specific PHP version in the cron job, enter <code>\/opt\/alt\/php73\/usr\/bin\/php<\/code> instead of <code>\/usr\/local\/bin\/php<\/code>.<br><br>You can <a rel=\"noreferrer noopener\" href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-manage-php\/#which-php-versions-are-available\" target=\"_blank\">choose what PHP version<\/a> to run by switching out 73 in the command above for another of the available PHP versions (<em>e.g.<\/em> 70 if you want to use PHP 7.0).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WP-CLI is available on all our web hosting accounts, Managed Servers and Agency services. It lets you manage WordPress installations via terminal\/SSH. Once you&#8217;ve set everything up so you can access your account via SSH, you can continue reading this guide where we&#8217;ll go through a number of useful WP-CLI&#8230;<\/p>\n","protected":false},"author":12,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"ht-kb-category":[213],"ht-kb-tag":[],"class_list":["post-14565","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-developer-tools"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb\/14565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/comments?post=14565"}],"version-history":[{"count":44,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb\/14565\/revisions"}],"predecessor-version":[{"id":14679,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb\/14565\/revisions\/14679"}],"wp:attachment":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/media?parent=14565"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb-category?post=14565"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb-tag?post=14565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}