Skip to content

Instantly share code, notes, and snippets.

@lbngoc
Last active April 16, 2024 12:45
Show Gist options
  • Save lbngoc/85847a73acaf5d450bc506da8ae39a11 to your computer and use it in GitHub Desktop.
Save lbngoc/85847a73acaf5d450bc506da8ae39a11 to your computer and use it in GitHub Desktop.
WP-CLI Reference Cheat Sheet

Quick mysql notes

mysql -u myawesomeusername -p
mysql> create database thisismy\_databasename;
mysql> grant all on thisismy\_databasename.\* to 'thisismy\_username' identified by 'ApasswordBetterThanThis';

In a nutshell

(copy and run after successfully running wp core config and wp core install)

wp plugin delete akismet hello;
wp theme activate twentytwelve;
wp theme delete twentyfourteen twentythirteen;
wp post delete $(wp post list --post\_type='post' --format=ids);
wp post delete $(wp post list --post\_type='page' --format=ids);
wp widget delete $(wp widget list sidebar-1 --format=ids);
wp option set default\_comment\_status closed;

WP-CLI

This assumes you have installed wp-cli and are familiar with the command line. There are many great tutorials out there on wp-cli installation and beginner use. I have simply compiled a list of commands I often use.

DOWNLOAD AND INSTALL WORDPRESS
wp core download
wp core config --dbname=my\_databasename --dbuser=my\_databaseuser --dbpass=password
wp core install --url=mysite.com --title='Awesome Site' --admin\_user=mydesiredusername1 --admin\_password=changemepass --admin\_email=myemail@mysite.com
DELETE PLUGINS AND THEMES

(Why keep what you don’t use?)

wp plugin delete akismet hello
wp theme delete twentyfourteen twentythirteen
DELETE ALL POSTS:

(good for when setting up an new site to quickly delete hello word fluff)

wp post delete $(wp post list --post\_type='post' --format=ids)
DELETE ALL PAGES:

(good for when setting up an new site to quickly delete hello word fluff)

wp post delete $(wp post list --post\_type='page' --format=ids)
REMOVE WIDGETS FROM SIDEBAR:

(sidebar-1 is default for wordpress twentytwelve, twentythriteen and twentyfourteen)

wp widget delete $(wp widget list sidebar-1 --format=ids)
CHANGE DESCRIPTION

(no my site is not ‘Just another WordPress site’)

wp option update blogdescription "One of a kind epic tagline for my one of a kind epic site"
TURN OF COMMENTS BY DEFAULT

(must be done before page creation to apply to the new pages)

wp option set default\_comment\_status closed
CREATE A PAGE

(Pretty static here…)

wp post create --post\_type=page --post\_status=publish --post\_title='About'
Pages everyone loves:
wp post create --post\_type=page --post\_status=publish --post\_title='Home'
wp post create --post\_type=page --post\_status=publish --post\_title='About'
wp post create --post\_type=page --post\_status=publish --post\_title='Contact'
Get fancy and do it all at once:
wp post create --post\_type=page --post\_status=publish --post\_title='Home' &&
wp post create --post\_type=page --post\_status=publish --post\_title='About' &&
wp post create --post\_type=page --post\_status=publish --post\_title='Contact'
CREATE A POST

(We like change…)

wp post create --post\_type=post --post\_status=publish --post\_title='The meaning of life...'
LIST ALL POSTS (OR PAGES OR ANY OTHER CONTENT TYPE)

(as shown before)

wp post list

wp post list --post\_type='page'

wp post list --post\_type='customcontenttype'
SET FRONT PAGE AND POSTS PAGE

(Home page ID is 5, Blog page ID is 10)

wp option update page\_on\_front 5
wp option update page\_for\_posts 10
wp option update show\_on\_front page
CHANGE THE SITE URL
wp option update siteurl http://mynewurl.com
wp option update home http://mynewurl.com
PERMALINKS
SET TO DEFAULT
wp option update permalink\_structure ""
SET TO POST NAME
wp rewrite structure '/%postname%/' --hard

Changing file permissions

Owner

chown -R username target-directory/

Group

chgrp -R servergroupname target-directory/

Directories

find /path/to/your/wordpress/install/ -type d -exec chmod 755 {} ;

Files

find /path/to/your/wordpress/install/ -type f -exec chmod 644 {} ;

Migrate Sites w/ WP-CLI

On old server

In WordPress directory export the db:

wp export

On new server

Send the file to the new server

scp username@oldserver.com:~/wordpres-directory/my-sql-dump-file-name.sql ~/public/

wp import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment