Making a custom WP-CLI command for Gravity Forms exports


(Leland Fiegel) #1

So here’s my issue. I run sites that have a ton of Gravity Forms entries. Like tens of thousands. These need to be exported periodically so they can be analyzed further, and to clean out the database.

Since the export files are so large, using Gravity Forms’ normal web-based export function isn’t really an option because it always times out long before it has a chance to finish.

My current (really inefficient process) is this:

  1. Dump the entire database.
  2. Import the database locally.
  3. Run a custom SQL query to extract the entry data I want, corresponding to a particular form and its fields.
  4. Export that as a CSV (essentially containing exactly what a GF export file would contain) and pass it on.

I know doing this over the CLI would be way more efficient, I’m just unsure the best way of going about it, even after browsing through the code of community commands.

Has anyone ever made a Gravity Forms export WP-CLI plugin? And if not, and if you had to, how would you go about it?


(Tim Nash) #2

So the community commands, are in a state of flux currently, so my quick suggestion is don’t go this route, instead build it out as a plugin.

Building out a plugin that uses WP-CLI is pretty simple as in as simple as creating a class that extends WP_CLI_COMMAND with your commands in it and then add an action. I did a quick video https://timnash.co.uk/extending-wp-cli/ late last year which might help.

So here is how I would do it, well it will need to be a little more robust (note I haven’t tested this code so do check for errors :slight_smile: )

<?php
class Gravity_Command extends WP_CLI_Command {

    function export( $args, $assoc_args ) {
        $formatter = $this->get_formatter( $assoc_args );
        //Put your Query or call Gravity forms query here

        $formatter->display_items( $queryobject );
    }
}

WP_CLI::add_command( 'gravity', 'Example_Command' );

Then call it with:

gravity export --format=csv > mycsv.csv

(Leland Fiegel) #3

Thanks Tim!

I know this thread is mega old but wanted to update it because the Gravity Forms team have since released their own CLI addon.

Looks like with that, exporting entries can be as simple as typing “wp gf export” followed by the form ID.