Skip to content

Instantly share code, notes, and snippets.

@amallory
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amallory/5dba85fda65b9587fda7 to your computer and use it in GitHub Desktop.
Save amallory/5dba85fda65b9587fda7 to your computer and use it in GitHub Desktop.
Import data "template" for wp cli, for importing into WordPress
<?php
/**
* /inc/class-migration-cli.php'
*/
class Migration_CLI extends WP_CLI_Command {
/**
* Imports data.
*
* ## OPTIONS
*
* ## EXAMPLES
*
* wp --require=mallory_import.php import
*
* @synopsis
*/
public function migrate_data( '$args, $assoc_args ) {
$this->connect_to_import_source();
// `has_import_data()` returns true if there's more to process
while ( $this->has_import_data() ) {
// Extract:
// `get_import_data()` gets next, and increments counter
// This can also do a lot of your heavy lifting in extraction
$row = $this->get_import_post();
$post = array(
'post_type' => 'post',
'post_title' => $row['title'],
'post_content' => $row['content'],
'post_date' => date( 'Y-m-d H;i:s', strtotime( $row['date'] ) )
);
// Transform:
if ( $row['is_slidewshow'] ) {
$post['post_type'] = 'slideshow';
}
// Load:
/*if ( $post_id = $this->new_post_exists( $row['id'] ) ) {
$post['ID'] = $post_id;
wp_update_post( $post );
} else {
$post_id = wp_insert_post( $post );
}*/
$post_id = wp_insert_post( $post );
update_post_meta( $post_id, 'import_id', $row['id'] );
if ( $row['is_slideshow'] ) {
update_post_meta( $post_id, 'slides', $this->get_import_slides( … );
}
}
}
public function connect_to_import_source() {
}
public function has_import_data() {
return true;
}
public function get_import_data() {
}
}
WP_CLI::add_command( 'import', 'Migration_CLI' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment