1

I have a Ubuntu 16.04 Nginx environment with a few minimal WordPress sites (virtually all with up to 5 conventional plugins, 10 pages, 10 images, and a simple contact form to send only textual data).

I daily execute the script cron_daily.sh with the following three loops, from crontab, to update all WordPress apps under document root. The script uses the WP-CLI shell extension.

for dir in ${drt}/*/; do cd ${dir} && wp plugin update --all --allow-root; done
for dir in ${drt}/*/; do cd ${dir} && wp core update --allow-root; done
for dir in ${drt}/*/; do cd ${dir} && wp theme update --all --allow-root; done

${drt} is document root. It was already declared outside permanently, with its file sourced.


I was looking for a way to unite the behavior of these three loops into one segment.

This pattern seems promising, and is based on this example:

for dir in ${drt}/*/; do
    if pushd ${dir}; then
        wp plugin update --all --allow-root
        wp core update --allow-root
        wp theme update --all --allow-root
        popd
    fi
done

Is this the shortest pattern one could use? How would you do that?

1

Why three times the same loop instead of one loop like in your example?

At first glance I don't see how this could get any shorter — nor why it should.

If anything, the script could get better (and thus longer) by improving the detection of WordPress (if needed). Also, I'd probably run wp language core update as well to make sure translations are up-to-date.

  • Thx. Improving the detection of WordPress? – user9303970 Feb 8 '18 at 14:10
  • I now understand you probably meant to wp core is-installed. – user9303970 Feb 8 '18 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.