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.
Built from https://develop.svn.wordpress.org/trunk@37915


git-svn-id: http://core.svn.wordpress.org/trunk@37856 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2016-06-29 17:28:28 +00:00
parent 25e66e4f1e
commit 170d22a22c
4 changed files with 77 additions and 1 deletions

View File

@ -143,6 +143,10 @@ add_filter( 'the_excerpt', 'wpautop' );
add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'the_post_thumbnail_caption', 'wptexturize' );
add_filter( 'the_post_thumbnail_caption', 'convert_smilies' );
add_filter( 'the_post_thumbnail_caption', 'convert_chars' );
add_filter( 'comment_text', 'wptexturize' );
add_filter( 'comment_text', 'convert_chars' );
add_filter( 'comment_text', 'make_clickable', 9 );

View File

@ -210,3 +210,44 @@ function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
echo esc_url( $url );
}
}
/**
* Returns the post thumbnail caption.
*
* @since 4.6.0
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
* @return string Post thumbnail caption.
*/
function get_the_post_thumbnail_caption( $post = null ) {
$post_thumbnail_id = get_post_thumbnail_id( $post );
if ( ! $post_thumbnail_id ) {
return '';
}
$caption = wp_get_attachment_caption( $post_thumbnail_id );
if ( ! $caption ) {
$caption = '';
}
return $caption;
}
/**
* Displays the post thumbnail caption.
*
* @since 4.6.0
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
*/
function the_post_thumbnail_caption( $post = null ) {
/**
* Filters the displayed post thumbnail caption.
*
* @since 4.6.0
*
* @param string $caption Caption for the given attachment.
*/
echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) );
}

View File

@ -4936,6 +4936,37 @@ function wp_get_attachment_url( $post_id = 0 ) {
return $url;
}
/**
* Retrieves the caption for an attachment.
*
* @since 4.6.0
*
* @param int $post_id Optional. Attachment ID. Default 0.
* @return string|false False on failure. Attachment caption on success.
*/
function wp_get_attachment_caption( $post_id = 0 ) {
$post_id = (int) $post_id;
if ( ! $post = get_post( $post_id ) ) {
return false;
}
if ( 'attachment' !== $post->post_type ) {
return false;
}
$caption = $post->post_excerpt;
/**
* Filters the attachment caption.
*
* @since 4.6.0
*
* @param string $caption Caption for the given attachment.
* @param int $post_id Attachment ID.
*/
return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
}
/**
* Retrieve thumbnail for an attachment.
*

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37914';
$wp_version = '4.6-alpha-37915';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.