这里我假设您的json是一个对象数组,其中的属性命名如下wp_insert_post
arguments: \'post\\u标题、“post\\u内容”等。
function process_ajax() {
if ( ! isset($_POST[\'nonce\']) || ! wp_verify_nonce($_POST[\'nonce\'], \'mynonce\') )
die(\'error on checking nonce\');
if ( ! isset($_POST[\'filepath\']) die(\'no file given\');
if ( ! file_exists($_POST[\'filepath\']) ) die(\'invalid file given\');
$posts = json_decode( file_get_contents($_POST[\'filepath\']) );
$done = 0;
if ($posts) {
foreach ( $posts as $post) {
$post = (array)$post;
if ( isset($post[\'ID\']) ) unset($post[\'ID\']);
if ( wp_insert_post($post) ) $done++;
}
$str = "Successfully insert " . $done . "posts, ";
$str .= ( count($posts) - $done ) . \' failed.\'
die($str);
} else {
die(\'File contains not valid Json.\');
}
}