#29607 closed enhancement (fixed)
image_add_caption() returns immediately if caption empty, preventing filter from firing
Reported by: | paulschreiber | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 4.1 | Priority: | normal |
Severity: | normal | Version: | 4.1 |
Component: | Media | Keywords: | needs-testing has-patch commit |
Focuses: | administration | Cc: |
Description
In wp-admin/includes/media.php, the function image_add_caption
returns immediately if the caption is empty.
Our caption filter adds a credit even if the image has no caption. However, because the filter is only fired at the end of image_add_caption:
apply_filters( 'image_add_caption_shortcode', $shcode, $html );
our filter never gets a chance to run when the caption is blank, so we can't add the credit.
Attachments (3)
Change History (18)
#1
@
10 years ago
- Component changed from General to Media
- Keywords needs-testing has-patch added
- Type changed from defect (bug) to enhancement
I've added a filter to the image_add_caption() function that filters the caption text.
#2
@
10 years ago
- Keywords needs-patch added; has-patch removed
The filter needs to be documented as per the documentation standards.
Also, please take a look at WordPress coding standards, specifically the Space Usage section.
#3
@
10 years ago
- Keywords has-patch added; needs-patch removed
Ha SergeyBiryukov, that's what I get for working late. Latest patch includes documentation block and code formatting (not sure why my IDE didn't format the first time).
#6
@
10 years ago
- Keywords commit added
At first I was concerned about effectively running two filters in a row here, but after further consideration, the new filter really does provide something not currently available -- the ability to directly filter the pre-parse caption text before output to the editor.
In 29607.3.patch, I've reworded the long description a bit to clarify expected behavior in passing an empty value. I also changed the hook name to 'image_add_caption_text'.
#7
@
10 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 29753:
#8
@
10 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
Should we pass the attachment ID to the filter as well?
#10
@
10 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
This doesn't solve my problem. We want to be able to call image_add_caption_shortcode()
even when caption is blank.
The isempty()
check is still present:
if ( empty($caption) || apply_filters( 'disable_captions', '' ) ) return $html;
Which means the function still returns before we get to call the filter.
#11
@
10 years ago
@paulschreiber Why can't you add a filter that verifies that the caption is present and appends it if it is blank?
#12
@
10 years ago
We have some images that do not (and should not) have captions. But they should have photo credits, which are added by our caption filter. However, the caption filter does not get a chance to run when $caption
is empty()
.
#13
@
10 years ago
looking at the code in image_add_caption, if you add something to $caption on line 175 via the filter it should not be empty on line 187 when checked. Then the only way line 188
"return $html" would run is if disable_caption is true(which seems to be expected).
#14
@
10 years ago
- Resolution set to fixed
- Status changed from reopened to closed
@MikeHansenMe is correct in comment:13. We need to preserve the existing behavior of the disable_caption
filter so the new filter added in [29753] should give you enough rope to manage that. Even passing something like a non-breaking space would bypass the empty/disable_caption
check.
Creation of image_add_caption filter.