Making WordPress.org

Changeset 9176


Ignore:
Timestamp:
10/14/2019 04:05:18 AM (5 years ago)
Author:
dd32
Message:

WordPress.TV: Upon updating the post title, update the respective video title to match.

This is done, as the attachment title is used as the VideoPress embed title.

Fixes #4667.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php

    r9175 r9176  
    920920remove_filter( 'the_title', 'widont' );
    921921add_filter( 'the_title', 'wptv_widont' );
     922
     923/**
     924 * Update Attachment titles to match the Post Title.
     925 *
     926 * This is done as the Attachment Title is shown within the VideoPress embeds.
     927 * This can't be done through a filter in the themes functions.php, as it appears that's not loaded for the Rest API.
     928 *
     929 * @see https://meta.trac.wordpress.org/ticket/4667
     930 */
     931function wptv_update_attachment_titles( $id, $post, $post_before ) {
     932    if ( 'post' !== $post->post_type ) {
     933        return;
     934    }
     935
     936    // Get attachments - Should only be one?
     937    $attachments = get_posts( array(
     938        'post_type'   => 'attachment',
     939        'post_parent' => $id,
     940    ) );
     941    if ( ! $attachments || count( $attachments ) > 1 ) {
     942        return;
     943    }
     944    $attachment = $attachments[0];
     945
     946    if ( $post->post_title === $attachment->post_title ) {
     947        // Titles match, nothing needs updating.
     948        return;
     949    } elseif ( sanitize_file_name( $attachment->post_title ) === $attachment->post_title ) {
     950        // Default title, Update it.
     951    } elseif ( $attachment->post_title === $post_before->post_title ) {
     952        // Set to the same as the post, update to the new title.
     953    } else {
     954        // It's set to something that isn't the current, previous, or default titles.
     955        // Assume that the title has been set manually and bail.
     956        return;
     957    }
     958
     959    // Update attachment to share the post title.
     960    // The attachment title appears in VideoPress thumbnails.
     961    $attachment->post_title = $post->post_title;
     962    wp_update_post( $attachment );
     963}
     964add_action( 'post_updated', 'wptv_update_attachment_titles', 10, 3 );
Note: See TracChangeset for help on using the changeset viewer.