Pre-populate WordPress posts

This post is about how to pre-fill some fields when opening the Add new posts form (/wp-admin/post-new.php). It’s straight forward for some fields like title and post type but more difficult for other fields. Actually there is no central resource for this and meanwhile I’m still thinking where to include it on WordPress codex, I started to document it here.

Fields to pre-fill with GET variables

Post type, title, content and excerpt can directly be pre-populated passing them as GET variables when calling the Add new post form:

/wp-admin/post-new.php?post_title=My Title&post_type=post&excerpt=Short excerpt

That’s because an empty post is created by get_default_post_to_edit() as this post explains in more details: This function reads the post_title, content and excerpt values in the $_REQUEST array, and also filters them through default_title, default_content and default_excerpt.

Better than passing the whole content as GET variable is is to apply the default_content filter. My use case was to copy a yet existing post so I passed a reference to the parent post ID as GET variable named post_parent:

<br />
add_filter( 'default_content', 'content_by_request', 10, 2 );</p>
<p>function content_by_request( $content, $post ) {<br />
	//set content<br />
	if (!empty( $_GET['post_parent'] )) {<br />
		$parent_id = $_GET['post_parent'];<br />
		$content = get_post($parent_id)-&gt;post_content;<br />
		return($content);<br />
	}<br />
    return $content;<br />
}<br />

Fields to pre-fill with actions and filters

All the other fields need to be read from the $_REQUEST array, a typical example would be passing them as GET variables. The following code shows how to pre-fill category, tags, custom fields and featured image:

<br />
add_action( 'load-post-new.php', 'myplugin_post_new' );<br />
function myplugin_post_new(){<br />
	add_action( 'wp_insert_post', 'myplugin_wp_insert_post_default' );<br />
}<br />
function myplugin_wp_insert_post_default( $post_id ){<br />
	//set category<br />
	$cat_id = $_REQUEST['tax-input']['event-category'];<br />
	wp_set_post_terms( $post_id, $cat_id, 'event-category' );</p>
<p>	//set tags<br />
	wp_set_post_tags( $post_id, $_REQUEST['tags']);</p>
<p>	//set custom field<br />
	add_post_meta( $post_id, 'myplugin_meta_key', $_REQUEST['meta_value'] );</p>
<p>	//set thumbnail and use the one from parent post<br />
	if (!empty( $_GET['post_parent'] )) {<br />
		$parent_id = $_GET['post_parent'];<br />
		$attached_image = get_children( &quot;post_parent=&quot;.$parent_id.&quot;&amp;post_type=attachment&amp;post_mime_type=image&amp;numberposts=1&quot; );<br />
		if ($attached_image) {<br />
			foreach ($attached_image as $attachment_id =&gt; $attachment) {<br />
				set_post_thumbnail($post_id, $attachment_id);<br />
			}<br />
		}<br />
	}<br />
}</p>
<p>

Pre-fill author using wp_insert_post_data filter

One way to pre-populate author field is to pass the author ID as GET variable to the Add new post form:

/wp-admin/post-new.php?author=1

Then you read the GET variable and apply wp_insert_post_data filter:

<br />
function myplugin_wp_set_post_author( $data , $postarr ){<br />
	if (!empty( $_GET['author'] )) {<br />
		$author = $_GET['author'];<br />
	}<br />
	else {<br />
		$author = 1;<br />
	}<br />
	$data['post_author'] = $author;<br />
	return $data;<br />
}<br />
add_filter('wp_insert_post_data' , 'myplugin_wp_set_post_author' , '99', 2);<br />

Plugin fields to pre-fill

Normally the best method is to pass the parameters as GET variable and pre-populate the fields with a plugin filter.

One example I needed recently is to prefill Event Organiser start and end date and time. This plugin offers the filter eventorganiser_get_event_schedule which (thanks to this post) can be used in the folloging way:

<br />
add_filter('eventorganiser_get_event_schedule', 'myplugin_prefill_date', 10, 2);</p>
<p>function myplugin_prefill_date($event_details, $post_id) {</p>
<p>	if ( !isset($_GET) || count($_GET) === 1 ) return $event_details;</p>
<p>	if ( isset($_GET['start']) ) {<br />
		$hm = explode(&quot;-&quot;, $_GET['start'] );<br />
		date_date_set($event_details['start'], $hm[2], $hm[1], $hm[0]);<br />
	}</p>
<p>	if ( isset($_GET['end']) ) {<br />
		$hm = explode(&quot;-&quot;, $_GET['end'] );<br />
		date_date_set($event_details['end'], $hm[2], $hm[1], $hm[0]);<br />
	}</p>
<p>	//set standart time<br />
	date_time_set($event_details['start'], 0, 0);<br />
	date_time_set($event_details['end'], 0, 0);</p>
<p>	return $event_details;<br />
}<br />

Tags: ,

Leave a Reply