Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( php_sapi_name() !== 'cli' || isset( $_SERVER['REMOTE_ADDR'] ) ) {
- die( 'CLI Only' );
- }
- // Get first arg
- if ( ! isset( $argv ) || count( $argv ) < 3 ) {
- echo "Missing parameters.\n";
- echo "script usage: php bea-insert-terms.php [taxonomy](required, must be already registered) [terms](required, list of terms to be inserted comma separated) \n";
- die();
- }
- //Domain
- $domain = ( isset( $argv[1] ) ) ? $argv[1] : '';
- $taxonomy = ( isset( $argv[2] ) ) ? $argv[2] : '';
- $terms = ( isset( $argv[3] ) ) ? explode( ",", $argv[3] ) : array();
- // Fake WordPress, build server array
- $_SERVER = array(
- 'HTTP_HOST' => $domain,
- 'SERVER_NAME' => $domain,
- 'REQUEST_URI' => '',
- 'REQUEST_METHOD' => 'GET',
- 'SCRIPT_NAME' => basename( __FILE__ ),
- 'SCRIPT_FILENAME' => basename( __FILE__ ),
- 'PHP_SELF' => basename( __FILE__ )
- );
- // Load WordPress
- $bootstrap = 'wp-load.php';
- while ( ! is_file( $bootstrap ) ) {
- if ( is_dir( '..' ) ) {
- chdir( '..' );
- } else {
- die( 'EN: Could not find WordPress! FR : Impossible de trouver WordPress !' );
- }
- }
- require_once( $bootstrap );
- /**
- * Class BEA_Insert_Terms
- * The purpose is to pass a list of terms to insert in specified taxonomy
- */
- class BEA_Insert_Terms {
- /**
- * Insert terms dynamically
- *
- * @author Julien Maury
- * @param string $taxonomy targeted taxonomy
- * @param array $terms list of terms to be inserted
- * @return void
- */
- public static function insert_terms( $taxonomy, $terms = array() ) {
- if ( ! is_string( $taxonomy ) ) {
- echo sprintf( 'This parameter should be a string please !' ) . PHP_EOL;
- exit( 1 );
- }
- if ( ! taxonomy_exists( $taxonomy ) ) {
- echo sprintf( 'This taxonomy is not even registered here, break your legs !' ) . PHP_EOL;
- exit( 1 );
- }
- $i = 0;
- foreach ( $terms as $term ) {
- $i ++;
- $insert = wp_insert_term( $term, $taxonomy );
- echo is_wp_error( $insert ) ? sprintf( '%s was not inserted : %s', $term, $insert->get_error_message() ) . PHP_EOL : sprintf( '%s inserted successfully :)', $term ) . PHP_EOL;
- }
- echo sprintf( '%d terms inserted in %s', $i, $taxonomy ) . PHP_EOL;
- exit( 0 );
- }
- }
- BEA_Insert_Terms::insert_terms( $taxonomy, $terms );
Advertisement
Add Comment
Please, Sign In to add comment