Make WordPress Core

Opened 5 years ago

Last modified 5 years ago

#39850 new enhancement

WordPress embedded links drop query vars from URL

Reported by: charleslf Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Embeds Keywords:
Focuses: Cc:

Description (last modified by swissspidy)

When embedding a WordPress page within another post, any query strings that are passed in the URL get dropped.

For example, if embedding http://example.com/page-name/?foo=bar, the actual URL that gets passed to the oEmbed function (and later on to the template files) is http://example.com/page-name/embed/ (foo=bar gets dropped).

The link inside the blockquote also drops the url query variable and links to http://example.com/page-name/ instead of http://example.com/page-name/?foo=bar

As I see, this occurs because the wp_filter_pre_oembed_result function in embed.php uses url_to_postid to get the embeded post's ID, and later get_post_embed_url uses that ID to create the embed permalink. So naturally any url query vars are not preserved in the final URL construction.
Similarly in get_post_embed_html, the function uses esc_url( get_permalink( $post ) ), thus reconstructing the permalink without knowledge of the initial url.

I encountered this when embedded a specific photo gallery (from nextcellent plugin), but I assume some other plugins such as language/translation ones may also be affected.

I don't think the URL's should be dropping any of the query variables.
However, if that is a requirement, it would be good to also pass on the original url in the post_embed_url hook to allow plugin developers to make sure to add any parameters from the original url that got dropped off in the embedded url.

Change History (3)

#1 @swissspidy
5 years ago

Previous discussion on Slack about this: https://wordpress.slack.com/archives/core/p1486845896008270

Passing the request URL to the post_embed_url filter would mean adding it to the get_post_embed_html() function and all functions using it, i.e. print_embed_sharing_dialog(), get_oembed_response_data_rich(), wp_filter_pre_oembed_result(). At a quick glance, it seems doable, but it would also make everything more complex.

My proposed (untested) workaround on Slack:

add_filter( 'oembed_request_post_id', function( $post_id, $url ) {
  if ( $url === … ) {
    add_filter( 'post_embed_url', function( $embed_url ) use ( $url ) {
      remove_filter( current_filter(), __FUNCTION__ );

      // define $my_new_url based on $url

      return $my_new_url;
    } );

  return $post_id;
}, 10, 2 );

#2 @swissspidy
5 years ago

  • Description modified (diff)
  • Summary changed from Wordpress embedded links drop query vars from URL to WordPress embedded links drop query vars from URL

#3 @charleslf
5 years ago

I can confirm that the proposed solution works perfectly for overriding the embedded iframe url.

The url in blockquote, however, is based on get_permalink and can not be modified in the same manner.

Note: See TracTickets for help on using tickets.