wp_write_post

Definition:
function wp_write_post() {}

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

Source code

function wp_write_post() {

	global $user_ID;





	if ( isset($_POST['post_type']) )

		$ptype = get_post_type_object($_POST['post_type']);

	else

		$ptype = get_post_type_object('post');



	if ( !current_user_can( $ptype->cap->edit_posts ) ) {

		if ( 'page' == $ptype->name )

			return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );

		else

			return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );

	}



	$_POST['post_mime_type'] = '';



	// Clear out any data in internal vars.

	unset( $_POST['filter'] );



	// Check for autosave collisions

	// Does this need to be updated? ~ Mark

	$temp_id = false;

	if ( isset($_POST['temp_ID']) ) {

		$temp_id = (int) $_POST['temp_ID'];

		if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )

			$draft_ids = array();

		foreach ( $draft_ids as $temp => $real )

			if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )

				unset($draft_ids[$temp]);



		if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write

			$_POST['post_ID'] = $draft_ids[$temp_id];

			unset($_POST['temp_ID']);

			update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );

			return edit_post();

		}

	}



	// Edit don't write if we have a post id.

	if ( isset( $_POST['ID'] ) ) {

		$_POST['post_ID'] = $_POST['ID'];

		unset ( $_POST['ID'] );

	}

	if ( isset( $_POST['post_ID'] ) ) {

		return edit_post();

	}



	$translated = _wp_translate_postdata( false );

	if ( is_wp_error($translated) )

		return $translated;



	if ( isset($_POST['visibility']) ) {

		switch ( $_POST['visibility'] ) {

			case 'public' :

				$_POST['post_password'] = '';

				break;

			case 'password' :

				unset( $_POST['sticky'] );

				break;

			case 'private' :

				$_POST['post_status'] = 'private';

				$_POST['post_password'] = '';

				unset( $_POST['sticky'] );

				break;

		}

	}



	// Create the post.

	$post_ID = wp_insert_post( $_POST );

	if ( is_wp_error( $post_ID ) )

		return $post_ID;



	if ( empty($post_ID) )

		return 0;



	add_meta( $post_ID );



	add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );



	// Reunite any orphaned attachments with their parent

	// Does this need to be udpated? ~ Mark

	if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )

		$draft_ids = array();

	if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )

		_relocate_children( $draft_temp_id, $post_ID );

	if ( $temp_id && $temp_id != $draft_temp_id )

		_relocate_children( $temp_id, $post_ID );



	// Update autosave collision detection

	if ( $temp_id ) {

		$draft_ids[$temp_id] = $post_ID;

		update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );

	}



	// Now that we have an ID we can fix any attachment anchor hrefs

	_fix_attachment_links( $post_ID );



	wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );



	return $post_ID;

}

4277

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: