henrywright henrywright - 1 year ago 106
PHP Question

How to have an empty post title and content when creating a new WordPress post via the front-end

My aim is to insert a new draft post into the database via a custom front-end form I've created. I need the post title and content to be empty. Is this possible?

I've tried the following which doesn't work:

$post_data = array(
'post_title' => '',
'post_type' => 'post',
'post_content' => '',
'post_status' => 'draft',
'post_author' => 1

$post_id = wp_insert_post( $post_data );

Note: You can create a new post using the back-end editor with empty title and content so I am wondering how they guys at WordPress do it.

Answer Source

You can not insert a blank post with wp_insert_post, Wordpress will prevent it with wp_insert_post_empty_content hook, you can see it in the source code : http://wpseek.com/wp_insert_post/

The only way to do it is to overpass this hook with a custom function

Here is an example (source)

add_filter('pre_post_title', 'wpse28021_mask_empty');
add_filter('pre_post_content', 'wpse28021_mask_empty');
function wpse28021_mask_empty($value)
    if ( empty($value) ) {
        return ' ';
    return $value;

add_filter('wp_insert_post_data', 'wpse28021_unmask_empty');
function wpse28021_unmask_empty($data)
    if ( ' ' == $data['post_title'] ) {
        $data['post_title'] = '';
    if ( ' ' == $data['post_content'] ) {
        $data['post_content'] = '';
    return $data;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download