Skip to content

Instantly share code, notes, and snippets.

Last active November 11, 2023 17:19
Show Gist options
  • Save skorasaurus/8deb8535069a0cec37322e9d5f71c6f3 to your computer and use it in GitHub Desktop.
Save skorasaurus/8deb8535069a0cec37322e9d5f71c6f3 to your computer and use it in GitHub Desktop.
Manage WordPress via WP-CLI with these Shortcuts/scripts/functions/aliases.

update text inside a post (we work with 3rd party vendors, their urls occasionally change; or we changed a campaign's name used in google analytics) and limit it to ONLY published posts. (sometimes, our marketing team wants to what see how content was EXACTLY in the past). wp-cli's search-replace command by default make edits in revisions.

(in this command, 'before' is what you want to get rid of and 'after' is what you want to replace 'before' with.

wp db query "UPDATE $(wp db tables "*_posts") SET post_content = REPLACE(post_content, 'before', 'after') WHERE post_status = 'publish';"

now available as one command (thanks to Alain Schlesser!)

Note the command is CASE-SENSITIVE.

Commands: These are all unix-based; (I put these into my .bashrc of ~/ my dev environments and then run source ~/.bashrc to ensure they are used) (for reference, in unix, $1 is the first argument)

  • Install a plugin and immediately activate it:

plugme() { wp plugin install $1 --activate }

For example plugme classic-editor would immediately install the plugin and activate it on your site.

  • Change the activation status of an plugin in a wordpress installation. Very helpful for troubleshooting and want to determine if a behaviour is repeatable when a plugin is active or inactive. e.g. deplugme query-monitor will deactivate an activated/active query-monitor plugin type it once more; and the plugin will be reactivated.

deplugme() { wp plugin toggle $1 }

  • Deactivate and uninstall an install plugin (helpful when I want to get rid of a plugin after I try it out, dislike it, want to get rid of it) unplugme() { wp plugin uninstall $1 --deactivate }

  • switch to another theme:

totheme() { wp theme activate $1 }

  • delete and trash a post (argument is the post_id)

trash() { wp post delete $1 --force }

  • empty the trash, specify the post_type b/c if you don't, wp-cli will only delete post types 'posts'

trashempty() { wp post delete $(wp post list --post_type=page,post,your_custom_post_type --post_status=trash --format=ids) }

#not quite wp-cli but very handy:

find all posts/pages on your WP install that do not contain any WordPress blocks;

select post_name, post_type, id from wp_posts WHERE post_content NOT LIKE '%<!-- wp:%' AND post_status = 'publish' AND post_type in ('post','page');

# save as
# then chmod+x
# daily backup of your database.
# assumes you have shell access to your website and that your wp installation is in ~/public_html
# names dates as ISO 8601;
# e.g. 2018-12-01 is December 01, 2018
today="$(date +%Y-%m-%d)"
echo "today" ${today}
# note, if you use cpanel, you may need to specify your paths to PHP and to the wp-cli executable
# ask me for more details if you need them
cd $HOME/public_html && wp db export ../sql_backup/${today}-production.sql && cd ../sql_backup && tar -cvzf ${today}-production.sql.tar.gz ${today}-production.sql && rm -f ${today}-production.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment