Make WordPress Core

Opened 7 years ago

Closed 5 years ago

Last modified 5 years ago

#42826 closed enhancement (fixed)

Change get_oembed_response_data to apply filters to title

Reported by: mheikkila's profile mheikkila Owned by: desrosj's profile desrosj
Milestone: 5.2 Priority: normal
Severity: normal Version: 4.4
Component: Embeds Keywords: has-patch commit
Focuses: Cc:

Description

The function get_oembed_response_data in wp-includes/embed.php outputs the title using

$post->post_title

This is a problem when the title includes special tags. For example, the very popular plugin qTranslateX (https://fi.wordpress.org/plugins/qtranslate-x/) does this to manage multilingual content. In this case, the function should use this to output the title:

apply_filters('the_title',$post->post_title)

The oembed content overrides the og tags. This is a problem for example when sharing a link to LinkedIn.

So here would be the new get_oembed_response_data function:

function get_oembed_response_data( $post, $width ) {
	$post  = get_post( $post );
	$width = absint( $width );

	if ( ! $post ) {
		return false;
	}

	if ( 'publish' !== get_post_status( $post ) ) {
		return false;
	}

	/**
	 * Filters the allowed minimum and maximum widths for the oEmbed response.
	 *
	 * @since 4.4.0
	 *
	 * @param array $min_max_width {
	 *     Minimum and maximum widths for the oEmbed response.
	 *
	 *     @type int $min Minimum width. Default 200.
	 *     @type int $max Maximum width. Default 600.
	 * }
	 */
	$min_max_width = apply_filters( 'oembed_min_max_width', array(
		'min' => 200,
		'max' => 600
	) );

	$width  = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
	$height = max( ceil( $width / 16 * 9 ), 200 );

	$data = array(
		'version'       => '1.0',
		'provider_name' => get_bloginfo( 'name' ),
		'provider_url'  => get_home_url(),
		'author_name'   => get_bloginfo( 'name' ),
		'author_url'    => get_home_url(),
		'title'         => apply_filters('the_title',$post->post_title),
		'type'          => 'link',
	);

	$author = get_userdata( $post->post_author );

	if ( $author ) {
		$data['author_name'] = $author->display_name;
		$data['author_url']  = get_author_posts_url( $author->ID );
	}

	/**
	 * Filters the oEmbed response data.
	 *
	 * @since 4.4.0
	 *
	 * @param array   $data   The response data.
	 * @param WP_Post $post   The post object.
	 * @param int     $width  The requested width.
	 * @param int     $height The calculated height.
	 */
	return apply_filters( 'oembed_response_data', $data, $post, $width, $height );
}

Attachments (2)

42826.diff (457 bytes) - added by chetan200891 7 years ago.
Created patch to change $post->post_title to get_the_title()
42826.2.diff (486 bytes) - added by chetan200891 6 years ago.
Refreshed patch to pass $post.

Download all attachments as: .zip

Change History (16)

#1 @swissspidy
7 years ago

  • Component changed from General to Embeds
  • Keywords needs-patch added
  • Type changed from defect (bug) to enhancement

Hey there

Thanks for your ticket!

Instead of using apply_filters() this should rather use get_the_title().

If you want to help create a proper patch for this, please have a look at https://make.wordpress.org/core/handbook/tutorials/working-with-patches/

@chetan200891
7 years ago

Created patch to change $post->post_title to get_the_title()

This ticket was mentioned in Slack in #core by chetan200891. View the logs.


6 years ago

#3 @chetan200891
6 years ago

  • Keywords has-patch added; needs-patch removed

#4 @swissspidy
6 years ago

  • Milestone changed from Awaiting Review to 5.0
  • Version set to 4.4

New attachments don‘t trigger notifications, so thanks for the comment :-)

#5 @pento
6 years ago

  • Milestone changed from 5.0 to 5.1

#6 @pento
6 years ago

  • Keywords needs-refresh added
  • Milestone changed from 5.1 to 5.2

In 42826.diff, $post needs to be passed to get_the_title().

@chetan200891
6 years ago

Refreshed patch to pass $post.

#7 @chetan200891
6 years ago

@pento Thanks for your suggestion. I have refreshed patch 42826.2.diff to pass $post to get_the_title().

#8 @chetan200891
6 years ago

  • Keywords needs-refresh removed

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


5 years ago

#10 @desrosj
5 years ago

  • Owner set to desrosj
  • Status changed from new to reviewing

#11 @desrosj
5 years ago

  • Keywords commit added

#12 @desrosj
5 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 44907:

Embeds: Allow post title to be filtered when preparing an oEmbed response.

By changing the way the post title is added to the array of data from $post->post_title to get_the_title(), the title can now be modified using the the_title filter. This ensures post titles returned in oEmbed responses are consistent with those that show on a site.

Props mheikkila, swissspidy.
Fixes #42826.

#13 @desrosj
5 years ago

@chetan200891 Apologies! I missed your props in that commit. It looks like you have received props on other tickets in the 5.2 milestone, though. So you will be properly credited in the release :).

#14 @chetan200891
5 years ago

@desrosj That's totally okay. No Problem! All Good. Cheers :)

Note: See TracTickets for help on using tickets.