User-Submitted Posts in WordPress

Recently, one of our clients needed to let visitors create WordPress posts (Or custom post types of course) from the website itself.

In this post, I’m going to walk through some of the WordPress functions I used to achieve this. We’ll go over creating the post itself, adding categories/tags, and adding metadata.

(We’ll skip the stage where you build a form, validate what users enter etc. and just focus on the WordPress functions instead)

Create the Post

The method to create the core post is simple. Essentially, you build a $post object, and then insert it with wp_insert_post().

You can use our $post reference to speed this up. Let’s take a look at some examples:

1
2
3
4
5
6
$new_post = array(
	'post_title' => 'Our New Post',
	'post_content' => 'Our filler text for the post.'
);
 
$post_id = wp_insert_post($new_post);

That would create a new post, with the title “Our New Post”, and the filler text we’ve entered.

If wp_insert_post() is successful, it returns the ID of the new post it has created (Which we assign to $post_id here). If it fails, it will return 0.

By default, the status is set to “Draft”, so let’s tweak it to publish the post instantly:

1
2
3
4
5
6
7
$new_post = array(
	'post_title' => 'Our New Post',
	'post_content' => 'Our filler text for the post.',
	'post_status' => 'publish'
);
 
$post_id = wp_insert_post($new_post);

You can set just about any value from the $post variable, e.g.

1
2
3
4
5
6
7
8
9
10
$new_post = array(
	'post_title' => 'Our New Post',
	'post_content' => 'Our filler text for the post.',
	'post_status' => 'publish',
	'post_type' => 'page',
	'post_author' => 2,
	'post_date' => '2011-09-01 15:10:30'
);
 
$post_id = wp_insert_post($new_post);

Our post is now a page, written by user #2, and scheduled to be published on the 1st of September.

Categories, Tags and Custom Taxonomies

Let’s move on to categories and tags. You can either set these as you make the rest of the post, or add them separately later. Let’s start by seeing how it is done in wp_insert_post().

For categories, you pass in an array containing the IDs of the categories you want to add, e.g. for categories 9 and 29:

1
2
3
4
5
6
7
8
$new_post = array(
	'post_title' => 'Our New Post',
	'post_content' => 'Our filler text for the post.',
	'post_status' => 'publish',
	'post_category' => array(9, 29)
);
 
$post_id = wp_insert_post($new_post);

Tags are a little easier. Instead of passing in the IDs, you can use the names of the tags themselves, e.g.

1
2
3
4
5
6
7
8
$new_post = array(
	'post_title' => 'Our New Post',
	'post_content' => 'Our filler text for the post.',
	'post_status' => 'publish',
	'tags_input' => array('tag1', 'Filler Tag')	
);
 
$post_id = wp_insert_post($new_post);

You can even set the terms of a custom taxonomy. This uses the “tax_input” parameter and expect you to give it an array where each key is a custom taxonomy, and each value is an array of tags in that taxonomy.

Update (8 Aug 2011): Rarst has left a comment saying that “tax_input” will only work if initiated by a logged in user with the permission to edit that taxonomy. If this affects you, read on below for a different solution that will work anywhere!

For example, let’s say we have a taxonomy called “custom_tax”, and the two tags we want to add to the post are “Dolor” and “Ipsum”.

1
2
3
4
5
6
7
8
9
10
$new_post = array(
	'post_title' => 'Our New Post',
	'post_content' => 'Our filler text for the post.',
	'post_status' => 'publish',
	'tax_input' => array(
		'custom_tax' => array('Dolor', 'Ipsum')
	)	
);
 
$post_id = wp_insert_post($new_post);

I mentioned that you could also add your tags later on. To do that, use the wp_set_object_terms() function (There are similar functions to this one, specifically for tags etc. but this covers it all for you).

The format is: wp_set_object_terms( post ID , array of tag IDs , taxonomy , append true/false ).

The last value is important. If you set true, then your new tags will be added to any existing ones. If you set false, then any existing tags will be replaced with your new ones.

Here are some examples of adding categories, tags, and a custom taxonomy this way:

1
2
3
wp_set_object_terms( $post_id, array(27, 28, 29), 'category', true);
wp_set_object_terms( $post_id, array('Example', 'Lorem Ipsum'), 'post_tag', true);
wp_set_object_terms( $post_id, array('Lorem', 'Sit'), 'custom_tax', true);

Metadata

If you’ve ever added custom meta data to a post before, you know what to do already.

Let’s say we want to add a custom field with the name “summary” and the value “Quick Summary!”:

1
add_post_meta( $post_id, 'summary', 'Quick summary!', true);

That’s quite self-explanatory. The ‘true’ at the end refers to whether this field is unique or not. If we set this to false, and a summary already exists, then another custom field also titled ‘summary’ would be added to the post.

By setting it to true, this won’t happen. WordPress checks for the ‘summary’ field first, and if it exists, it does nothing (You would then need to use update_post_meta() instead).

One special note to make is that if you want to set a page template, you do this with the meta functions as well. WordPress stores the name of the template file to use in a custom field titled ‘_wp_page_template’

1
update_post_meta( $post_id, '_wp_page_template',  'onecolumn-page.php');

Between those functions, you should be able to set up any post data you need. Feel free to post in the comments if you have any questions, or I’d love to hear how you’ve allowed users to add content to a WordPress site!

Enjoy this post? You should follow me on Twitter!