Skip to content

Instantly share code, notes, and snippets.

@benoitmercusot
Last active June 6, 2019 12:45
Show Gist options
  • Save benoitmercusot/752d86e292e3202ccce16984725612f7 to your computer and use it in GitHub Desktop.
Save benoitmercusot/752d86e292e3202ccce16984725612f7 to your computer and use it in GitHub Desktop.
Dossier txt > post WordPress
<?php
define( 'WP_USE_THEMES', false );
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
require_once ABSPATH . '/wp-admin/includes/post.php';
header("HTTP/1.1 200 OK");
$directory = dirname(__FILE__)."/txt";
$iterator = new DirectoryIterator($directory);
foreach ($iterator as $fileinfo) {
if ( $fileinfo->isFile() && $fileinfo->getExtension() === 'txt' ) {
$mypost = [
'post_title' => trim( str_replace('.txt','', $fileinfo->getFilename() ) ),
'post_content' => file_get_contents( $directory.'/'.$fileinfo->getFilename() ),
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post'
];
if( !post_exists($mypost['post_title'], $mypost['post_content']) )
var_dump( wp_insert_post( $mypost ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment