- Getting Started With WordPress Optimized Template
- How To Deploy A WordPress Optimized Template Server
- Adding Sites To Your WordPress Optimized Template Server
- Use SFTP To Connect To WordPress Optimized Template
- Using WP-CLI With Your WordPress Optimized Template Site
- How To Restore WordPress Optimized Template Backups
- cPanel How To: Change a Domain’s MX Record
Using the Command Line ToolsLiquid Web’s WordPress Optimized Template servers come with a pre-installed set of command-line tools designed to simplify common site maintenance tasks. WP-CLI (WordPress Command Line Interface) can be accessed via SSH, and allows you to do nearly anything that can be done from within the WordPress admin interface. Among many other things, WP-CLI can be used to:
- create and modify users and their roles
- install and activate or deactivate plugins
- perform basic database maintenance tasks
- manage cron jobs
- manage rewrite rules
[examplesiteuser@wphost]# pwd /home/examplesiteuser/ [examplesiteuser@wphost]# cd public_html [examplesiteuser@wphost ~/public_html]# pwd /home/examplesiteuser/public_html
Create a New User and Assign a RoleIn this example, we’re going to create a new user named “sample”, assign them the role of Editor on the site, and email them their credentials (a randomly-generated password will be assigned automatically, and the email is sent to their specified address with a link to reset it.):
wp user create sample firstname.lastname@example.org --role=editor --send-emailBreaking down this command:
- wp invokes WP-CLI
- user create creates the new user
- sample is the name we chose for the new user
- email@example.com is the new user’s email address
- –role=editor assigns the role of Editor to the new user
- –send-email instructs WordPress to email the user’s credentials, and a link to change their password, to the new user’s email address
Install and Activate a PluginHere we’re going to download, install, and activate the Meta Slider plugin from wordpress.org. While we know the name of the plugin, the plugin’s actual file name may be different, so we first will run a search to find out, using the command:
[examplesiteuser@wphost ~/public_html]# wp plugin search metasliderRunning that command tells us that the plugin’s name is “ml-slider”:
[examplesiteuser@wphost ~/public_html]# wp plugin search metaslider Success: Showing 4 of 4 plugins. +----------------------------+----------------------------+--------+ | name | slug | rating | +----------------------------+----------------------------+--------+ | Meta Slider | ml-slider | 96 | | ThreeWP Broadcast | threewp-broadcast | 94 | | EWWW Image Optimizer | ewww-image-optimizer | 90 | | EWWW Image Optimizer Cloud | ewww-image-optimizer-cloud | 90 | +----------------------------+----------------------------+--------+ [examplesiteuser@wphost ~/public_html]#Now that we know the file name of the Meta Slider plugin, we can download, install and activate ml-slider with the following command:
wp plugin install ml-slider --activateRunning that code generates the following output:
[examplesiteuser@wphost ~/public_html]# wp plugin install ml-slider --activate Installing Meta Slider (3.3.6) Downloading install package from https://downloads.wordpress.org/plugin/ml-slider.3.3.6.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Success: Translations updates are not needed for the 'English (US)' locale. Activating 'ml-slider'... Success: Plugin 'ml-slider' activated. [examplesiteuser@wphost ~/public_html]#The “Success” messages tell us that ml-slider was installed and activated.
Database OperationsWP-CLI has a number of basic database management features built right in.
wp db exportRunning that command will export the site’s database to an sql file in the current working directory. If you do not specify a name for the file, the exported database file will be the database name with a “.sql” extension, and any existing file with that name will be overwritten. Because we are backing up the database and don’t want this file to overwrite any previous versions, we will be specifying a file name (which must end in .sql).
[examplesiteuser@wphost ~/public_html]# wp db export mydatabase_01042016.sql Success: Exported to mydatabase_01042016.sqlYou always should back up the WordPress database prior to making any significant changes. It takes only a moment and ensures that you will have a restore point in case your changes don’t quite go as planned.
Restore A DatabaseYou can quickly restore a database from an sql file with:
wp db importSimply supply the file name of the database to restore, and the specified sql file will overwrite the existing database:
[examplesiteuser@wphost ~/public_html]# wp db import mydatabase_01042016.sql Success: Imported from mydatabase_01042016.sql
Note that importing a database will completely overwrite the existing database. Do not import a database unless you are certain that you have no further use for the existing database or its contents. If in doubt, back up the current version with a unique file name before restoring.
Search and Replace in the DatabaseWP-CLI includes an advanced search and replace that can be used for delicate operations on database tables. This feature commonly is used to update references to the site name when taking a development site into production. In this example, we’re going to change the name of the user we created earlier, “sample”, to “example” using the command “wp search-replace”. Note that we’ve already backed up the database above using “wp db export”. And because we’re changing a username, we’ll also specify that we want only to include the wp_users and wp_usermeta tables in the search and replace, so that the word “sample” doesn’t get replaced with “example” in posts or anywhere else it may crop up:
wp search-replace 'sample' 'example' wp_users wp_usermetaBreaking down that command:
- wp invokes WP-CLI
- search-replace searches for the specified string and replaces it in the specified tables
- ’sample’ is the string we’re searching for (the old text). The search string needs to be enclosed in quotes.
- ’example’ is the string we’re replacing the old text with. The string needs to be enclosed in quotes.
- wp_users wp_usermeta are the specific tables to which we are restricting the search and replace. You can list as many table names as necessary, separating them with a space. If no table name is specified, the search and replace will be performed on the entire database. Performing a search and replace on the entire database is a potentially dangerous operation and should be done only if you are absolutely certain of the results and even then, only after backing up the existing database with a unique name.
Note: Do not attempt to run a search and replace (or any other database-altering procedure) without first backing up the database with a unique file name. Even the simplest search and replace has the potential to cause a great deal of damage, or even take the site down (consider the result if we had replaced the word “sample” with “example” in every table in the database for a product sample giveaway site). Always manually take a database backup with a unique file name before running search-replace.
Manage Cron JobsWP-CLI can be used to display scheduled cron jobs, run them manually, or even add new ones. For instance:
wp cron event listreturns a list of all scheduled WordPress crons. You also may choose to run a listed WordPress cron immediately:
wp cron event run wp_scheduled_deleteexecutes the wp_scheduled_delete cron event right away, instead of at its normally scheduled time.
Manage Rewrite RulesWP-CLI gives you the ability to see current rewrite rules as well as change the rewrite structure:
wp rewrite listdisplays all current rules. You also can change the rewrite structure using default WordPress variables. To make your rewrites display the month number first, followed by the year and the name of the post, you would use:
wp rewrite structure '/%monthnum%/%year%/%postname%'
Multisite WordPress InstallationsNearly all the WP-CLI commands can be used the same way on multisite installs as well, so long as you specify the site in the command. This is done by specifying the URL (in the format: –url=domainname.com) in the command. For example, the command to list scheduled crons for examplewordpresssite.com on multisite would become:
wp cron event list --url=examplewordpresssite.com