Last active
June 6, 2019 12:45
-
-
Save benoitmercusot/752d86e292e3202ccce16984725612f7 to your computer and use it in GitHub Desktop.
Dossier txt > post WordPress
This file contains 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 | |
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