Wordpress automatic updates

Wordpress automatic updates

By devin, 1 March, 2018

I maintain a couple of wordpress sites, and I rarely want to spend time keeping them up to date. I use this permissions script https://www.devinhoward.ca/technology/2014/mar/wordpress-permissions-sc… though, which disallows Wordpress's normal auto-update mechanism. This vastly increases the security, which is worth it, but I need another way to auto-update.

So I use this script, along with the excellent command line tool wp-cli: https://wp-cli.org

Here is the script:

#!/bin/bash

WP_ROOT="$1" # e.g. /home/devinhoward/wordpress
WEBSITE="$2" # e.g. https://wordpress.devinhoward.ca
WP_CMD=/usr/local/bin/wp

cd $WP_ROOT
$WP_CMD core check-update | grep -v 'WordPress is at the latest version'
$WP_CMD plugin status | grep '^ U'
$WP_CMD theme status | grep '^ U'

CHANGED=''

CORE_OUTPUT=$($WP_CMD core check-update | grep -v 'WordPress is at the latest version')
if [[ -n "$CORE_OUTPUT" ]]; then
  CORE_UP_OUTPUT=$($WP_CMD core update | grep -v 'Success: WordPress is up to date.')
  echo $CORE_UP_OUTPUT
  git add -A .
  git commit -m "$(echo "$CORE_UP_OUTPUT" | head -n1)"
  CHANGED=changed
fi

PLUGIN_OUTPUT=$($WP_CMD plugin status | grep '^ U')
if [[ -n "$PLUGIN_OUTPUT" ]]; then
  $WP_CMD plugin update --all | grep -v 'Success: Updated 0/0 plugins.'
  git add -A .
  git commit -m " $PLUGIN_OUTPUT"
  CHANGED=changed
fi

#
# Theme updates are too scary, otherwise you could add a section here.
#

# create a textual verification that the website actually still renders
if [[ -n "$CHANGED" ]]; then
  COMMAND="pandoc -f html -t markdown $WEBSITE"
  echo "$COMMAND yields: "
  $COMMAND
fi

 

Some notes:

  • I keep my wordpress websites under version control via git. You can remove the four lines that use git if you don't do this.
  • The last section uses pandoc to verify the site still renders. I have never experienced my site not rendering, but when cron sends me an email it's a nice reassurance. If you don't have pandoc, you can remove this section as well as the "CHANGED" variable.

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.