[wp-trac] [WordPress Trac] #18713: No hooks available to "calculate" a post_title when only custom metaboxes in use
WordPress Trac
wp-trac at lists.automattic.com
Thu Sep 29 19:46:11 UTC 2011
#18713: No hooks available to "calculate" a post_title when only custom metaboxes
in use
------------------------------------------------+--------------------------
Reporter: mikeschinkel | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting
Component: Post Types | Review
Severity: normal | Version: 3.2.1
Keywords: has-patch dev-feedback 2nd-opinion | Resolution:
------------------------------------------------+--------------------------
Comment (by mikeschinkel):
@prettyboymp - The `santize_post_field()` is an interesting suggestion.
Here's what I found after trying it:
Yes it is possible to provide a value for `post_title` using the
`'pre_post_title'` hook before WordPress punts on an empty value. However
when used in the needed`'db'` context that hook only gets passed the
$value and not `$post_id` or any other needed info. This is ironic because
the alternate hook `'edit_post_field'` used in the `'edit'` context is
passed both `$value` and `$post_id`. So for it to be a solution we'd
minimally need to add `$post_id` as a 2nd parameter to the
`'pre_post_title'` hook. Here's the `sanitize_post_fields()` function so
you can see what I'm talking about:
{{{
function sanitize_post_field($field, $value, $post_id, $context) {
$int_fields = array('ID', 'post_parent', 'menu_order');
if ( in_array($field, $int_fields) )
$value = (int) $value;
// Fields which contain arrays of ints.
$array_int_fields = array( 'ancestors' );
if ( in_array($field, $array_int_fields) ) {
$value = array_map( 'absint', $value);
return $value;
}
if ( 'raw' == $context )
return $value;
$prefixed = false;
if ( false !== strpos($field, 'post_') ) {
$prefixed = true;
$field_no_prefix = str_replace('post_', '', $field);
}
if ( 'edit' == $context ) {
$format_to_edit = array('post_content', 'post_excerpt',
'post_title', 'post_password');
if ( $prefixed ) {
$value = apply_filters("edit_{$field}", $value,
$post_id);
// Old school
$value =
apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id);
} else {
$value = apply_filters("edit_post_{$field}",
$value, $post_id);
}
if ( in_array($field, $format_to_edit) ) {
if ( 'post_content' == $field )
$value = format_to_edit($value,
user_can_richedit());
else
$value = format_to_edit($value);
} else {
$value = esc_attr($value);
}
} else if ( 'db' == $context ) {
if ( $prefixed ) {
$value = apply_filters("pre_{$field}", $value);
$value =
apply_filters("{$field_no_prefix}_save_pre", $value);
} else {
$value = apply_filters("pre_post_{$field}",
$value);
$value = apply_filters("{$field}_pre", $value);
}
} else {
// Use display filters by default.
if ( $prefixed )
$value = apply_filters($field, $value, $post_id,
$context);
else
$value = apply_filters("post_{$field}", $value,
$post_id, $context);
}
if ( 'attribute' == $context )
$value = esc_attr($value);
else if ( 'js' == $context )
$value = esc_js($value);
return $value;
}
}}}
However `sanitize_post_field()` is called in at least eight (8) places
within WordPress core with six (6) different contexts, and leveraging it
to set a default value for `post_title` or `post_content` is likely to
result in lots of unintended consequences that would take a long time to
identify and then correct. So I think a hook that is much more limited in
scope would have less potential for inadvertent bugs.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/18713#comment:14>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list