Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
WP-CLI –
Super Admin Level
Tips and Tricks
JONATHAN PERLMAN
@JPURPLEMAN
WWW.JPURPLEMAN.CA/WCOTTAWA2016
Jonathan Perlman
14 years using PHP & MySql as a web developer at Dawson College
9 years teaching the web and Microsoft Of...
What you need to know…
This works on Linux computers / servers.
Might work on Mac. Not tested on Mac.
Won't work on Window...
WP-CLI Requirements
UNIX-like environment (OS X, Linux, FreeBSD, Cygwin);
limited support in Windows environment
PHP 5.3.2...
The way to level up …
Learn bash!
6/18/2016 WORDCAMP OTTAWA 2016
hello-world.sh
#!/bin/bash
echo 'Hello World!'
bash hello-world.sh
chmod +x hello-world.sh
./hello-world.sh
6/18/2016 WORD...
variables.sh
#!/bin/bash
error
X=hello world
X = "hello world"
OK
X="hello world"
#output
echo $X
6/18/2016 WORDCAMP OTTAW...
conditionals.sh
#!/bin/bash
city=$1
if [ "$city" == "Ottawa" ]
then
echo "What a great city"
else
echo "Hello, you're in W...
loops-and-arrays.sh
#!/bin/bash
servers=( 'web1.example.com' 'web2.example.com' )
for server in "${servers[@]}"; do
echo $...
.bashrc & scripts
HTTPS://GITHUB.COM/JPURPLEMAN/WORDPRESS-STUFF
6/18/2016 WORDCAMP OTTAWA 2016
.bashrc & scripts
You can run the custom
command in any folder
Will work for only your user
account
Better if you have mor...
Script – wp-cli-install.sh
#!/bin/bash
servers=( 'web1.example.com' 'web2.example.com‘ )
curl -O https://raw.githubusercon...
.bashrc - Get WordPress Salts
alias wordpress-salt='wget
https://api.wordpress.org/secret-key/1.1/salt/ -qO-'
Normally one...
.bashrc - Update WordPress
alias wordpress-update-all='wp core update &&
wp core update-db --network &&
wp plugin update -...
.bashrc – Delete Sample Page
alias wordpress-delete-sample-page=‘wp post delete $(
wp post list
--post_type=page
--posts_p...
.bashrc - Remove Default Widgets
alias wordpress-remove-default-widgets='
wp widget delete search-2 &&
wp widget delete re...
Script - see-option-on-all-sites.sh
#!/bin/bash
for url in $( wp site list --field=url --url=http://site.com | sort –u )
d...
see-all-sites-with-gravity-forms.sh
#!/bin/bash
for blog_id in $(wp site list --field=blog_id --url=http://site.com | sort...
Script - create-pages-in-bluk.sh ( 1 )
userID=1
pages=( 'Home' 'About' 'Contact Us' )
for page in "${pages[@]}"; do
wp pos...
Script - create-pages-in-bluk.sh ( 2 )
wp menu create "Menu" --quiet
export IFS=" "
for pageID in $( wp post list
--order=...
Now to the real
timesavers!
6/18/2016 WORDCAMP OTTAWA 2016
Resulting git log – 14 commits
123438b 2016-06-16 Jonathan Perlman Adding plugin: WordPress Importer at version 0.6.2
449e...
.bashrc – git-wp-commit-object ( 1 )
Go into a plugin or theme folder
Get the current directory name
Check to see if we're...
.bashrc – git-wp-commit-object ( 2 )
Check to see if it's in the repo already or not
Create parts of the commit message co...
Script – wp-install.sh
If we're going to remove sites
Do mysql stuff to drop the db, revoke all and remove the user
Destro...
Resources
https://www.maketecheasier.com/write-linux-shell-scripts/
https://www.ltconsulting.co.uk/automated-wordpress-ins...
Thank you! Questions?
JONATHAN PERLMAN
@JPURPLEMAN
WWW.JPURPLEMAN.CA/WCOTTAWA2016
Upcoming SlideShare
Loading in …5
×

WP-CLI - Super Admin Tips and Tricks

1,677 views

Published on

WP-CLI is a powerful tool for developers and system administrators alike. I use the tool on a weekly basis for managing users, settings and WordPress installs. In this lightening talk, I will explain how I use the tool to create sites in bulk for learning environments, install and deploy updates to WordPress and perform changes to multi-site environments in a higher education setting.

Published in: Technology
  • Be the first to comment

WP-CLI - Super Admin Tips and Tricks

  1. 1. WP-CLI – Super Admin Level Tips and Tricks JONATHAN PERLMAN @JPURPLEMAN WWW.JPURPLEMAN.CA/WCOTTAWA2016
  2. 2. Jonathan Perlman 14 years using PHP & MySql as a web developer at Dawson College 9 years teaching the web and Microsoft Office for Dawson College 6 years using WordPress 2 WordCamp talks I’m not a Linux unicorn 6/18/2016 WORDCAMP OTTAWA 2016
  3. 3. What you need to know… This works on Linux computers / servers. Might work on Mac. Not tested on Mac. Won't work on Windows. Shared and managed hosts don't allow you to install WP-CLI We won't be talking about WP-CLI "Packages“ All WordPress code samples are located in the repo https://github.com/jpurpleman/WordPress-Stuff 6/18/2016 WORDCAMP OTTAWA 2016
  4. 4. WP-CLI Requirements UNIX-like environment (OS X, Linux, FreeBSD, Cygwin); limited support in Windows environment PHP 5.3.29 or later WordPress 3.7 or later WordPress 4.5 or later requires WP-CLI version 0.23.0 6/18/2016 WORDCAMP OTTAWA 2016
  5. 5. The way to level up … Learn bash! 6/18/2016 WORDCAMP OTTAWA 2016
  6. 6. hello-world.sh #!/bin/bash echo 'Hello World!' bash hello-world.sh chmod +x hello-world.sh ./hello-world.sh 6/18/2016 WORDCAMP OTTAWA 2016
  7. 7. variables.sh #!/bin/bash error X=hello world X = "hello world" OK X="hello world" #output echo $X 6/18/2016 WORDCAMP OTTAWA 2016
  8. 8. conditionals.sh #!/bin/bash city=$1 if [ "$city" == "Ottawa" ] then echo "What a great city" else echo "Hello, you're in WordCamp $city!" fi 6/18/2016 WORDCAMP OTTAWA 2016
  9. 9. loops-and-arrays.sh #!/bin/bash servers=( 'web1.example.com' 'web2.example.com' ) for server in "${servers[@]}"; do echo $server done 6/18/2016 WORDCAMP OTTAWA 2016
  10. 10. .bashrc & scripts HTTPS://GITHUB.COM/JPURPLEMAN/WORDPRESS-STUFF 6/18/2016 WORDCAMP OTTAWA 2016
  11. 11. .bashrc & scripts You can run the custom command in any folder Will work for only your user account Better if you have more than one server You have to copy it to work in a specific directory You can create reusable scripts You can create scripts the run scripts 6/18/2016 WORDCAMP OTTAWA 2016
  12. 12. Script – wp-cli-install.sh #!/bin/bash servers=( 'web1.example.com' 'web2.example.com‘ ) curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar for server in "${servers[@]}"; do echo $server scp ./wp-cli.phar user@$server: ssh user@$server "chmod +x wp-cli.phar" ssh user@$server "sudo mv wp-cli.phar /usr/local/bin/wp" ssh user@$server "/usr/local/bin/wp --info" done rm ./wp-cli.phar 6/18/2016 WORDCAMP OTTAWA 2016
  13. 13. .bashrc - Get WordPress Salts alias wordpress-salt='wget https://api.wordpress.org/secret-key/1.1/salt/ -qO-' Normally one line, not multiple… 6/18/2016 WORDCAMP OTTAWA 2016
  14. 14. .bashrc - Update WordPress alias wordpress-update-all='wp core update && wp core update-db --network && wp plugin update --all && wp theme update --all ' Normally one line, not multiple… 6/18/2016 WORDCAMP OTTAWA 2016
  15. 15. .bashrc – Delete Sample Page alias wordpress-delete-sample-page=‘wp post delete $( wp post list --post_type=page --posts_per_page=1 --post_status=publish --pagename="sample-page" --field=ID --format=ids )’ 6/18/2016 WORDCAMP OTTAWA 2016
  16. 16. .bashrc - Remove Default Widgets alias wordpress-remove-default-widgets=' wp widget delete search-2 && wp widget delete recent-posts-2 && wp widget delete recent-comments-2 && wp widget delete archives-2 && wp widget delete categories-2 && wp widget delete meta-2' Normally one line, not multiple… 6/18/2016 WORDCAMP OTTAWA 2016
  17. 17. Script - see-option-on-all-sites.sh #!/bin/bash for url in $( wp site list --field=url --url=http://site.com | sort –u ) do echo $url wp option get blogname done 6/18/2016 WORDCAMP OTTAWA 2016
  18. 18. see-all-sites-with-gravity-forms.sh #!/bin/bash for blog_id in $(wp site list --field=blog_id --url=http://site.com | sort -u ) do echo $blog_id wp db query "select count(id) from wp_${blog_id}_rg_form" done 6/18/2016 WORDCAMP OTTAWA 2016
  19. 19. Script - create-pages-in-bluk.sh ( 1 ) userID=1 pages=( 'Home' 'About' 'Contact Us' ) for page in "${pages[@]}"; do wp post create --post_type=page --post_title="$page" --post_status=publish --post_author=$userID --porcelain echo "wp post create $page" done 6/18/2016 WORDCAMP OTTAWA 2016
  20. 20. Script - create-pages-in-bluk.sh ( 2 ) wp menu create "Menu" --quiet export IFS=" " for pageID in $( wp post list --order="ASC" --orderby="ID" --post_type=page --post_status=publish --posts_per_page=-1 --field=ID --format=ids ); do wp menu item add-post menu $pageID --quiet done wp menu location assign menu primary 6/18/2016 WORDCAMP OTTAWA 2016
  21. 21. Now to the real timesavers! 6/18/2016 WORDCAMP OTTAWA 2016
  22. 22. Resulting git log – 14 commits 123438b 2016-06-16 Jonathan Perlman Adding plugin: WordPress Importer at version 0.6.2 449e092 2016-06-16 Jonathan Perlman Updating plugin: Print Friendly and PDF to version 3.4.6 b1d983a 2016-06-16 Jonathan Perlman Updating plugin: WooThemes Helper to version 1.6.2 8d2d7e3 2016-06-16 Jonathan Perlman Updating plugin: WP Migrate DB Pro Media Files to version 1.4.4 ce0181e 2016-06-16 Jonathan Perlman Updating plugin: WP Migrate DB Pro to version 1.6 3a37bd6 2016-06-16 Jonathan Perlman Updating plugin: Gravity Forms + Custom Post Types to version 3.1.3 91e2560 2016-06-16 Jonathan Perlman Updating plugin: jQuery Responsive Select Menu to version 1.5.0 83af679 2016-06-16 Jonathan Perlman Updating plugin: Gravity Forms to version 1.9.19 bbaa123 2016-06-16 Jonathan Perlman Adding plugin: Gravity Forms Advanced File Uploader at version 1.4 41b2ae1 2016-06-16 Jonathan Perlman Updating plugin: Google Analytics by MonsterInsights to version 5.5 81eac9d 2016-06-16 Jonathan Perlman Updating plugin: Custom Post Type UI to version 1.3.5 fbcc677 2016-06-16 Jonathan Perlman Updating plugin: Basic Google Maps Placemarks to version 1.10.6 c5bdb6c 2016-06-16 Jonathan Perlman Updating plugin: Advanced Custom Fields to version 4.4.7 476c404 2016-06-16 Jonathan Perlman Updating plugin: Accordion Shortcodes to version 2.3.0 6/18/2016 WORDCAMP OTTAWA 2016
  23. 23. .bashrc – git-wp-commit-object ( 1 ) Go into a plugin or theme folder Get the current directory name Check to see if we're in a plugin or theme folder Convert "plugins" to plugin or "themes" to theme Get details about the WordPress object we want to commit Get the title of the plugin or theme we're committing Get the version of the plugin or theme we're committing 6/18/2016 WORDCAMP OTTAWA 2016
  24. 24. .bashrc – git-wp-commit-object ( 2 ) Check to see if it's in the repo already or not Create parts of the commit message conditionally Add all files to git that have been added or modified Add all files to git that have been deleted or moved Git commit! with appropriate message Print that message 6/18/2016 WORDCAMP OTTAWA 2016
  25. 25. Script – wp-install.sh If we're going to remove sites Do mysql stuff to drop the db, revoke all and remove the user Destroy the file system folder of the site If we’re going to add sites Loop proposed sites and make sure we don't overwrite any folder Mysql stuff, drop db if exists, create database, grant privileges Delete and Create the directory of the install path Go into the install path Download the WordPress core files Create the wp-config file with our standard setup Generate random 8 character password Create database tables, and install WordPress Dump out information to a text file for the teacher Dump out information for the specific student discourage search engines delete sample page, and create homepage set homepage as front page set pretty urls delete akismet and hello dolly create a navigation bar disable file edit in wordpress config create .htaccess file create the .htpasswd file change ownership of the folder to apache change file permissions Calculate and send percent done to whiptail Convert text file of info for teacher to pdf Convert many student one page documents into one pdf 6/18/2016 WORDCAMP OTTAWA 2016
  26. 26. Resources https://www.maketecheasier.com/write-linux-shell-scripts/ https://www.ltconsulting.co.uk/automated-wordpress-installation-with-bash-wp-cli/ https://deliciousbrains.com/automating-local-wordpress-site-setup-scripts/ https://www.smashingmagazine.com/2015/09/wordpress-management-with-wp-cli/ 6/18/2016 WORDCAMP OTTAWA 2016
  27. 27. Thank you! Questions? JONATHAN PERLMAN @JPURPLEMAN WWW.JPURPLEMAN.CA/WCOTTAWA2016

×