📜 ⬆️ ⬇️

WordPress Command Prompt

WordPress is becoming more and more like an operating system in a variety of settings and functions. As a result, the execution of tasks in the admin panel is too slow to work, which is especially noticeable when performing routine actions.

Any developer and administrator, going to a certain level of professionalism, seeks to automate and simplify such actions. In the case of an operating system, a tool to do this exists. This is the command line.

The benefits of using the command line are well known. Is there a similar tool to manage WordPress? It turns out, yes. This is wp-cli, a command line interface for WordPress.
')
Let's see what it is.

System requirements

We will need:

Installation

Download the installation script from the wp-cli website and run it:
curl http://wp-cli.org/installer.sh | bash 

All that is needed, the script will do by itself.
Other installation methods are described here .

Job

To start working with WordPress, you need to go to the directory where it is installed and be able to connect to its database.
After that we command
 wp 
and get a list of commands:

For each team, if you want to get help:
 wp help <command> 

But you can start from scratch and install WordPress with wp-cli first. Download the latest version of WordPress in the folder ./wp:
 wp core download --locale=ru_RU --path=wp 

A directory will be created (wp in our case) into which WordPress will be installed.
Go to the wp directory:
 cd wp 

Create wp-config.php:
 wp core config --dbname=wpcli --dbuser=root --dbpass=alkdfhqiu 

 Success: Generated wp-config.php file. 

And finally, install Wordpress:
 wp core install --url=wp.home --admin_password=ralrfaier --admin_email=xxx@yyy.zz --title="Wordpress   wp-cli" 

 /sendmail: Cannot open mail:25 Success: WordPress installed successfully. 

Well, WordPress is installed. It is easy to see that wp-cli is trying to send a message about this to the administrator’s email address.
To check if WordPress is installed, use the command
 wp core is-installed 

Well, let's create a user?
 wp user create VI moio@milo.ru --role=author --user_pass=qiwKKkjhKIU --user_registered=2013-06-29 --display_name=" " 

 Success: Created user 3. 

Here 3 is the identifier of the created user.
What kind of users do we have now?
 wp user list 

We get the list in the form of a table:
 +----+------------+---------------------------------+--------------+---------------------+---------------+ | ID | user_login | display_name | user_email | user_registered | roles | +----+------------+---------------------------------+--------------+---------------------+---------------+ | 1 | admin | admin | xxx@yyy.zz | 2013-06-29 03:50:49 | administrator | | 3 | VI |   | moio@milo.ru | 2013-06-29 00:00:00 | author | +----+------------+---------------------------------+--------------+---------------------+---------------+ 

The table, as we see, turns out to be wide, you have to configure the terminal for this work.
There is an interesting wp user generate command that allows you to create many users at once. This may be necessary when testing plugins.
 wp user generate --count=10 --role=subscriber 

 wp user list 

 +----+------------+---------------------------------+--------------+---------------------+---------------+ | ID | user_login | display_name | user_email | user_registered | roles | +----+------------+---------------------------------+--------------+---------------------+---------------+ | 1 | admin | admin | xxx@yyy.zz | 2013-06-29 03:50:49 | administrator | | 12 | user_1_10 | User 10 | | 2013-06-29 08:00:48 | subscriber | | 13 | user_1_11 | User 11 | | 2013-06-29 08:00:48 | subscriber | | 4 | user_1_2 | User 2 | | 2013-06-29 08:00:48 | subscriber | | 5 | user_1_3 | User 3 | | 2013-06-29 08:00:48 | subscriber | | 6 | user_1_4 | User 4 | | 2013-06-29 08:00:48 | subscriber | | 7 | user_1_5 | User 5 | | 2013-06-29 08:00:48 | subscriber | | 8 | user_1_6 | User 6 | | 2013-06-29 08:00:48 | subscriber | | 9 | user_1_7 | User 7 | | 2013-06-29 08:00:48 | subscriber | | 10 | user_1_8 | User 8 | | 2013-06-29 08:00:48 | subscriber | | 11 | user_1_9 | User 9 | | 2013-06-29 08:00:48 | subscriber | | 3 | VI |   | moio@milo.ru | 2013-06-29 00:00:00 | author | +----+------------+---------------------------------+--------------+---------------------+---------------+ 

Change user VI - because of him the table is a curve:
 wp user update 3 --display_name="Vasiliy Ivanovich" 

 Success: Updated user 3. 

Check it out.
 wp user list 

 +----+------------+-------------------+--------------+---------------------+---------------+ | ID | user_login | display_name | user_email | user_registered | roles | +----+------------+-------------------+--------------+---------------------+---------------+ | 1 | admin | admin | xxx@yyy.zz | 2013-06-29 03:50:49 | administrator | | 12 | user_1_10 | User 10 | | 2013-06-29 08:00:48 | subscriber | | 13 | user_1_11 | User 11 | | 2013-06-29 08:00:48 | subscriber | | 4 | user_1_2 | User 2 | | 2013-06-29 08:00:48 | subscriber | | 5 | user_1_3 | User 3 | | 2013-06-29 08:00:48 | subscriber | | 6 | user_1_4 | User 4 | | 2013-06-29 08:00:48 | subscriber | | 7 | user_1_5 | User 5 | | 2013-06-29 08:00:48 | subscriber | | 8 | user_1_6 | User 6 | | 2013-06-29 08:00:48 | subscriber | | 9 | user_1_7 | User 7 | | 2013-06-29 08:00:48 | subscriber | | 10 | user_1_8 | User 8 | | 2013-06-29 08:00:48 | subscriber | | 11 | user_1_9 | User 9 | | 2013-06-29 08:00:48 | subscriber | | 3 | VI | Vasiliy Ivanovich | moio@milo.ru | 2013-06-29 00:00:00 | author | +----+------------+-------------------+--------------+---------------------+---------------+ 

Well, now it's ok. Although declared full support for utf-8. There is no ideal in life ... Well, nothing will be finalized.
We can delete the user
 wp user delete 3 

 Success: Deleted user 3. 

What else? You can create, assign roles to users. Naturally, you can work with records. Create:
 wp post create --post_type=post --post_title=" №1" --post-status=future --post_date="2013-05-01 07:00:00" 

 Success: Created post 3. 

Like users, entries can be made in bulk:
 --count=10 --post_author=VI 

Get:
 wp post list 

 +----+-----------------------+---------------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+-----------------------+---------------------+---------------------+-------------+ | 3 | Post title | name | 2013-06-29 12:50:48 | draft | | 7 |  2 | post-2 | 2013-06-29 12:45:40 | publish | | 8 |  3 | post-3 | 2013-06-29 12:45:40 | publish | | 9 |  4 | post-4 | 2013-06-29 12:45:40 | publish | | 10 |  5 | post-5 | 2013-06-29 12:45:40 | publish | | 11 |  6 | post-6 | 2013-06-29 12:45:40 | publish | | 12 |  7 | post-7 | 2013-06-29 12:45:40 | publish | | 13 |  8 | post-8 | 2013-06-29 12:45:40 | publish | | 14 |  9 | post-9 | 2013-06-29 12:45:40 | publish | | 15 |  10 | post-10 | 2013-06-29 12:45:40 | publish | | 16 |  11 | post-11 | 2013-06-29 12:45:40 | publish | PHP Warning: sprintf(): Too few arguments in /home/andyudol/.composer/vendor/wp-cli/php-cli-tools/lib/cli/Streams.php on line 38 +----+-----------------------+---------------------+---------------------+-------------+ 

Ops! Some kind of mistake. And where Russian letters, the table is damaged. Well, nothing, the project is young, correct. And what is the version, by the way?
 wp --info 

 PHP binary: /usr/lib64/php5.4/bin/php PHP version: 5.4.13--pl0-gentoo php.ini used: /etc/php/cli-php5.4/php.ini wp-cli root: /home/andyudol/.composer/vendor/wp-cli/wp-cli/php/ wp-cli config: wp-cli version: 0.10.2 

0.10.2. Well, yes, a young project. On github.com, they have been present for about 2 years. Not so young ...
By the way, this is the latest version. I just just updated. This is how it is done:
 cd ~/.composer php composer.phar update 

And what about the promised automation? Delete some records. Let's do this with this script:
 for i in 7 9 11 12 16 do echo $i wp post delete $i done 

On the screen we will see:
 Success: Trashed post 7. Success: Trashed post 9. Success: Trashed post 11. Success: Trashed post 12. Success: Trashed post 16. 

Well, what else is interesting?
There are commands to simplify the creation of plug-ins, themes, taxonomies and record types.
For example, create a blank for the plugin:
 wp scaffold plugin zabor --plugin_name="  " 
We see:
 Success: Created /home/andyudol/wp/wp-content/plugins/zabor Success: Created test files. 
We are informed that the plugin directory and test files have been created. In fact, the main plugin file has also been created.
Let now we need to test this plugin. Commanding
 wp core init-tests ~/svn/wp-tests --dbname=wp_db --dbuser=root --dbpass=wpdbpass 
The WordPress testing library will be loaded and configured.
Go to the plugin directory
 cd wp-content/plugins/zabor 
and run the test
 WP_TESTS_DIR=~/svn/wp-tests phpunit 

What else?
Work with the database: create / delete, export / import and a few more commands.
Search and replace in database. It is useful when transferring the database.
Manage user roles and permissions.
Caching management
Manage rewrite rules.
Manage arbitrary record fields and additional user profile fields.
There are commands that allow you to run any PHP code:
wp eval - from the command line,
wp eval-file - from file.
And the wp shell command opens an interactive PHP console.
Yes, and a little more detail about the export. The wp export command allows, in addition to everything that is possible by regular WordPress tools, first, to export individual entries (or several, given by their identifiers) and, secondly, there is the possibility of exporting not into one file, but into several, containing the specified number of records.
 wp export --post__in=12,124, 325 

 wp export --file_item_count=30 



Well, that's all, each team is not ready to describe in detail yet, there is not enough experience with it. When will be, then there will be a continuation. May be.

Source: https://habr.com/ru/post/185428/


All Articles