Skip to content

Instantly share code, notes, and snippets.

@jimfloss
Created January 30, 2018 17:40
Show Gist options
  • Save jimfloss/76cb2a418fb19c4b196e64c14abf600f to your computer and use it in GitHub Desktop.
Save jimfloss/76cb2a418fb19c4b196e64c14abf600f to your computer and use it in GitHub Desktop.
Programmatically add WP pages or posts
<?php
//Create Page
function create_page() {
if ( !$post = get_page_by_path( 'product-configurator', OBJECT, 'post_type' ) ) {
$user_id = get_current_user_id();
$page = array(
'post_title' => 'Product Configurator',
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'page',
);
$page_exists = get_page_by_title( $page['post_title'] );
if( $page_exists == null ) {
// Page doesn't exist, so lets add it
$insert = wp_insert_post( $page );
if( $insert ) {
// Page was inserted ($insert = new page's ID)
}
}
}
}
add_action( 'wp_loaded', 'create_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment