WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/29/2016 05:28:00 PM (6 years ago)
Author:
joemcgill
Message:

Post Thumbnails: Add helper functions for attachment captions.

This adds three new functions for getting/displaying attachment captions:

  • wp_get_attachment_caption - Retrieves a caption for a specific attachment.
  • get_the_post_thumbnail_caption() - Returns the post thumbnail caption.
  • the_post_thumbnail_caption() - Displays the post thumbnail caption.

These are helpful for displaying a caption associated with an image directly
in a template, rather than using the caption shortcode.

This also introduces two new filters:

  • wp_get_attachment_caption - Filters the value of wp_get_attachment_caption().
  • the_post_thumbnail_caption - Filters the display of the post thumbnail caption.

the_post_thumbnail_caption() is automatically filtered by wptexturize(),
convert_smilies(), and convert_chars() in wp-includes/default-filters.php.

Props flixos90, joemcgill.
Fixes #12235.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r37702 r37915  
    903903
    904904    /**
     905     * @ticket 12235
     906     */
     907    function test_wp_get_attachment_caption() {
     908        $this->assertFalse( wp_get_attachment_caption( 0 ) );
     909
     910        $caption = 'This is a caption.';
     911
     912        $post_id = self::factory()->post->create();
     913        $attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
     914            'post_mime_type' => 'image/jpeg',
     915            'post_type'      => 'attachment',
     916            'post_excerpt'   => $caption,
     917        ) );
     918
     919        $this->assertFalse( wp_get_attachment_caption( $post_id ) );
     920
     921        $this->assertEquals( $caption, wp_get_attachment_caption( $attachment_id ) );
     922    }
     923
     924    /**
     925     * @ticket 12235
     926     */
     927    function test_wp_get_attachment_caption_empty() {
     928        $post_id = self::factory()->post->create();
     929        $attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
     930            'post_mime_type' => 'image/jpeg',
     931            'post_type'      => 'attachment',
     932            'post_excerpt'   => '',
     933        ) );
     934
     935        $this->assertEquals( '', wp_get_attachment_caption( $attachment_id ) );
     936    }
     937
     938    /**
    905939     * Helper function to get image size array from size "name"
    906940     */
Note: See TracChangeset for help on using the changeset viewer.