Created
January 17, 2014 17:50
-
-
Save petenelson/8478056 to your computer and use it in GitHub Desktop.
WordPress: Simple bulk page importer
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 | |
/* | |
Plugin Name: GGA Import Pages | |
Description: Quick plugin to bulk import a list of pages <a href="admin-ajax.php?action=gga_import_pages">[Import Now]</a> | |
*/ | |
add_action( 'wp_ajax_gga_import_pages', 'gga_import_pages' ); | |
function gga_import_pages() { | |
global $post; | |
$results = new stdClass(); | |
$results->new_pages = array(); | |
$results->updated_pages = array(); | |
// read list of pages | |
$filedata = explode("\n", file_get_contents (plugin_dir_path( __FILE__ ) . 'pagelist.txt')); | |
foreach ($filedata as $line) { | |
if ('' === trim($line)) | |
continue; | |
$pagedata = explode('|', $line); | |
$title = trim($pagedata[0]); | |
$slug = trim($pagedata[1]); | |
$author = 4; | |
$status = 'publish'; | |
$post_type = 'page'; | |
$args = array( | |
'pagename' => $slug, | |
'post_type' => $post_type, | |
'post_status' => 'any', | |
); | |
$query = new WP_Query( $args ); | |
if (!$query->have_posts()) { | |
$post_id = wp_insert_post( array( | |
'post_name' => wp_unique_post_slug( sanitize_title( $slug ), $post->ID, $status, $post_type, 0 ), | |
'post_title' => $title, | |
'post_status' => $status, | |
'post_type' => 'page', | |
'post_author' => $author, | |
)); | |
$results->new_pages[] = get_permalink($post_id); | |
} | |
else { | |
$query->the_post(); | |
wp_update_post(array( | |
'ID' => $post->ID, | |
'post_name' => wp_unique_post_slug( sanitize_title( $slug ), $post->ID, $status, $post_type, 0 ), | |
'post_title' => $title, | |
'post_status' => $status, | |
'post_type' => $post_type, | |
'post_author' => $author, | |
)); | |
$results->updated_pages[] = get_permalink($post->ID); | |
} | |
wp_reset_query(); | |
} | |
echo json_encode($results); | |
die(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment