Created
January 30, 2018 17:40
-
-
Save jimfloss/76cb2a418fb19c4b196e64c14abf600f to your computer and use it in GitHub Desktop.
Programmatically add WP pages or posts
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 | |
//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