|
| 1 | +# How to WP-CLI |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +Using WP-CLI you can perform specific tasks using the command line instead of accessing the WordPress admin panel. In the following section we will cover step by step some of these |
| 6 | +tasks. |
| 7 | + |
| 8 | +## How to install WordPress |
| 9 | + |
| 10 | +Download and install WordPress using WP-CLI is very easy. It's done in four simple steps. |
| 11 | +First you will need to download WordPress using the `wp core download` command. |
| 12 | + |
| 13 | +### Step 1 - Download WordPress |
| 14 | + |
| 15 | +The syntax of the command to download WordPress is the following `wp core download [--path=<path>] [--locale=<locale>] [--version=<version>] [--skip-content] [--force]` |
| 16 | + |
| 17 | + $ wp core download --path=wpdemo.test --locale=it_IT |
| 18 | + Creating directory '/wpdemo.test/'. |
| 19 | + Downloading WordPress 5.4.1 (it_IT)... |
| 20 | + md5 hash verified: 3fa03967b47cdfbf263462d451cdcdb8 |
| 21 | + Success: WordPress downloaded. |
| 22 | + |
| 23 | +The command above creates a wpdemo.test/ folder inside your current working directory and downloads the latest WordPress version. You can replace the --path=wpdemo.test with your |
| 24 | +desired folder name and the --locale=it_IT with your desired locale. You can omit the --locale option and that will download by default WordPress in American English using the locale en_US. |
| 25 | + |
| 26 | +### Step 2 - Generate a config file |
| 27 | + |
| 28 | +In this step we will generate a config file and setup the database |
| 29 | +credentials for our installation. |
| 30 | +The basic syntax of the command is the following `wp config create --dbname=<dbname> --dbuser=<dbuser> [--dbpass=<dbpass>]` |
| 31 | + |
| 32 | + $ wp config create --dbname=your_db_name_here --dbuser=your_db_user_here --prompt=dbpass |
| 33 | + 1/10 [--dbpass=<dbpass>]: type_your_password |
| 34 | + Success: Generated 'wp-config.php' file. |
| 35 | + |
| 36 | +The command above generates the wp-config.php file and adds to it the database credentials that you passed. Make sure to replace "your_db_name_here" with the name you want to assign to the database, replace "your_db_user_here" with your database user and type the database password when prompted `1/10 [--dbpass=<dbpass>]:` |
| 37 | + |
| 38 | +### Step 3 - Create the database |
| 39 | + |
| 40 | +In this step we are going to actually create the database based on the information we passed to the wp-config.php file in the step 2. |
| 41 | + |
| 42 | + $ wp db create |
| 43 | + Success: Database created. |
| 44 | + |
| 45 | +Now we are ready to move to the final step where we actually install WordPress. |
| 46 | + |
| 47 | +### Step 4 - Install WordPress |
| 48 | + |
| 49 | +To install WordPress now we need to run one last command. |
| 50 | + |
| 51 | + $ wp core install --url=wpclidemo.dev --title="WP-CLI" --admin_user=wpcli --admin_password=wpcli [email protected] |
| 52 | + Success: WordPress installed successfully. |
| 53 | + |
| 54 | +remember to replace the values passed to each of the following options with the your desider values: |
| 55 | + |
| 56 | +- `--url=wpclidemo.dev` replace wpclidemo.dev with your website url, |
| 57 | +- `--title="WP-CLI"` replace WP-CLI with the name you want to assign to the website, |
| 58 | +- `--admin_user=wpcli` replace wpcli with the username you want to assign to the website administrator |
| 59 | +- `--admin_password=wpcli` replace wpcli with the password you want to use to access the WordPress administrator panel. |
| 60 | + |
| 61 | +Congratulation! You have successfully Installed WordPress using WP-CLI. |
| 62 | + |
| 63 | +## How to put the site in maintenance mode |
| 64 | + |
| 65 | +WP-CLI offers a command to enable, disable maintenance mode and check in maintenance mode is |
| 66 | +enabled or not. |
| 67 | + |
| 68 | +### Step 1 - Check the status |
| 69 | + |
| 70 | + $ wp maintenance-mode status |
| 71 | + Maintenance mode is not active. |
| 72 | + |
| 73 | +### Step 2 - Enable maintenance mode |
| 74 | + |
| 75 | + $ wp maintenance-mode activate |
| 76 | + Enabling Maintenance mode... |
| 77 | + Success: Activated Maintenance mode. |
| 78 | + |
| 79 | +### Step 3 - Disable maintenance mode |
| 80 | + |
| 81 | + $ wp maintenance-mode deactivate |
| 82 | + Disabling Maintenance mode... |
| 83 | + Success: Deactivated Maintenance mode. |
| 84 | + |
| 85 | +## How to start the web server |
| 86 | + |
| 87 | +You can user the command `wp server` to launches PHP's built-in web server for a specific WordPress installation. By default the webserver will start using the local host and default port 8080 but you can change them using the --host and --port options. |
| 88 | + |
| 89 | +### Step 1 - Start the web server using the default settings |
| 90 | + |
| 91 | + $ wp server |
| 92 | + PHP 7.2.24-0ubuntu0.18.04.4 Development Server started at Thu Jun 4 17:40:13 2020 |
| 93 | + Listening on http://localhost:8080 |
| 94 | + Document root is ../wpdemo.test |
| 95 | + Press Ctrl-C to quit. |
| 96 | + |
| 97 | +This command will start the server using the default settings. We can now open our browser |
| 98 | +and visit the link http://localhost:8080 to access our WordPress installation. |
| 99 | + |
| 100 | +### Step 2 - Start the web server using different settings |
| 101 | + |
| 102 | +If you want to specify a different port number or host you can simply pass them to the options |
| 103 | +`--port` and `--host` |
| 104 | + |
| 105 | + $ wp server --port=9090 --host=192.168.0.4 |
| 106 | + |
| 107 | +The command above will start the web server and listen for requests on http://192.168.0.4:9090 |
| 108 | +you can open the browser and visit the page to access your WordPress intallation. |
| 109 | + |
| 110 | +## How to create a custom plugin: |
| 111 | + |
| 112 | +If you want to create your own plugins, WP-CLI has a powerful scaffold command that allows us to generate starter code. In this guide we will see how to generate starter code for a basic plugin. |
| 113 | + |
| 114 | +### Step 1 - Scaffold the plugin files |
| 115 | + |
| 116 | +The following command uses a number of options to lets us specify the plugin slug, its name, description, author name and uri as well as the plugin uri. You can replace the values passed to the options below to curomize the plugin based on your needs. |
| 117 | + |
| 118 | + $ wp scaffold plugin wpcli-demo-plugin --plugin_name="WP-CLI Demo Plugin" --plugin_description="This is a wp-cli demo plugin" --plugin_author=wp-cli --plugin_author_uri="https://wp-cli.org" --plugin_uri="https://plugins.wp-cli.org/demo-plugin"Success: Created plugin files. |
| 119 | + Success: Created test files. |
| 120 | + |
| 121 | +The above command generates a new folder called wpcli-demo-plugin in the plugins directory, with the following files structure. |
| 122 | + |
| 123 | + | - bin/ |
| 124 | + | - tests/ |
| 125 | + | - .gitignore |
| 126 | + | - .editorconfig |
| 127 | + | - .phpcs.xml.dist |
| 128 | + | - .travis.yml |
| 129 | + | - Gruntfile.js |
| 130 | + | - package.json |
| 131 | + | - phpunit.xml.dist |
| 132 | + | - readme.txt |
| 133 | + | - wpcli-demo-plugin.php |
| 134 | + |
| 135 | +Unless you use the --skip-tests option the following files are always generated: |
| 136 | + |
| 137 | +- `phpunit.xml.dist` is the configuration file for PHPUnit. |
| 138 | +- `.travis.yml` is the configuration file for Travis CI. Use `--ci=<provider>` to select a different service. |
| 139 | +- `bin/install-wp-tests.sh` configures the WordPress test suite and a test database. |
| 140 | +- `tests/bootstrap.php` is the file that makes the current plugin active when running the test suite. |
| 141 | +- `tests/test-sample.php` is a sample file containing test cases. |
| 142 | +- `.phpcs.xml.dist` is a collection of PHP_CodeSniffer rules. |
| 143 | + |
| 144 | +### Step 2 - How to create a custom post type: |
| 145 | + |
| 146 | +We can now use the scaffold command again to add a custom post type inside our new plugin using the wp scaffold post-type command. |
| 147 | + |
| 148 | + \$ wp scaffold post-type books --label=Book --textdomain=wpcli-demo-plugin --dashicon=dashicons-book-alt --plugin=wpcli-demo-plugin |
| 149 | + Success: Created '/wpcli-demo-plugin/post-types/books.php'. |
| 150 | + |
| 151 | +### Step 3 - Write code inside the main file: |
| 152 | + |
| 153 | +The main plugin file wpcli-demo-plugin.php is the starting point that we can use to write our plugin logic. |
| 154 | + |
| 155 | +Inside the main plugin file lets now reference the new post type we just created. |
| 156 | + |
| 157 | +Open in your favourite text editor the file wpcli-demo-plugin.php |
| 158 | +and under the line saying "your code starts here" add the following: |
| 159 | +\$ Your code starts here. |
| 160 | +require('post-types/books.php'); |
| 161 | + |
| 162 | +### Step 4 - Activate the plugin |
| 163 | + |
| 164 | +You can now use two wp-cli commands to check the list of plugins and activate your newly created plugin. |
| 165 | + |
| 166 | + $ wp plugin list |
| 167 | + +-------------------+----------+-----------+---------+ |
| 168 | + | name | status | update | version | |
| 169 | + +-------------------+----------+-----------+---------+ |
| 170 | + | akismet | inactive | available | 4.1.5 | |
| 171 | + | hello | inactive | none | 1.7.2 | |
| 172 | + | wpcli-demo-plugin | inactive | none | 0.1.0 | |
| 173 | + +-------------------+----------+-----------+---------+ |
| 174 | + |
| 175 | +From the list above we can see that our plugin wpcli-demo-plugin is inactive. Let's enable it using anotehr command. |
| 176 | + |
| 177 | + $ wp plugin activate wpcli-demo-plugin |
| 178 | + Plugin 'wpcli-demo-plugin' activated. |
| 179 | + Success: Activated 1 of 1 plugins. |
| 180 | + |
| 181 | +Our plugin is now active. We can visit the WordPress admin panel and |
| 182 | +start using our books custom post type. |
| 183 | + |
| 184 | +## How to create a custom Taxonomy |
| 185 | + |
| 186 | +Coming Soon |
| 187 | + |
| 188 | +## How to Manage Widgets |
| 189 | + |
| 190 | +Coming Soon |
| 191 | + |
| 192 | +## How to Create a Child theme |
| 193 | + |
| 194 | +Coming soon |
0 commit comments