#22960 closed defect (bug) (fixed)
Add functions to extract images from content, attached to post, [gallery], or arbitrary string
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 3.6 | Priority: | normal |
Severity: | normal | Version: | 3.5 |
Component: | Post Formats | Keywords: | needs-codex has-unit-tests has-patch commit |
Focuses: | Cc: |
Description
Sorry if this is not a bug. If we create the gallery with the existing image from the media, we cannot get the images from that post using get_children()
or do_shortcode()
or get_posts()
. It only returns which are actual attachments to that post. And as of WordPress 3.5, galleries are no more related to particular post. Any thoughts on this?
Attachments (8)
Change History (58)
#3
@
12 years ago
- Keywords needs-codex added
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Severity changed from major to normal
- Status changed from new to closed
And as of WordPress 3.5, galleries are no more related to particular post.
Related: #22439
The ability to specify attachment IDs in a gallery shortcode isn't new to 3.5, it just got a handy UI.
You can grab attachment IDs from a gallery with a snippet like this:
function grab_attachment_ids_from_gallery() { global $post; $attachment_ids = array(); $pattern = get_shortcode_regex(); if ( preg_match_all( "/$pattern/s", $post->post_content, $matches ) ) { if ( $key = array_search( 'gallery', $matches[2] ) ) $atts = shortcode_parse_atts( $matches[3][ $key ] ); if ( isset( $atts['ids'] ) ) $attachment_ids = explode( ',', $atts['ids'] ); } echo '<pre>'; print_r( $attachment_ids ); echo '</pre>'; } add_action( 'wp', 'grab_attachment_ids_from_gallery' );
#4
@
12 years ago
- Milestone set to 3.6
- Resolution invalid deleted
- Status changed from closed to reopened
We're going to want to do an API around this.
#20
@
12 years ago
Bump! Chatting with Helen about this yesterday in terms of default themes and gallery post formats.
Twenty Eleven and Twenty Thirteen both have code to grab the 1st image from the gallery, so having a core function would allow us to remove a whole bunch of code.
#21
@
12 years ago
yeah, I have been adding patches per post format, I just ran out of steam last night - this is forthcoming
#22
@
12 years ago
- Summary changed from grab the images of a gallery to Add functions to extract images from content, attached to post, [gallery], or arbitrary string
#23
@
12 years ago
- Keywords has-patch added
attachment:22960.diff provides utility functions to get all image srcs in post_content and / or images attached to the post plus image src(s) from gallery shortcode(s)
Introduces get_content_images( &$content, $remove = false )
which will assemble [gallery]
s and read <img>
s, extract their srcs, optionally remove them from the content, and return them in a unique array.
Introduces get_the_images()
which assembles a unique list of every image in the post's content retrieved via get_content_images()
+ all of the images attached to the post, displayed or not
Introduces get_post_images()
which retrieves WP_Post objects for all images attached to the post
If more or different functionality is needed / wanted, let's iterate!
#28
follow-up:
↓ 30
@
12 years ago
- Keywords needs-unit-tests added
I would suggest the following breakdown:
- get_attached_images() — get images attached to the post. Simple wrapper of get_children(), as in the patch.
- get_content_images() — fetch all images referenced in the content
- get_post_galleries() — fetch all galleries referenced in the content
To bring it all together:
- get_associated_post_images() — which should also return post thumbnails. More or less covers #16349.
To also cover #23593:
- get_post_gallery() — first gallery referenced in the content
- get_content_image() — first image referenced in the content
Let's avoid the get_tag_regex() abstraction for now, I am thinking.
Also, the content and gallery extraction functions need unit tests like whoa.
#30
in reply to:
↑ 28
@
12 years ago
Replying to nacin:
get_post_galleries() — fetch all galleries referenced in the content
Specifically, an array of galleries, each an array of images. May need to be more robust if you want to know more about the gallery itself (like its settings), but the nature of how galleries are implemented make that fairly painful to implement anyway.
The primary use case for post formats is going to be "get me the first image of the first gallery". Indeed, this is the focus of #23593. (It has also been brought up previously in #20157, as gallery cover images.) I am thinking a bit farther here. The original title for this ticket is "grab the images of a gallery", and came out of 3.5 — see #23041, #22829.
#32
@
12 years ago
get_post_images()
becomesget_attached_images()
, returnsWP_Post
objects keyed by ID, wrapsget_children()
get_attached_image_srcs()
, calls the above and returns the result of passing post IDs towp_get_attachment_url()
get_content_images()
grabs<img>
src
attributes from the passed content blobget_content_image()
grabs first<img>
src
, calls above and returns first image, above can be limited to parse$limit
# of itemsget_content_galleries()
grabs[gallery]
s from passed content and returns structured data, which currently is only one node:src
,which contains a list of image URLs. In the future, more nodes can hold more data about the galleryget_post_galleries_images()
callsget_content_galleries()
for passed post and returns a list of lists by callingwp_list_pluck( $data, 'src' )
on each parsed gallery aboveget_post_gallery_images()
callsget_content_galleries()
for passed post's content and limits it to parsing one shortcodeget_associated_post_images()
is not impl'd / saved for later
UNIT TESTS also attached
#34
@
12 years ago
Of note, none of those deal with the post format meta for gallery, I am going to suggest we get rid of it
#36
@
12 years ago
once the functions exist in the WP, we can do another sweep to see if meta is necessary - also, if the UI vibes change to make meta more desirable, we can change the funcs
#37
@
12 years ago
Reiterating from ticket:23572:8 and ticket:23282:49, it seems like there's a lot of duplicated code in these get_content_*
functions. Had you considered a more generic approach such as maybe get_content_media( 'audio|video|link|url|etc' )
? We should be careful about creating specifically-named getters because we'd lock ourselves into the pattern of having to create brand new functions for every new kind of supported media.
Also happy to contribute with patch(es) :).
#38
@
12 years ago
Updated the patch so it doesn't have funcs that are already committed. Gonna take another pass through this.
#39
@
12 years ago
Updated patch and docs, get_content_galleries()
now additionally returns the parsed shortcode attrs with the list of image srcs. get_content_galleries()
is the meat and potatoes for all of the other functions
#41
@
12 years ago
Helen pointed at that we need to remove the [caption]
for an image when we remove the <img>
s in get_content_images()
- updated patch does so, what a joy!
#42
@
12 years ago
- Owner set to markjaquith
- Resolution set to fixed
- Status changed from reopened to closed
In 23772:
#43
@
12 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
What about filters for this? There may be other ways an image is associated that might want to become accessible via these new API functions.
#45
@
12 years ago
- Keywords needs-patch has-unit-tests added; has-patch needs-unit-tests removed
Added some filters that affect the get_attached_images()
function in [23776]. Looking at this, do we need filters for more than get_post_gallery|galleries? The ones that only get $content for context should probably be left alone.
#46
@
12 years ago
- Keywords has-patch commit added; needs-patch removed
attachment:22960.7.diff adds filters to the get_content_* functions for images, which are used by all extraction functions for images
#48
@
12 years ago
Cross-referencing with #23621 with the note that Twenty Thirteen actually doesn't use the first image from the gallery.
#49
@
12 years ago
In 1260/tests:
Related: #22907