3 * Post format functions.
10 * Retrieve the format slug for a post
14 * @param int|object $post Post ID or post object. Optional, default is the current post from the loop.
15 * @return mixed The format if successful. False otherwise.
17 function get_post_format( $post = null ) {
18 if ( ! $post = get_post( $post ) )
21 if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
24 $_format = get_the_terms( $post->ID, 'post_format' );
26 if ( empty( $_format ) )
29 $format = array_shift( $_format );
31 return str_replace('post-format-', '', $format->slug );
35 * Check if a post has any of the given formats, or any format.
39 * @param string|array $format Optional. The format or formats to check.
40 * @param object|int $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop.
41 * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise.
43 function has_post_format( $format = array(), $post = null ) {
47 foreach ( (array) $format as $single ) {
48 $prefixed[] = 'post-format-' . sanitize_key( $single );
52 return has_term( $prefixed, 'post_format', $post );
56 * Assign a format to a post
60 * @param int|object $post The post for which to assign a format.
61 * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
62 * @return mixed WP_Error on error. Array of affected term IDs on success.
64 function set_post_format( $post, $format ) {
65 $post = get_post( $post );
68 return new WP_Error( 'invalid_post', __( 'Invalid post' ) );
70 if ( ! empty( $format ) ) {
71 $format = sanitize_key( $format );
72 if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) )
75 $format = 'post-format-' . $format;
78 return wp_set_post_terms( $post->ID, $format, 'post_format' );
82 * Returns an array of post format slugs to their translated and pretty display versions
86 * @return array The array of translated post format names.
88 function get_post_format_strings() {
90 'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
91 'aside' => _x( 'Aside', 'Post format' ),
92 'chat' => _x( 'Chat', 'Post format' ),
93 'gallery' => _x( 'Gallery', 'Post format' ),
94 'link' => _x( 'Link', 'Post format' ),
95 'image' => _x( 'Image', 'Post format' ),
96 'quote' => _x( 'Quote', 'Post format' ),
97 'status' => _x( 'Status', 'Post format' ),
98 'video' => _x( 'Video', 'Post format' ),
99 'audio' => _x( 'Audio', 'Post format' ),
105 * Retrieves an array of post format slugs.
109 * @return array The array of post format slugs.
111 function get_post_format_slugs() {
112 $slugs = array_keys( get_post_format_strings() );
113 return array_combine( $slugs, $slugs );
117 * Returns a pretty, translated version of a post format slug
121 * @param string $slug A post format slug.
122 * @return string The translated post format name.
124 function get_post_format_string( $slug ) {
125 $strings = get_post_format_strings();
127 return $strings['standard'];
129 return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
133 * Returns a link to a post format index.
137 * @param string $format The post format slug.
138 * @return string The post format term link.
140 function get_post_format_link( $format ) {
141 $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
142 if ( ! $term || is_wp_error( $term ) )
144 return get_term_link( $term );
148 * Filters the request to allow for the format prefix.
153 function _post_format_request( $qvs ) {
154 if ( ! isset( $qvs['post_format'] ) )
156 $slugs = get_post_format_slugs();
157 if ( isset( $slugs[ $qvs['post_format'] ] ) )
158 $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
159 $tax = get_taxonomy( 'post_format' );
161 $qvs['post_type'] = $tax->object_type;
164 add_filter( 'request', '_post_format_request' );
167 * Filters the post format term link to remove the format prefix.
172 function _post_format_link( $link, $term, $taxonomy ) {
174 if ( 'post_format' != $taxonomy )
176 if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
177 return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
179 $link = remove_query_arg( 'post_format', $link );
180 return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
183 add_filter( 'term_link', '_post_format_link', 10, 3 );
186 * Remove the post format prefix from the name property of the term object created by get_term().
191 function _post_format_get_term( $term ) {
192 if ( isset( $term->slug ) ) {
193 $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
197 add_filter( 'get_post_format', '_post_format_get_term' );
200 * Remove the post format prefix from the name property of the term objects created by get_terms().
205 function _post_format_get_terms( $terms, $taxonomies, $args ) {
206 if ( in_array( 'post_format', (array) $taxonomies ) ) {
207 if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
208 foreach( $terms as $order => $name ) {
209 $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
212 foreach ( (array) $terms as $order => $term ) {
213 if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
214 $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
221 add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
224 * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
229 function _post_format_wp_get_object_terms( $terms ) {
230 foreach ( (array) $terms as $order => $term ) {
231 if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
232 $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
237 add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );