WordPress.org

Make WordPress Core

Changeset 24043


Ignore:
Timestamp:
04/22/2013 04:00:25 AM (7 years ago)
Author:
markjaquith
Message:

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

props kovshenin. see #24011.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/css/wp-admin.css

    r24042 r24043  
    39963996}
    39973997
     3998.wp-format-aside #titlewrap,
     3999.wp-format-status #titlewrap,
    39984000.wp-format-aside .wp-media-buttons .insert-media,
    39994001.wp-format-status .wp-media-buttons .insert-media {
  • trunk/wp-includes/default-filters.php

    r23881 r24043  
    132132add_filter( 'the_title', 'convert_chars' );
    133133add_filter( 'the_title', 'trim'          );
     134add_filter( 'the_title', '_post_formats_title', 10, 2 );
    134135
    135136add_filter( 'the_content', 'post_formats_compat', 7 );
     
    251252add_action( 'plugins_loaded',             'wp_maybe_load_embeds',                     0    );
    252253add_action( 'shutdown',                   'wp_ob_end_flush_all',                      1    );
     254add_action( 'wp_insert_post_data',        '_post_formats_fix_empty_title',           10, 2 );
    253255add_action( 'wp_insert_post',             'wp_save_post_revision',                   10, 1 );
    254256add_action( 'publish_post',               '_publish_post_hook',                       5, 1 );
  • trunk/wp-includes/post-formats.php

    r24034 r24043  
    932932    echo str_replace( ']]>', ']]>', $content );
    933933}
     934
     935/**
     936 * Don't display post titles for asides and status posts on the front end.
     937 *
     938 * @since 3.6.0
     939 * @access private
     940 */
     941function _post_formats_title( $title, $post_id ) {
     942    if ( is_admin() || is_feed() || ! in_array( get_post_format( $post_id ), array( 'aside', 'status' ) ) )
     943        return $title;
     944
     945    // Return an empty string only if the title is auto-generated.
     946    $post = get_post( $post_id );
     947    if ( $title == _post_formats_generate_title( $post->post_content, get_post_format( $post_id ) ) )
     948        $title = '';
     949
     950    return $title;
     951}
     952
     953/**
     954 * Generate a title from the post content or format.
     955 *
     956 * @since 3.6.0
     957 * @access private
     958 */
     959function _post_formats_generate_title( $content, $post_format = '' ) {
     960    $title = wp_trim_words( strip_shortcodes( $content ), 8, '' );
     961
     962    if ( empty( $title ) )
     963        $title = get_post_format_string( $post_format );
     964
     965    return $title;
     966}
     967
     968/**
     969 * Runs during save_post, fixes empty titles for asides and statuses.
     970 *
     971 * @since 3.6.0
     972 * @access private
     973 */
     974function _post_formats_fix_empty_title( $data, $postarr ) {
     975    if ( 'auto-draft' == $data['post_status'] || ! post_type_supports( $data['post_type'], 'post-formats' ) )
     976        return $data;
     977
     978    $post_id = ( isset( $postarr['ID'] ) ) ? absint( $postarr['ID'] ) : 0;
     979
     980    if ( $post_id )
     981        $post_format = get_post_format( $post_id );
     982
     983    if ( isset( $postarr['post_format'] ) )
     984        $post_format = ( in_array( $postarr['post_format'], get_post_format_slugs() ) ) ? $postarr['post_format'] : '';
     985
     986    if ( ! in_array( $post_format, array( 'aside', 'status' ) ) )
     987        return $data;
     988
     989    if ( $data['post_title'] == _post_formats_generate_title( $data['post_content'], $post_format ) )
     990        return $data;
     991
     992    // If updating an existing post, check whether the title was auto-generated.
     993    if ( $post_id && $post = get_post( $post_id ) )
     994        if ( $post->post_title == $data['post_title'] && $post->post_title == _post_formats_generate_title( $post->post_content, get_post_format( $post->ID ) ) )
     995            $data['post_title'] = '';
     996
     997    if ( empty( $data['post_title'] ) )
     998        $data['post_title'] = _post_formats_generate_title( $data['post_content'], $post_format );
     999
     1000    return $data;
     1001}
Note: See TracChangeset for help on using the changeset viewer.