Force Uploading of Files Using Wp Cli
The writer selected the Complimentary Software Foundation to receive a donation equally part of the Write for DOnations program.
Introduction
WP-CLI is a command-line tool for WordPress evolution and administrative tasks. Information technology provides several commands that yous can employ to manage your WordPress website without needing to log in to the dashboard and navigate through the pages.
Using WP-CLI to manage your WordPress installation over the conventional interface procedure helps to speed up your workflow. For many aspects of your website, y'all can besides use WP-CLI in Bash scripts to automate tasks that are tedious or have a long time to perform.
In this tutorial, you'll use many of the features of WP-CLI and discover how it tin can fit into your workflow. You'll cover common operations such as managing plugins and themes, creating content, working with the database, and updating WordPress. The capabilities of WP-CLI go beyond this tutorial; withal, y'all'll be able to transfer the skills from this tutorial to work with the more common options of other WP-CLI features.
Prerequisites
To follow this tutorial, yous'll need a secure WordPress installation. If y'all need to fix WordPress, you tin can follow these tutorials for your called server distribution:
- A server that is configured with a non-root
sudo
user. You can follow one of our Initial Server Setup Guides for this. - Linux, Apache, MySQL, PHP (LAMP stack) installed on your server. Follow How To Install Linux, Apache, MySQL, PHP (LAMP) stack for your server's distribution.
- A secure WordPress installation. You can set this up by following How To Install WordPress with a LAMP Stack.
Note: It's also possible to install WordPress with WP-CLI if you don't have an existing setup, just we won't be roofing that aspect in this article.
Step 1 — Installing WP-CLI
In this step, you lot'll install the latest version of the WP-CLI tool on your server. The tool is packaged in a Phar file, which is a packaging format for PHP applications that makes app deployment and distribution convenient.
Y'all can download the Phar file for WP-CLI through curl
:
- coil -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Once you have downloaded the file, run the following command to verify that information technology is working:
- php wp-cli.phar --info
Yous volition receive the post-obit output:
Output
OS: Linux v.iv.0-51-generic #56-Ubuntu SMP Mon Oct five 14:28:49 UTC 2022 x86_64 Vanquish: /bin/bash PHP binary: /usr/bin/php7.four PHP version: seven.4.3 php.ini used: /etc/php/7.4/cli/php.ini WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli WP-CLI vendor dir: phar://wp-cli.phar/vendor WP_CLI phar path: /home/ayo WP-CLI packages dir: WP-CLI global config: WP-CLI project config: WP-CLI version: two.4.0
Next, make the file executable with the following control:
- chmod +10 wp-cli.phar
At this point, y'all tin can execute the wp-cli.phar
file directly to admission the WP-CLI tool. To make information technology available globally on the system, move information technology to your /usr/local/bin/
directory and rename it to wp
. This ensures that you lot can access WP-CLI from whatever directory past entering the wp
command at the first of a prompt:
- sudo mv wp-cli.phar /usr/local/bin/wp
Now, you volition be able to issue the following control to check the installed version of WP-CLI:
- wp cli version
Output
WP-CLI 2.iv.0
In this pace, you installed WP-CLI on your server. You can bank check out alternative installation methods in the documentation. In subsequent sections, y'all'll explore the tasks you lot tin accomplish through the WP-CLI interface.
Step ii — Configuring WordPress Plugins
Information technology can be irksome to install and manage WordPress plugins through the admin user interface. It's possible to offload such tasks to WP-CLI to make the process much faster. In this section you will learn to install, update, and delete plugins on a WordPress site through the command line.
Earlier y'all go on, brand sure you are in the directory of your WordPress installation:
- cd /var/www/wordpress
Remember to change the highlighted directory name to the directory that contains your WordPress installation. This might be your domain name, if you followed the prerequisite tutorials.
Listing Current Plugins
You can list the currently installed plugins on a WordPress site with the following control:
- wp plugin listing
It displays a list of plugin names along with their status, version, and an indication of an available update.
Output
+---------+----------+-----------+---------+ | proper noun | status | update | version | +---------+----------+-----------+---------+ | akismet | inactive | available | 4.one.seven | | hello | inactive | none | ane.seven.two | +---------+----------+-----------+---------+
Searching for Plugins
You tin can search for plugins through the search bar on the WordPress plugin repository page or you can utilize the following command for quicker access:
- wp plugin search seo
Once you lot run this control, you will receive a list of the top 10 plugins that lucifer the search term (as of early 2021). The expected output for the seo
query is:
Output
Success: Showing 10 of 4278 plugins. +------------------------------------------------------------+---------------------+--------+ | name | slug | rating | +------------------------------------------------------------+---------------------+--------+ | Yoast SEO | wordpress-seo | 98 | | All in One SEO | all-in-one-seo-pack | 92 | | Rank Math – SEO Plugin for WordPress | seo-past-rank-math | 98 | | The SEO Framework | autodescription | 98 | | SEOPress, on-site SEO | wp-seopress | 98 | | Slim SEO – Fast & Automated WordPress SEO Plugin | slim-seo | 92 | | W3 Total Cache | w3-total-cache | 88 | | LiteSpeed Cache | litespeed-cache | 98 | | SEO 2022 by Squirrly (Smart Strategy) | squirrly-seo | 92 | | WP-Optimize – Clean, Compress, Enshroud. | wp-optimize | 96 | +------------------------------------------------------------+---------------------+--------+
Yous can get to the adjacent page by using the --page
flag:
- wp plugin search seo --page= 2
Take annotation of the value in the slug
cavalcade. You'll utilise this value to install or update the plugin on the control line.
Installing Plugins
Yous tin can install one or more than plugins past using the wp plugin install
command. You notice the name of the plugin y'all want to install (in the slug
column) and pass it as an argument to wp plugin install
. You tin can too find the name of the plugin in the URL of the plugin folio.
- wp plugin install jetpack wordpress-seo gutenberg
The output indicates the progress and completion of the installation of each of the plugins:
Output
Installing Jetpack – WP Security, Fill-in, Speed, & Growth (nine.iii.1) Downloading installation package from https://downloads.wordpress.org/plugin/jetpack.ix.3.1.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Installing Yoast SEO (15.6.two) Downloading installation package from https://downloads.wordpress.org/plugin/wordpress-seo.15.six.2.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Installing Gutenberg (ix.8.1) Downloading installation parcel from https://downloads.wordpress.org/plugin/gutenberg.nine.8.ane.zero... Unpacking the package... Installing the plugin... Plugin installed successfully. Success: Installed 3 of iii plugins.
You tin run the wp plugin list
command again to ostend that you've installed the plugins successfully:
Output
+---------------+----------+-----------+---------+ | name | status | update | version | +---------------+----------+-----------+---------+ | akismet | inactive | available | 4.ane.7 | | gutenberg | inactive | none | 9.viii.1 | | hello | inactive | none | 1.7.ii | | jetpack | inactive | none | nine.3.1 | | wordpress-seo | inactive | none | xv.6.2 | +---------------+----------+-----------+---------+
If you want to install a plugin from a remote source other than the WordPress plugin repository, you lot can laissez passer the goose egg file's URL every bit an argument to wp plugin install
. This can be helpful for installing custom or premium plugins. For instance, the following command volition install the myplugin.zip
file hosted on case.com
. Brand certain to replace the highlighted URL with a link to the plugin cipher file earlier running the control:
- wp plugin install https://case.com/wp-content/uploads/myplugin.zip
To install an older version of a plugin in the WordPress repository, specify the desired plugin version through the --version
flag:
- wp plugin install jetpack --version= 8.0
Activating and Deactivating Plugins
You lot can install and actuate plugins in one become past appending the --actuate
flag to wp plugin install
:
- wp plugin install redirection --activate
Output
Installing Redirection (5.0) Downloading installation parcel from https://downloads.wordpress.org/plugin/redirection.zip... Using cached file '/home/ayo/.wp-cli/cache/plugin/redirection-5.0.naught'... Unpacking the bundle... Installing the plugin... Plugin installed successfully. Activating 'redirection'... Warning: Plugin 'redirection' is already active. Success: Installed i of i plugins.
To activate or deactivate one or more plugins, use the wp plugin activate
and wp plugin deactivate
commands respectively:
- wp plugin actuate jetpack gutenberg
- wp plugin deactivate jetpack gutenberg
Or y'all can use the --all
flag to activate or deactivate all plugins at once. This is useful if yous desire to debug a problem in your WordPress installation:
- wp plugin activate --all
- wp plugin conciliate --all
Updating Plugins
You can update plugins through the wp plugin update
control. Yous can choose to update a set of plugins or all of them at once past appending the --all
flag. For instance, to update the akismet
plugin, you can run the following command:
- wp plugin update akismet
Yous would receive output like to:
Output
Enabling Maintenance mode... Downloading update from https://downloads.wordpress.org/plugin/akismet.4.one.8.aught... Unpacking the update... Installing the latest version... Removing the sometime version of the plugin... Plugin updated successfully. Disabling Maintenance style... +---------+-------------+-------------+---------+ | name | old_version | new_version | status | +---------+-------------+-------------+---------+ | akismet | 4.1.7 | 4.1.viii | Updated | +---------+-------------+-------------+---------+ Success: Updated 1 of 1 plugins.
Deleting plugins
To delete WordPress plugins, you can use the wp plugin delete
command. You can specify one or more plugins to delete like the post-obit:
- wp plugin delete redirection
Your output will confirm the deletion:
Output
Deleted 'redirection' plugin. Success: Deleted one of 1 plugins.
You can also delete all the installed plugins in one go by appending the --all
flag instead of specifying the plugin names one later on the other:
- wp plugin delete --all
In this step, you've used WP-CLI to manage the plugins on your WordPress website. It'due south much faster to perform actions compared to clicking through the admin dashboard. In the side by side section, you'll leverage WP-CLI for installing and managing WordPress themes.
Step 3 — Configuring Themes
The process of managing themes through WP-CLI is about identical to the fashion you lot can use it to manage plugins. In this section, you'll source and apply new themes to a WordPress website through the wp theme
subcommand.
First, check what themes you lot currently have installed on the website:
- wp theme listing
You'll receive a list of the installed themes:
Output
+-----------------+----------+-----------+---------+ | name | status | update | version | +-----------------+----------+-----------+---------+ | twentynineteen | inactive | available | ane.8 | | twentytwenty | inactive | none | 1.6 | | twentytwentyone | active | available | 1.0 | +-----------------+----------+-----------+---------+
At that place are three themes currently installed and the active one is twentytwentyone
. If you want to detect something with more features, you can try a search similar the following:
- wp theme search color
The output shows there are 832 themes that lucifer the color
search term:
Output
Success: Showing x of 832 themes. +---------------------+---------------------+--------+ | name | slug | rating | +---------------------+---------------------+--------+ | Colour | color | 0 | | All Colors | all-colors | 100 | | Color Blog | color-web log | 98 | | Colour Block | color-block | 0 | | X Weblog color | x-blog-color | 0 | | Multicolor Concern | multicolor-business | 0 | | ColorNews | colornews | 100 | | Colorist | colorist | 100 | | ColorMag | colormag | 98 | | MultiColors | multicolors | 74 | +---------------------+---------------------+--------+
Y'all tin can folio through the results using the --page
flag. For this case, but go ahead and install the ColorMag
theme since information technology has a pretty skillful rating. The --actuate
flag activates the theme immediately:
- wp theme install colormag --actuate
The output volition ostend the installation:
Output
Installing ColorMag (2.0.4) Downloading installation package from https://downloads.wordpress.org/theme/colormag.2.0.iv.zip... Unpacking the package... Installing the theme... Theme installed successfully. Activating 'colormag'... Success: Switched to 'ColorMag' theme. Success: Installed i of i themes.
If yous visit your website, you'll detect that the ColorMag theme was applied successfully.
The output from the wp theme list
command notes that in that location is an update available for both the twentynineteen
and twentytwentyone
themes. You can update them both using the post-obit command:
- wp theme update --all
Yous'll receive output like to the following:
Output
Downloading update from https://downloads.wordpress.org/theme/twentynineteen.1.ix.zip... Unpacking the update... Installing the latest version... Removing the old version of the theme... Theme updated successfully. Downloading update from https://downloads.wordpress.org/theme/twentytwentyone.1.1.naught... Unpacking the update... Installing the latest version... Removing the old version of the theme... Theme updated successfully. +-----------------+-------------+-------------+---------+ | name | old_version | new_version | status | +-----------------+-------------+-------------+---------+ | twentynineteen | 1.viii | i.ix | Updated | | twentytwentyone | i.0 | one.1 | Updated | +-----------------+-------------+-------------+---------+ Success: Updated two of 2 themes.
The wp theme
command offers many subcommands that can help you reach tasks like getting the details of a theme, checking if a particular theme is installed, or even deleting ane or more themes. You can explore all of the options past prepending help
earlier the subcommand, as in wp assistance theme
or wp help theme install
.
Now that you can manage themes through WP-CLI, you lot'll review the options that the tool provides for managing WordPress content.
Step iv — Creating Posts and Pages
WP-CLI provides several ways to manage content through the command line. Information technology can be more comfortable to write posts in the last if y'all're familiar with a control-line editor like nano or vim.
You can browse the list of posts on the site with:
- wp post list
You'll receive a list of posts:
Output
+----+--------------+-------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+-------------+---------------------+-------------+ | 1 | Hello world! | how-do-you-do-earth | 2021-01-24 12:32:06 | publish | +----+--------------+-------------+---------------------+-------------+
The output shows one published mail service with the title of Hello world!
and an ID of one
. To delete this postal service, utilize the wp post delete
control and laissez passer it the mail ID:
- wp post delete 1
Your output will confirm the postal service'south deletion:
Output
Success: Trashed post 1.
To create a new mail, run the post-obit command:
- wp post create --post_status=publish --post_title= "Sample mail created with WP-CLI" --edit
This command uses the --post_status
flag to set the status of the mail. Setting it to publish
ensures that the post is published immediately after running the command. If you want to create a draft instead, ready the --post_status
flag to draft
. The --post_title
flag is how you lot tin can specify the championship of the post, while --edit
causes the post trunk to be opened in the default system editor (vim). You can find out the other flags that yous can use in conjunction with the create
subcommand past typing wp help mail service create
in your terminal.
Once the vim editor is open, printing the i
key to enter INSERT style then enter the content of the mail service into the editor. One time you're washed editing the post, exit the vim editor by pressing the ESC
button and then type :wq
and printing ENTER
. Yous will receive the following output after exiting vim:
Output
Success: Created post half dozen.
If yous enter the wp post list
command again, you volition discover the postal service yous but created. You lot can also check the frontend of the website.
Instead of writing the mail on the command line, it's as well possible to import the mail content from a text file. Commencement, you demand to create the file. For example:
- touch content.txt
Adjacent, open up the file in a control-line editor to add or edit your content:
- nano content.txt
Once you're through with the edits, save and close the file past pressing CTRL-X
followed by Y
to salve. You lot tin can import the contents of that file as a WordPress post past using the following command. All you lot demand to do is specify the path to the file after the create
subcommand. For the example file here, you would run:
- wp postal service create ./content.txt --post_title= 'Sample Post Created with WP-CLI' --post_status=publish
If you want to create a page instead of a mail service, append the --post_type
flag and set it to page
:
- wp post create --post_title= "A simple folio" --post_status=draft --post_type=folio
Generating Posts or Pages
WP-CLI also provides an pick to cleanly generate posts and pages with dummy data. This is useful if you need custom data to quickly examination a theme or plugin that you are developing. The following control is to generate posts. If you don't include additional flags, information technology volition generate 100 posts past default.
- wp post generate
You can change the number of posts generated by using the --count
flag:
- wp postal service generate --count= 20
If you want to generate pages instead of posts, append the --post_type
flag and set information technology to page
:
- wp post generate --count= 20 --post_type=page
You can also utilize the wp assistance mail generate
to see other available options that tin help y'all get your desired outcome.
WordPress Revisions
It is non uncommon for older sites to have tens or hundreds of revisions on their chief pages due to years of editing and updating content. Revisions tin be helpful in case you need to revert back to a previous version of your content, but they tin also hurt the performance if there are too many. Y'all tin clean upwardly all the mail service revisions in the WordPress database by executing the following command:
- wp postal service delete $(wp post list --post_type= 'revision' --format=ids) --force
The command enclosed in the parenthesis is evaluated first and it will produce the ids
of all the postal service revisions that are present passing them to the delete
subcommand. The --force
flag is necessary considering posts of type 'revision'
do not support beingness sent to trash.
Stride 5 — Managing Your Database
One of the most useful features of WP-CLI is its ability to collaborate with the MySQL database. For example, if y'all demand an interactive session, you can enter a MySQL prompt with the following command:
- wp db cli
You tin can then utilise the MySQL shell as you lot usually would and, once you are through with your tasks, get out the shell by typing exit
.
For one-off queries, you can use the wp db query
command by passing a valid SQL query every bit an argument to the control. For example, to listing all the registered users in the WordPress database, you could run:
- wp db query "SELECT user_login,ID FROM wp_users;"
Y'all will be presented with an output similar to the following:
Output
+------------+----+ | user_login | ID | +------------+----+ | admin | 1 | +------------+----+
With wp db query
yous tin can run any i-off SQL query for the WordPress database.
Backing Up and Restoring
WP-CLI also allows you lot to back up your WordPress database. Running this post-obit command volition identify a SQL dump file in the current directory. This file contains your unabridged WordPress database including your posts, pages, user accounts, menus, and so on:
wp db export
Once the file is produced, y'all can move it to a different location for safekeeping:
Output
Success: Exported to 'wordpress-2021-01-25-25618e7.sql'.
You tin can also import a SQL dump file into your database through the wp db import
command. This is useful when you are migrating a WordPress website from ane location to another.
- wp db import file.sql
Searching and Replacing
Another common operation you can perform with WP-CLI is a find-and-replace performance. Yous can brand a dry run outset to find out how many instances it would modify. The first string is the search component while the second is the replacement:
- wp search-supplant --dry-run 'example.com' 'instance.net'
Afterward running this, your output would be like to the post-obit:
Output
Success: 10 replacements to be made.
Once you are certain you lot want to keep, remove the --dry-run
flag from the previous control:
- wp search-replace 'instance.com' 'example.net'
In this stride, you've reviewed several database operations that you tin can perform using WP-CLI. You can as well consummate other operations, such as optimizing the database, viewing database tables, deleting a database, or resetting one. You can explore the other options under the wp db
subcommand by typing wp help db
in your terminal.
Step 6 — Updating WordPress
You tin can update the core WordPress file with WP-CLI. You lot can examine the electric current version of WordPress that you have installed by running:
wp core version
Output
5.6
You tin bank check for updates through the wp core check-update
command. If your version is not the latest, this volition produce an output similar to the following:
Output
+---------+-------------+-----------------------------------------------------------------------+ | version | update_type | package_url | +---------+-------------+-----------------------------------------------------------------------+ | 5.half dozen.1 | minor | https://downloads.wordpress.org/release/wordpress-5.6.one-partial-0.zero | +---------+-------------+-----------------------------------------------------------------------+
If an update is available, you can install it with:
- wp core update
Output
Updating to version 5.6.ane (en_US)... PHP Warning: Declaration of WP_CLI\Cadre\CoreUpgrader::download_package($bundle, $check_signatures = truthful) should exist uniform with WP_Upgrader::download_package($package, $check_signatures = false, $hook_extra = Array) in phar:///usr/local/bin/wp/vendor/wp-cli/core-command/src/WP_CLI/Cadre/CoreUpgrader.php on line 30 Warning: Declaration of WP_CLI\Cadre\CoreUpgrader::download_package($package, $check_signatures = true) should exist compatible with WP_Upgrader::download_package($package, $check_signatures = fake, $hook_extra = Array) in phar:///usr/local/bin/wp/vendor/wp-cli/core-command/src/WP_CLI/Cadre/CoreUpgrader.php on line 30 Downloading update from https://downloads.wordpress.org/release/wordpress-five.half-dozen.1-partial-0.zip... Unpacking the update... Success: WordPress updated successfully.
You tin too update to a specific version by setting the --version
flag to the version number. If y'all want to revert to an older version, you also need to add the --forcefulness
flag, but this isn't recommended:
- wp core update --version= 5.6
- wp cadre update --version= 5.0 --force
In this final step, y'all updated your version of WordPress with WP-CLI.
Decision
For WordPress developers and adminstrator'southward working on the command line, WP-CLI is a great addition to the toolbox. In this tutorial, we covered several of the more mutual tasks that yous can perform through the command line.
WP-CLI has many more commands and options that you tin can familiarize yourself with to accomplish even more on the command line without the spider web interface. Use wp help <control>
to find out all the things yous can practise with a specific subcommand. There are likewise many community tools that extend WP-CLI with even more than features.
For more than tutorials on WordPress, check out our WordPress topic page.
Source: https://www.digitalocean.com/community/tutorials/how-to-use-wp-cli-v2-to-manage-your-wordpress-site-from-the-command-line
Belum ada Komentar untuk "Force Uploading of Files Using Wp Cli"
Posting Komentar