Created
October 27, 2020 14:51
-
-
Save rocketgeek/bf21d328ad35b607aae82dc0896ed559 to your computer and use it in GitHub Desktop.
Simple framework for running WP imports via command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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