/*
A function used to programmatically create a post in WordPress.
@returns -1 if the post was never created, -2 if a post with the same title exists, or the ID of the post if successful.
*/
function programmatically_create_post($user_id) {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
// Setup the title of the post
$title = 'My New Post'
// If the page doesn't already exist, then create it
if( null == get_page_by_title($title)) {
// Set the post ID so that we know the post was created successfully
$new_post = array(
'post_title' => $title,
'post_name' => my-new-post,
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('j.n.Y H:i:s'),
'post_author' => $user_id,
'post_type' => 'post',
'post_category' => array(0),
'comment_status' => 'closed',
'ping_status' => 'closed',
);
$post_id = wp_insert_post($new_post);
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
} // end programmatically_create_post
Programmatically Create a Post in WordPress »
„How to Create a Post in WordPress Programmatically »“:How to Create a Post in WordPress Programmatically
To lze využít např. pro automatické vytvoření příspěvku z uživatelského profilu při jeho ukládání přes user_register hook
add_action('user_register','programmatically_create_post');
