Hide the post title and auto-generate based on the post body, for the status and aside post formats.

props kovshenin. see #24011.

git-svn-id: http://core.svn.wordpress.org/trunk@24043 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Mark Jaquith 2013-04-22 04:00:25 +00:00
parent d2a8f064aa
commit 18aab9531a
3 changed files with 72 additions and 0 deletions

View File

@ -3995,6 +3995,8 @@ body .ui-tooltip {
margin-bottom: 20px;
}
.wp-format-aside #titlewrap,
.wp-format-status #titlewrap,
.wp-format-aside .wp-media-buttons .insert-media,
.wp-format-status .wp-media-buttons .insert-media {
display: none;

View File

@ -131,6 +131,7 @@ add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3
add_filter( 'the_title', 'wptexturize' );
add_filter( 'the_title', 'convert_chars' );
add_filter( 'the_title', 'trim' );
add_filter( 'the_title', '_post_formats_title', 10, 2 );
add_filter( 'the_content', 'post_formats_compat', 7 );
add_filter( 'the_content', 'wptexturize' );
@ -250,6 +251,7 @@ add_action( 'init', 'smilies_init',
add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 );
add_action( 'plugins_loaded', 'wp_maybe_load_embeds', 0 );
add_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
add_action( 'wp_insert_post_data', '_post_formats_fix_empty_title', 10, 2 );
add_action( 'wp_insert_post', 'wp_save_post_revision', 10, 1 );
add_action( 'publish_post', '_publish_post_hook', 5, 1 );
add_action( 'transition_post_status', '_transition_post_status', 5, 3 );

View File

@ -931,3 +931,71 @@ function the_remaining_content( $more_link_text = null, $strip_teaser = false )
echo str_replace( ']]>', ']]>', $content );
}
/**
* Don't display post titles for asides and status posts on the front end.
*
* @since 3.6.0
* @access private
*/
function _post_formats_title( $title, $post_id ) {
if ( is_admin() || is_feed() || ! in_array( get_post_format( $post_id ), array( 'aside', 'status' ) ) )
return $title;
// Return an empty string only if the title is auto-generated.
$post = get_post( $post_id );
if ( $title == _post_formats_generate_title( $post->post_content, get_post_format( $post_id ) ) )
$title = '';
return $title;
}
/**
* Generate a title from the post content or format.
*
* @since 3.6.0
* @access private
*/
function _post_formats_generate_title( $content, $post_format = '' ) {
$title = wp_trim_words( strip_shortcodes( $content ), 8, '' );
if ( empty( $title ) )
$title = get_post_format_string( $post_format );
return $title;
}
/**
* Runs during save_post, fixes empty titles for asides and statuses.
*
* @since 3.6.0
* @access private
*/
function _post_formats_fix_empty_title( $data, $postarr ) {
if ( 'auto-draft' == $data['post_status'] || ! post_type_supports( $data['post_type'], 'post-formats' ) )
return $data;
$post_id = ( isset( $postarr['ID'] ) ) ? absint( $postarr['ID'] ) : 0;
if ( $post_id )
$post_format = get_post_format( $post_id );
if ( isset( $postarr['post_format'] ) )
$post_format = ( in_array( $postarr['post_format'], get_post_format_slugs() ) ) ? $postarr['post_format'] : '';
if ( ! in_array( $post_format, array( 'aside', 'status' ) ) )
return $data;
if ( $data['post_title'] == _post_formats_generate_title( $data['post_content'], $post_format ) )
return $data;
// If updating an existing post, check whether the title was auto-generated.
if ( $post_id && $post = get_post( $post_id ) )
if ( $post->post_title == $data['post_title'] && $post->post_title == _post_formats_generate_title( $post->post_content, get_post_format( $post->ID ) ) )
$data['post_title'] = '';
if ( empty( $data['post_title'] ) )
$data['post_title'] = _post_formats_generate_title( $data['post_content'], $post_format );
return $data;
}