wp_write_post

Creates a new post from the "Write Post" form using $_POST information.

Description

(int|WP_Error) wp_write_post(); 

Returns (int|WP_Error)


Usage

  1. if ( !function_exists( 'wp_write_post' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_write_post(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/post.php  
  1. function wp_write_post() { 
  2. if ( isset($_POST['post_type']) ) 
  3. $ptype = get_post_type_object($_POST['post_type']); 
  4. else 
  5. $ptype = get_post_type_object('post'); 
  6.  
  7. if ( !current_user_can( $ptype->cap->edit_posts ) ) { 
  8. if ( 'page' == $ptype->name ) 
  9. return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) ); 
  10. else 
  11. return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) ); 
  12.  
  13. $_POST['post_mime_type'] = ''; 
  14.  
  15. // Clear out any data in internal vars. 
  16. unset( $_POST['filter'] ); 
  17.  
  18. // Edit don't write if we have a post id. 
  19. if ( isset( $_POST['post_ID'] ) ) 
  20. return edit_post(); 
  21.  
  22. if ( isset($_POST['visibility']) ) { 
  23. switch ( $_POST['visibility'] ) { 
  24. case 'public' : 
  25. $_POST['post_password'] = ''; 
  26. break; 
  27. case 'password' : 
  28. unset( $_POST['sticky'] ); 
  29. break; 
  30. case 'private' : 
  31. $_POST['post_status'] = 'private'; 
  32. $_POST['post_password'] = ''; 
  33. unset( $_POST['sticky'] ); 
  34. break; 
  35.  
  36. $translated = _wp_translate_postdata( false ); 
  37. if ( is_wp_error($translated) ) 
  38. return $translated; 
  39.  
  40. // Create the post. 
  41. $post_ID = wp_insert_post( $_POST ); 
  42. if ( is_wp_error( $post_ID ) ) 
  43. return $post_ID; 
  44.  
  45. if ( empty($post_ID) ) 
  46. return 0; 
  47.  
  48. add_meta( $post_ID ); 
  49.  
  50. add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID ); 
  51.  
  52. // Now that we have an ID we can fix any attachment anchor hrefs 
  53. _fix_attachment_links( $post_ID ); 
  54.  
  55. wp_set_post_lock( $post_ID ); 
  56.  
  57. return $post_ID;