Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#26675 closed enhancement (fixed)

Rewrite of get_media_embedded_in_content() function to produce expected results.

Reported by: kopepasah's profile kopepasah Owned by: drewapicture's profile DrewAPicture
Milestone: 4.2 Priority: normal
Severity: normal Version: 3.6
Component: Media Keywords: good-first-bug has-patch commit 2nd-opinion
Focuses: Cc:


The get_media_embedded_in_content() function does not output the media in the array in the order it appears in the content (as it should). Rather, it loops through each tag and gets the media in the order of tag (if get_tag_regex() worked correctly, see #26674).

The attached patch adds a third parameter 'sortby' to declare how the results should sort. By default it sorts by 'type' and the remains as it is now. If a user declares the 'sortby' parameter as 'source', the results would appear in the order they are found in the content (which is what I expected).

Also preg_match() was changed to preg_match_all() to find all the media of a type (instead of just one).

Lastly, the regex used in the 'source' sort by is a modification of the get_tag_regex() to try subpatterns in alternation. An optional fix would be to add the named group in the get_tag_regex() function, but that is another patch.

Side note: I added 'img' to the option of allowed media types (because it is media). If anyone has a reason this should not be in there, I would like to know why. I did not find any complications while testing.

Attachments (5)

26675.patch (2.0 KB) - added by kopepasah 10 years ago.
media.php_unit-test.patch (3.5 KB) - added by kopepasah 10 years ago.
26675-2014.02.19.patch (1.1 KB) - added by Kopepasah 10 years ago.
26675.3.diff (690 bytes) - added by valendesigns 9 years ago.
26675.diff (751 bytes) - added by DrewAPicture 9 years ago.
Renamed to media_embedded_in_content_allowed_types

Download all attachments as: .zip

Change History (25)

10 years ago

#1 @kopepasah
10 years ago

  • Cc justin@… added
  • Keywords has-patch needs-testing added

This ticket was mentioned in IRC in #wordpress-dev by kopepasah. View the logs.

10 years ago

#3 @nacin
10 years ago

  • Milestone changed from Awaiting Review to Future Release

Hey, Justin. I think we could get away with sorting these by how they appear in the content always, without a parameter. Would be nice to still use get_tag_regex() but I get how the sorting makes it tough.

"media" was designed to refer to "audio" or "video" (or some other form of interaction).

We could make it additionally allow for "img" (probably not as a default). The problem with images is they can be inserted all sorts of ways. Gallery shortcodes, captioned images, image HTML, etc., etc. We actually had some image extraction functions as a counterpart to this function, but they weren't really good enough to ship in core, because of all of the edge cases. Sticking to these elements makes us limit the edge cases pretty well, and we ditched the other functions later in the 3.6 cycle.

#4 @Kopepasah
10 years ago

Nacin, great suggestions. I will submit another patch based on those.

For the images, should I add an additional parameter or a filter. I personally vote filter, as this will allow users to not only add additional media types to search for (e.g. img), but also search for a specific media type (e.g. iframe) or multiple types (e.g. audio & video). With that being said, it will also allow for searching for any HTML element if a users sets the filter that way, but that could be edge case.

What do you think?

#5 @Kopepasah
10 years ago


I've attached 26675-2014.01.19.patch to address your suggestions.

I removed the $sortby parameter and changed the sorting to how the media was located in the content. Should we add an additional parameter to allow for sorting of by media item and not by found?

I also added a filter (get_media_embedded_in_content_allowed) to customize the allowed embedded media types. I think this is a better alternative than adding an additional parameter.

Feedback is appreciated.

This ticket was mentioned in IRC in #wordpress-dev by kopepasah. View the logs.

10 years ago

#7 @johnbillion
10 years ago

  • Version changed from trunk to 3.6

#8 @wonderboymusic
9 years ago

  • Keywords needs-docs added; needs-testing removed
  • Milestone changed from Future Release to 4.2

#9 @wonderboymusic
9 years ago

In 31574:

Improve get_media_embedded_in_content() so that it returns the media it finds in the same order that it appears in the content.

Adds unit test, updates another.

Props kopepasah.
See #26675.

#10 @wonderboymusic
9 years ago

  • Keywords good-first-bug added; has-patch removed

'get_media_embedded_in_content_allowed' need filter docs

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

9 years ago

#12 @DrewAPicture
9 years ago

  • Keywords 4.2-beta added

@wonderboymusic: What's left here? This will ride into Beta 1, see [31574].

9 years ago

#13 @valendesigns
9 years ago

  • Keywords has-patch added; needs-docs removed

Patch 26675.3.diff adds filter docs.

#14 @DrewAPicture
9 years ago

  • Owner set to wonderboymusic
  • Status changed from new to assigned

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

9 years ago

#16 @DrewAPicture
9 years ago

  • Owner changed from wonderboymusic to DrewAPicture
  • Status changed from assigned to reviewing

#17 @DrewAPicture
9 years ago

  • Keywords commit added; 4.2-beta removed

#18 @DrewAPicture
9 years ago

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

In 31851:

Add hook documentation for the get_media_embedded_in_content_allowed filter, introduced in [31574].

Props valendesigns.
Fixes #26675.

#19 @ocean90
9 years ago

  • Keywords 2nd-opinion added
  • Resolution fixed deleted
  • Status changed from closed to reopened

I think that the filter name doesn't fit into core's existing *_allowed_* filters.


  • wp_kses_allowed_html
  • kses_allowed_protocols
  • wp_nav_menu_container_allowedtags
  • customize_allowed_urls

The current name pretends that you are able to disable the use of get_media_embedded_in_content(). But that's wrong, you can filter the allowed media types for get_media_embedded_in_content(). Based on the existing names the filter should end with _allowed(_media)_types.

I suggest get_media_embedded_in_content_allowed_types or media_embedded_in_content_allowed_types as the name for this filter.

9 years ago

Renamed to media_embedded_in_content_allowed_types

#20 @ocean90
9 years ago

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

In 32113:

Rename get_media_embedded_in_content_allowed filter to media_embedded_in_content_allowed_types.

The new name fits better with some other _allowed_ filters.

props DrewAPicture.
fixes #26675.

Note: See TracTickets for help on using tickets.