Loop Through An Array

Today I needed to loop through the output of a command on the WP CLI. The most simple way I could think to do that was to save the output of the command into an array and loop through it.

What I did was use wp site list to get the subsites on a WordPress Multisite. I needed to pass each of those sites as an argument to a command and give it some time in between each execution since it was going to be database intensive. Here is what worked out well.

#!/bin/bash

subsites=($(wp site list --field=url))

for i in "${subsites[@]}"
do
wp my_command --url="$i"
sleep 5
done

Save the above into a file.

  1. vi sites.sh
  2. Add code and then save. :wq
  3. chmod and make executable chmod 766 sites.sh
  4. Execute it ./sites.sh | tee site.log

Update

Some of the amazing devs at work helped to modify the loop a bit to make it better for logging.

!/bin/bash

sites=($(wp site list --field=url --path=/var/www))
for i in "${sites[@]}"
do
    domain=$(echo "$i" | awk -F/ '{print $3}')
    wp my_command --url="$i" | tee /path/to/file-"$domain".log
done

Detect Cache Version 1.1

Today I updated the plugin I wrote almost a year ago, bringing to to version 1.1. It’s not a huge accomplishment but I am proud of it. The plugin is used by several of my co-workers and it’s pretty cool that it’s useful enough to continue adding to it. I came across an http header that allows me to guess a little bit more about the proxy or load balancer in use, so I added detection for that.

WordPress: Adding custom fields

Every day that I get a CrossFit workout in, I like to record that on my site, Level Up CrossFit. But that is a time consuming task that I have less and less time for. I don’t want to stop doing that, so I decided to add some custom fields so that entry is easier and formatting is all done.

Enter Advanced Custom Fields.

This plugin makes it very simple. Just add the custom fields in the Admin and then edit your templates to display.

Here is what my extra fields look like in Advanced Custom Fields.

And this is what that looks like when Adding or Editing a post.

This is the simple PHP I added to content-single.php in the theme I am using.

    <?php echo "<h3>MetCon</h3>" ?>
    <?php if( get_field('metcon') ): ?>
    <p><?php the_field('metcon'); ?></p>
    <?php endif; ?>
    <?php echo "<h3>Strength</h3>" ?>
    <?php if( get_field('front_squats') ): ?>
    <p>Front Squats: <?php the_field('front_squats'); ?></p>
    <?php endif; ?>
    <?php if( get_field('strict_push_press') ): ?>
    <p>Strict Push Press: <?php the_field('strict_push_press'); ?></p>
    <?php endif; ?>
    <?php if( get_field('back_squats') ): ?>
    <p>Back Squats: <?php the_field('back_squats'); ?></p>
    <?php endif; ?>
    <?php if( get_field('dead_lifts') ): ?>
    <p>Dead Lifts: <?php the_field('dead_lifts'); ?></p>
    <?php endif; ?>
    <?php if( get_field('bench_press') ): ?>
    <p>Bench Press: <?php the_field('bench_press'); ?></p>
    <?php endif; ?>
    <?php if( get_field('barbell_rows') ): ?>
    <p>Barbell Rows: <?php the_field('barbell_rows'); ?></p>
    <?php endif; ?>
    <?php echo "<h3>WOD Results</h3>" ?>
    <?php if( get_field('wod_rx') ): ?>
    <p>RX: <?php the_field('wod_rx'); ?></p>
    <?php endif; ?>
    <?php if( get_field('wod_time') ): ?>
    <p>Time: <?php the_field('wod_time'); ?></p>
    <?php endif; ?>

The finished result looks like this.