Last active
January 30, 2019 04:00
-
-
Save trvswgnr/8b4ea1855f31c4a3e592a3f49b3ba083 to your computer and use it in GitHub Desktop.
Programmatically Generate WordPress Pages
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 | |
/** | |
* Generate WordPress Pages | |
* @example: taw_generate_pages('page one', 'Page Two', 'page-three'); | |
*/ | |
function taw_generate_pages($names) { | |
if (!$names) { return false; } | |
$list = func_get_args(); | |
foreach ($list as $item) { | |
$slug = str_replace(' ', '-', strtolower($item)); | |
$title = ucwords(str_replace('-', ' ', $item)); | |
$exists = taw_slug_exists($slug); | |
$new_post = array( | |
'post_type' => 'page', | |
'post_title' => $title, | |
'post_content' => '', | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_slug' => $slug | |
); | |
// insert the post into the database if it doesnt already exist | |
if (!$exists && is_admin()) { | |
wp_insert_post( $new_post ); | |
} | |
} | |
return false; | |
} | |
/** check if slug exists */ | |
function taw_slug_exists($post_slug) { | |
global $wpdb; | |
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_slug . "'", 'ARRAY_A')) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment