> For the complete documentation index, see [llms.txt](https://docs.ititanhosting.ro/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ititanhosting.ro/docs/english/vps/linux/how-to-install-and-use-wp-cli.md).

# How to install and use WP-CLI

WP-CLI is a command-line tool for managing WordPress sites. Instead of logging into the admin dashboard for every small task, you can manage updates, plugins, users, and content directly from the terminal. It is faster and perfect for automation.

### Step 1: Download WP-CLI

```bash
cd /tmp
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
```

### Step 2: Make it executable and move it to PATH

```bash
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
```

Verify it works:

```bash
wp --info
```

You should see version and environment details.

### Step 3: Install WordPress (if not already installed)

Navigate to your web directory and download WordPress:

```bash
cd /var/www/example.com/html
wp core download
```

Create the wp-config file:

```bash
wp config create --dbname=mydb --dbuser=myuser --dbpass=mypassword --dbhost=localhost
```

Run the installation:

```bash
wp core install --url=example.com --title="My Site" --admin_user=admin --admin_password=strongpass --admin_email=admin@example.com
```

That is it. Your WordPress site is now ready.

### Step 4: Manage plugins

List all installed plugins:

```bash
wp plugin list
```

Install a plugin:

```bash
wp plugin install woocommerce --activate
```

Update all plugins:

```bash
wp plugin update --all
```

Deactivate a plugin:

```bash
wp plugin deactivate plugin-name
```

### Step 5: Manage themes

List themes:

```bash
wp theme list
```

Install and activate a theme:

```bash
wp theme install astra --activate
```

### Step 6: Manage users

List users:

```bash
wp user list
```

Create a new user:

```bash
wp user create editor editor@example.com --role=editor --user_pass=securepass
```

### Step 7: Update WordPress core

```bash
wp core update
wp core update-db
```

Always update the database after updating the core.

### Step 8: Useful WP-CLI commands for everyday work

| Command                                 | What it does                                        |
| --------------------------------------- | --------------------------------------------------- |
| `wp cache flush`                        | Clear all cache                                     |
| `wp search-replace 'old.com' 'new.com'` | Update URLs in the database (useful for migrations) |
| `wp db export`                          | Export the database to a SQL file                   |
| `wp db import file.sql`                 | Import a database                                   |
| `wp post list`                          | List recent posts                                   |
| `wp media list`                         | Show uploaded media                                 |
| `wp option get siteurl`                 | Get the site URL setting                            |
| `wp rewrite flush`                      | Flush rewrite rules                                 |

### Step 9: Set up cron for WordPress

WordPress has its own cron system, but it only runs when someone visits the site. For better reliability, disable WP cron and use the system cron instead.

Add this to `wp-config.php`:

```php
define('DISABLE_WP_CRON', true);
```

Then set up a system cron job:

```bash
crontab -e
```

Add this line to run every 15 minutes:

```
*/15 * * * * /usr/local/bin/wp cron event run --due-now --path=/var/www/example.com/html
```

### Step 10: Keep WP-CLI updated

WP-CLI can update itself:

```bash
wp cli update
```

### Common tasks by example

**Reset a forgotten password:**

```bash
wp user update admin --user_pass=newpassword
```

**Bulk update all plugins and themes:**

```bash
wp plugin update --all && wp theme update --all
```

**Check WordPress health:**

```bash
wp core verify-checksums
```

**Export and import content:**

```bash
wp export --user=admin
wp import exported-file.xml --authors=create
```

### Common issues

**"Error: This does not seem to be a WordPress site"** Navigate to the WordPress root directory first. WP-CLI needs to find the `wp-config.php` file.

**"Error: Too many redirects"** This usually means the site URL is misconfigured. Check it with `wp option get siteurl` and `wp option get home`.

**Permission errors** Make sure the web server user can read and write WordPress files. On Ubuntu with Nginx, that is usually `www-data`.

### Security tips

* Do not use `--user_pass` in commands that get logged to shell history. Use `wp user create` interactively instead.
* Keep WP-CLI and WordPress core updated.
* Limit WP-CLI access to trusted users on the server.

### Conclusion

WP-CLI turns hours of clicking into seconds of typing. Once you get used to managing WordPress from the terminal, going back to the admin dashboard feels slow.
