From this post we will see how to perform routine actions, but instead of a click, using the console.
And first of all, install a WordPress locally. For this example I am going to rely on a local environment with XAMPP. Create an entry in the hosts file, and a conf with a domain, for example, wptest.devz and point to the folder c: \xampp\htdocs\wptest\
Downloading WordPress with WP-CLI
Open the console and go to c:\xampp\htdocs\wptest\. The first thing is to download WordPress, for that we are going to use the core command, specifically the core download subcommand :
wp core download
This command will download the latest version of WordPress. Accept the following parameters:
[--path=<path>], to specify the path where we want WordPress to go down, if we do not indicate anything, it will be downloaded to the current directory.
[--locale=<locale>], to select the language in which we want you to download WordPress.
[--version=<version>], in case we want to download a specific version of WordPress.
[--force], to overwrite files in the case that we are downloading WordPress in a directory that already contains a version of it.
For our case then it may be convenient to download the English version:
wp core download —locale=en_US
Creating and configuring the wp-config.php file from the command line
The next step is to generate the wp-config file with the parameters of connection to the database, etc… We will use the create subcommand of the core command:
wp config create —dbname=wptest —dbuser=myuser —dbpass=mypass
The options of this subcommand are:
--dbname=<dbname>, to establish the name of the database.
--dbuser=<dbuser>, to set the user access to database.
[--dbpass=<dbpass>], to set the password to access the database.
[--dbhost=<dbhost>], to establish the host of the database. default: localhost.
[--dbprefix=<dbprefix>], to set the prefix of the tables in the database. default: wp_
[--dbcharset=<dbcharset>], to establish the charset of the database. default: utf8.
[--dbcollate=<dbcollate>], to establish the collation of the database.
[--locale=<locale>], to set the constant WPLANG ..
[--extra-php], to write PHP instructions.
[--skip-salts], so as not to generate keys & salts.
[--skip-check], so as not to check the connection to the database.
[--force], to overwrite the file if it exists
Some examples with this subcommand:
# Standard file wp–config.php
$ wp config create —dbname=wptest —dbuser=myuser —dbpass=mypassword —locale=en_US
Success: Generated ‘wp-config.php’ file.
# Enable WP_DEBUG y WP_DEBUG_LOG
$ wp config create —dbname=wptest —dbuser=myuser —dbpass=mypassword —extra–php <<PHP
$ define( ‘WP_DEBUG’, true );
$ define( ‘WP_DEBUG_LOG’, true );
Success: Generated ‘wp-config.php’ file.
The next thing is to create the database. For this we also have a command:
It will create a database with the information set in the file wp-config.php.
Installing WordPress with WP-CLI
Finally we are going to install WordPress. For this we will use the install subcommand of the core command. This subcommand accepts the following parameters:
--url=<url>, the URL of our domain.
--title=<site-title>, the title of our site.
--admin_user=<username>, the username of the administrator user.
[--admin_password=<password>], the password of the administrator user. If we do not put it in, it will create a random password.
--admin_email=<email>, email of the administrator user.
[--skip-email], so as not to send an email to the administrator user with the notification that a new site has been created
wp core install —url=mydomain.com —title=“This is my site made with WordPress” —admin_user=my_user—admin_password=my_password—firstname.lastname@example.org
If everything is correct we will receive the message Success: WordPress installed successfully . We already have WordPress installed!
Installing and activating plugins
From the command line we can also download, install and activate or deactivate plugins. The wp plugin command has many subcommands, although for this entry I will only comment on wp plugin install. Accept the following parameters:
- <plugin|zip|url> …, accept that we pass the slug of the plugin, the local path to a .zip file, or the remote path of a .zip file
- [-Version = <version>], we can even install a specific version of a plugin
- [-Force], to overwrite the entire installation of the plugin if it is already installed, without asking for confirmation.
- [-Activate], to activate the plugin directly once downloaded.
- [-Activate-network], to activate the plugin for the entire network (in multisite environments).
For example, to install and activate directly WP SuperCache, the command would be:
wp plugin install wp–super–cache —activate
This command opens the door to create your own bash, where you write the instructions to download and install the usual plugins that each of us usually use by default in any web project.