As of version 2.1, USP Pro makes it easy to filter and customize submitted post content. This tutorial explains how to make it happen using the usp_post_array filter hook.

The heart of front-end submission in WordPress is wp_insert_post(), which accepts an array of arguments that define the post. In USP Pro, the accepted array is named $usp_post and looks like this:

$usp_post = array(
	'post_author'   => $user_id,
	'post_content'  => $post_content,
	'post_type'     => $post_type,
	'tags_input'    => $post_tags,
	'post_title'    => $post_title,
	'post_status'   => $args['post_status'],
	'post_password' => $args['password'],
	'post_date'     => $date['post_date'],
	'post_date_gmt' => $date['post_date_gmt'],
);

Once this array is assembled, it’s passed to wp_insert_post() to create the post. Before it gets there, however, it’s hooked into the usp_post_array filter, like so:

$usp_post = apply_filters('usp_post_array', $usp_post);

This enables us to customize any aspect of the $usp_post array, so we can modify any of the data as desired. For example, we could do things like prepend text to the title, insert markup into the content, and modify tags.

Specific Example

Let’s look at a specific example of how we can use the usp_post_array filter to prepend some text to the submitted post_title. Consider the following code:

function usp_pro_modify_post_title($usp_post) {
	// do anything..
	return $usp_post;
}
add_filter('usp_post_array', 'usp_pro_modify_post_title');

Here we have a function named usp_pro_modify_post_title that is hooked into the usp_post_array via WP’s add_filter(). As written, the function accepts the $usp_post variable and then returns it to USP Pro, where it will be immediately used to create the post. In case you are wondering, this code could be included in a theme’s functions.php file, or even better, made into a plugin.

So now that we have access to the post array, let’s modify it by prepending a string of text to the title.

function usp_pro_modify_post_title($usp_post) {
	if (isset($usp_post['post_title'])) {
		$usp_post['post_title'] = 'Added text - '. $usp_post['post_title'];
	}
	return $usp_post;
}
add_filter('usp_post_array', 'usp_pro_modify_post_title');

Here we are checking to see if the post title is set. If it is, we append our added text to $usp_post['post_title'], which contains the submitted title data. The modified post array is then returned to USP Pro. Technically, the post array is returned to the plugin regardless of whether or not any changes are made. This ensures that the plugin functionality continues properly.

Of course, this is a very simple example — much more is possible using usp_post_array and other USP Hooks.

References