As of late, I’ve found myself doing quite a few things via the command line with WP-CLI. If you’ve never used it before, it’s an extremely powerful command line interface for interacting with your WordPress installation. It comes bundled with some great utilities to help you out, including a WPCLI progress bar, but we’ll get to that in a minute.

Where it becomes extremely powerful is with tasks that can cause a web based interface to timeout or reach the max execution times. I have found it very helpful when running migration, import, and export scripts. If you’ve ever executed a long running task in the command line, you’ve felt the pain of thinking:

Is it still going? Did it break? What’s happening?

Enter…the progress bar

WP-CLI has an awesome built in method that allows you to output a progress bar, complete with the percentages, running time, and estimated time to completion.

The code to make this happen is surprisingly simple. It consists of 3 lines:

First you define what the text will be in front of the progress bar, and the total number of items to complete.
$progress = \WP_CLI\Utils\make_progress_bar( 'Progress Bar', $total );

Next, during each step of your loop, you tick the progress bar.
$progress->tick();

After your loop is complete, you finish the progress bar
$progress->finish();

Example Plugin

 

Posted by Chris Klosowski

Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.

6 Comments

  1. Hey Chris,

    Thanks for the post. Are you using Phpstorm IDE? do you know how to remove WP_CLI Undefined class error from IDE?

    Reply

  2. If you’ve ever executed a long running task in the command line, you’ve felt the pain of thinking:
    Is it still going?

    Reply

  3. If you’ve never used it before, it’s an extremely powerful command line interface for interacting with your WordPress installation.

    Reply

  4. Thanks for ones marvelous posting! I certainly enjoyed reading it, you happen to be a great
    author. I will always bookmark your blog and will come back
    from now on. I want to encourage continue your great
    work, have a nice morning!

    Reply

  5. I’m really enjoying the design and layout of your site.
    It’s a very easy on the eyes which makes it much more enjoyable for me to come here
    and visit more often. Did you hire out a developer to create your theme?
    Exceptional work!

    Reply

  6. Hello there! I know this is kinda off topic but I was wondering
    if you knew where I could locate a captcha plugin for
    my comment form? I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *