Create a new Page

If you want your Theme or Plugin to create a new page (or two or three) on its own, here is some quick code to do it. Just put this into an activation hook, and you’ll be good to go.

wp_insert_post returns the ID of the newly-created page. In the event of an error, it will return 0 if $wp_error is set to false, or a WP_Error object if $wp_error is set to true.

And of course, this code could be used to create a POST or any other type of Custom Post Type content, just by changing the value for “post_type”.

Code Snippet:

$new_page = array(
	'slug' => 'this-is-my-new-page',
	'title' => 'Write a Headline that Captivates',
	'content' => "Enter the body Content for your Page here"
$new_page_id = wp_insert_post( array(
	'post_title' => $new_page['title'],
	'post_type' 	=> 'page',
	'post_name'	 => $new_page['slug'],
	'comment_status' => 'closed',
	'ping_status' => 'closed',
	'post_content' => $new_page['content'],
	'post_status' => 'publish',
	'post_author' => 1,
	'menu_order' => 0

Posted in Pages