Skip to content

Instantly share code, notes, and snippets.

@rocketgeek
Created October 27, 2020 14:51
Show Gist options
  • Save rocketgeek/bf21d328ad35b607aae82dc0896ed559 to your computer and use it in GitHub Desktop.
Save rocketgeek/bf21d328ad35b607aae82dc0896ed559 to your computer and use it in GitHub Desktop.
Simple framework for running WP imports via command line
<?php
/**
* A framework for using the command line for big
* WordPress import projects.
*/
// Identify that script must run from command line.
if (php_sapi_name() !== 'cli') {
die("This script is meant to be run from the command line");
}
// Prevent script from timing out.
set_time_limit(0);
ini_set('max_execution_time', 0);
/**
* Gets the basepath for WordPress.
*/
function find_wordpress_base_path() {
$dir = dirname(__FILE__);
do {
if( file_exists($dir."/wp-config.php") ) {
return $dir;
}
} while($dir = realpath("$dir/.."));
return null;
}
// Initialize WordPress.
define('BASE_PATH', find_wordpress_base_path()."/");
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header, $wpdb;
require(BASE_PATH . 'wp-load.php');
/*
* You're all set at this point. You can run queries and WordPress
* functions. Echo any required output to the command line screen.
*/
echo "\r\n";
echo "************************************************************\r\n";
echo "BEGIN NEW RUN \r\n";
echo "************************************************************\r\n";
echo "\r\n";
// Your processing can go here.
// Add any necessary globals (already included $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header, $wpdb)
//fclose( $log );
echo "\r\n";
echo "Done!";
echo "\r\n";
echo "\r\n";
// The end...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment