id summary reporter owner description type status priority milestone component version severity resolution keywords cc focuses
18713 "No hooks available to ""calculate"" a post_title when only custom metaboxes in use" mikeschinkel nacin "When building fully custom post data entry screens for custom post types where the default `post_title`, `post_content` and `post_excerpt` fields are ''not'' displayed in their standard data entry format the `wp_insert_post()` function bails without any hooks available to correct the problem.
The code that bails in on line # ~2430 in `/wp-includes/post.php` within `wp_insert_post()` when `$post_title` and `$post_content` and `$post_excerpt` are not set and `$post_type` is not `'attachment'`:
{{{
if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) && ('attachment' != $post_type) ) {
if ( $wp_error )
return new WP_Error('empty_content', __('Content, title, and excerpt are empty.'));
else
return 0;
}
}}}
An example where this is an issue is when creating a ''""Person""'' custom post type where there are meta fields for `'first_name'`, `'middle_initial'` and `'last_name'` and we want to calculate the `post_title` field based ON (something) like the following code:
{{{
extract( $_POST );
if ( empty( $middle_initial ) )
$_POST['post_title'] = ""{$first_name} {$last_name}"";
else
$_POST['post_title'] = ""{$first_name} {$middle_initial} {$last_name}"";
}}}
One solution would be to add a `'wp_translate_postdata'` hook on the return statement of the `__wp_translate_postdata()` function found in `/wp-admin/includes/post.php`:
{{{
return apply_filters( 'wp_translate_postdata', $post_data, $udpate );
}}}
This filter would allow someone building a custom post type to update `$post_data` with values for `$post_content`, `$post_title` and `$post_excerpt`, or really any other field's value, for that matter.
Such a hook would also allow for critical error handling for unit, acceptance and regression tests. A user could write code in this hook for use with `WP_DEBUG` to ensure that data is always POSTed in the correct format. For example it would be helpful to test for when Javascript is used to create fields with complex values that need to be in the correct format to be properly saved to the database.
I have attached a patch that can add the above pondered `'wp_translate_postdata'` hook to core, and as it is very simple hopefully it could make it in without much (any?) testing. However, suggestions for alternate solutions that address these two needs are welcome with the hope that something will make it into WordPress core to address these issues in the near future. Thanks in advance." defect (bug) closed normal 3.3 Posts, Post Types 3.2.1 normal fixed has-patch