Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#29607 closed enhancement (fixed)

image_add_caption() returns immediately if caption empty, preventing filter from firing

Reported by: paulschreiber's profile paulschreiber Owned by: sergeybiryukov's profile 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)

29607.patch (472 bytes) - added by collinsinternet 10 years ago.
Creation of image_add_caption filter.
29607.2.patch (868 bytes) - added by collinsinternet 10 years ago.
Documented and up to coding standards.
29607.3.patch (966 bytes) - added by DrewAPicture 10 years ago.
Rewording

Download all attachments as: .zip

Change History (18)

@collinsinternet
10 years ago

Creation of image_add_caption filter.

#1 @collinsinternet
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 @SergeyBiryukov
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.

@collinsinternet
10 years ago

Documented and up to coding standards.

#3 @collinsinternet
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).

#4 @SergeyBiryukov
10 years ago

  • Milestone changed from Awaiting Review to 4.1

#5 @SergeyBiryukov
10 years ago

  • Focuses docs added

@DrewAPicture
10 years ago

Rewording

#6 @DrewAPicture
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 @SergeyBiryukov
10 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 29753:

Add 'image_add_caption_text' filter for changing the pre-parse caption text before output to the editor.

props collinsinternet, DrewAPicture.
fixes #29607.

#8 @SergeyBiryukov
10 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Should we pass the attachment ID to the filter as well?

#9 @SergeyBiryukov
10 years ago

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

In 29754:

Pass the attachment ID to the 'image_add_caption_text' filter.

fixes #29607.

#10 @paulschreiber
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 @collinsinternet
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 @paulschreiber
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 @MikeHansenMe
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 @DrewAPicture
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.

#15 @DrewAPicture
10 years ago

  • Focuses docs removed
Note: See TracTickets for help on using tickets.