WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 5 weeks ago

#41403 new defect (bug)

Support "class" and "id" attributes on wp_oembed_get()

Reported by: ramiy Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Embeds Keywords: needs-patch close dev-feedback
Focuses: Cc:

Description

The oembed function wp_oembed_get( $url, $args ) allows us to set additional arguments for retrieving embed HTML.

The problem is that currently the function supports two arguments, only width and height. In some cases developers need more flexibility, to set other HTML attributes like id, class and maybe even title (for better accessibility).


I was trying to thinking of an example and I think that the simplest example would be bootstrap responsive embeds.

<div class="embed-responsive embed-responsive-21by9">
  <iframe class="embed-responsive-item" src="https://..."></iframe>
</div>

Currently you can't set custom classes in iframe with the attributes:

wp_oembed_get( 'https://...', array( 'class' => 'embed-responsive-item' ) );

Change History (4)

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


5 weeks ago

#2 @markparnell
5 weeks ago

  • Component changed from General to Embeds
  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

#3 @peterwilsoncc
5 weeks ago

  • Keywords close added

This was discussed in a triage session today and moved to the Embed component as a result.

I suspect the reason only width and height are supported are due to the oembed spec, specifically the section on consumer requests:

Requests sent to the API endpoint must be HTTP GET requests, with all arguments sent as query parameters. All arguments must be urlencoded (as per RFC 1738).

The following query parameters are defined as part of the spec:

url (required)
The URL to retrieve embedding information for.

maxwidth (optional)
The maximum width of the embedded resource. Only applies to some resource types (as specified below). For supported resource types, this parameter must be respected by providers.

maxheight (optional)
The maximum height of the embedded resource. Only applies to some resource types (as specified below). For supported resource types, this parameter must be respected by providers.

format (optional)
The required response format. When not specified, the provider can return any valid response format. When specified, the provider must return data in the request format, else return an error (see below for error codes).

Providers should ignore all other arguments it doesn't expect. Providers are welcome to support custom additional parameters.

With the exception of a few embed types, WP passes the data on to the provider to return the expected HTML. Accepting IDs and classes within WordPress would be possible but not often respected by providers.

I'll leave this open for now rather than closing it outright so someone else with further knowledge of embeds can provide some thoughts.

#4 @ramiy
5 weeks ago

  • Keywords dev-feedback added

@peterwilsoncc Thank you for clarifying that. But I think we can overcome this by adding a new parameter for for the container div wrapping the oEmbed provider html.

The wp_oembed_get( $url, $args, $container ) function will check if $container is not empty, then a container will wrap the oEmbed html with custom Classes, IDs, Titles, aria-* tags, data-* tags and other attributes the developer want to put around the oEmbed code.

Another solutions is to filter thewp_oembed_get() returned value. This way developers can add container divs.

Note: See TracTickets for help on using tickets.