WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/29/2016 05:28:00 PM (5 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/post/thumbnails.php

    r35244 r37915  
    7575    }
    7676
     77    /**
     78     * @ticket 12235
     79     */
     80    function test_get_the_post_thumbnail_caption() {
     81        $this->assertEquals( '', get_the_post_thumbnail_caption() );
     82
     83        $caption = 'This is a caption.';
     84
     85        $post_id = self::factory()->post->create();
     86        $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     87            'post_mime_type' => 'image/jpeg',
     88            'post_type'      => 'attachment',
     89            'post_excerpt'   => $caption,
     90        ) );
     91
     92        set_post_thumbnail( $post_id, $attachment_id );
     93
     94        $this->assertEquals( $caption, get_the_post_thumbnail_caption( $post_id ) );
     95    }
     96
     97    /**
     98     * @ticket 12235
     99     */
     100    function test_get_the_post_thumbnail_caption_empty() {
     101        $post_id = self::factory()->post->create();
     102        $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     103            'post_mime_type' => 'image/jpeg',
     104            'post_type'      => 'attachment',
     105            'post_excerpt'   => '',
     106        ) );
     107
     108        set_post_thumbnail( $post_id, $attachment_id );
     109
     110        $this->assertEquals( '', get_the_post_thumbnail_caption( $post_id ) );
     111    }
     112
     113    /**
     114     * @ticket 12235
     115     */
     116    function test_the_post_thumbnail_caption() {
     117        $caption = 'This is a caption.';
     118
     119        $post_id = self::factory()->post->create();
     120        $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     121            'post_mime_type' => 'image/jpeg',
     122            'post_type'      => 'attachment',
     123            'post_excerpt'   => $caption,
     124        ) );
     125
     126        set_post_thumbnail( $post_id, $attachment_id );
     127
     128        ob_start();
     129        the_post_thumbnail_caption( $post_id );
     130
     131        $this->assertEquals( $caption, ob_get_clean() );
     132    }
     133
    77134    function test_get_the_post_thumbnail() {
    78135        $this->assertEquals( '', get_the_post_thumbnail() );
Note: See TracChangeset for help on using the changeset viewer.