3 * WordPress Post Template Functions.
5 * Gets content for the current post in the loop.
12 * Display the ID of the current item in the WordPress Loop.
21 * Retrieve the ID of the current item in the WordPress Loop.
25 * @return int|false The ID of the current item in the WordPress Loop. False if $post is not set.
27 function get_the_ID() {
29 return ! empty( $post ) ? $post->ID : false;
33 * Display or retrieve the current post title with optional markup.
37 * @param string $before Optional. Markup to prepend to the title. Default empty.
38 * @param string $after Optional. Markup to append to the title. Default empty.
39 * @param bool $echo Optional. Whether to echo or return the title. Default true for echo.
40 * @return string|void Current post title if $echo is false.
42 function the_title( $before = '', $after = '', $echo = true ) {
43 $title = get_the_title();
45 if ( strlen($title) == 0 )
48 $title = $before . $title . $after;
57 * Sanitize the current title when retrieving or displaying.
59 * Works like the_title(), except the parameters can be in a string or
60 * an array. See the function for what can be override in the $args parameter.
62 * The title before it is displayed will have the tags stripped and esc_attr()
63 * before it is passed to the user or displayed. The default as with the_title(),
64 * is to display the title.
68 * @param string|array $args {
69 * Title attribute arguments. Optional.
71 * @type string $before Markup to prepend to the title. Default empty.
72 * @type string $after Markup to append to the title. Default empty.
73 * @type bool $echo Whether to echo or return the title. Default true for echo.
74 * @type WP_Post $post Current post object to retrieve the title for.
76 * @return string|void String when echo is false.
78 function the_title_attribute( $args = '' ) {
79 $defaults = array( 'before' => '', 'after' => '', 'echo' => true, 'post' => get_post() );
80 $r = wp_parse_args( $args, $defaults );
82 $title = get_the_title( $r['post'] );
84 if ( strlen( $title ) == 0 ) {
88 $title = $r['before'] . $title . $r['after'];
89 $title = esc_attr( strip_tags( $title ) );
99 * Retrieve post title.
101 * If the post is protected and the visitor is not an admin, then "Protected"
102 * will be displayed before the post title. If the post is private, then
103 * "Private" will be located before the post title.
107 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
110 function get_the_title( $post = 0 ) {
111 $post = get_post( $post );
113 $title = isset( $post->post_title ) ? $post->post_title : '';
114 $id = isset( $post->ID ) ? $post->ID : 0;
116 if ( ! is_admin() ) {
117 if ( ! empty( $post->post_password ) ) {
120 * Filters the text prepended to the post title for protected posts.
122 * The filter is only applied on the front end.
126 * @param string $prepend Text displayed before the post title.
127 * Default 'Protected: %s'.
128 * @param WP_Post $post Current post object.
130 $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
131 $title = sprintf( $protected_title_format, $title );
132 } elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
135 * Filters the text prepended to the post title of private posts.
137 * The filter is only applied on the front end.
141 * @param string $prepend Text displayed before the post title.
142 * Default 'Private: %s'.
143 * @param WP_Post $post Current post object.
145 $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
146 $title = sprintf( $private_title_format, $title );
151 * Filters the post title.
155 * @param string $title The post title.
156 * @param int $id The post ID.
158 return apply_filters( 'the_title', $title, $id );
162 * Display the Post Global Unique Identifier (guid).
164 * The guid will appear to be a link, but should not be used as a link to the
165 * post. The reason you should not use it as a link, is because of moving the
166 * blog across domains.
168 * URL is escaped to make it XML-safe.
172 * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
174 function the_guid( $post = 0 ) {
175 $post = get_post( $post );
177 $guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
178 $id = isset( $post->ID ) ? $post->ID : 0;
181 * Filters the escaped Global Unique Identifier (guid) of the post.
185 * @see get_the_guid()
187 * @param string $guid Escaped Global Unique Identifier (guid) of the post.
188 * @param int $id The post ID.
190 echo apply_filters( 'the_guid', $guid, $id );
194 * Retrieve the Post Global Unique Identifier (guid).
196 * The guid will appear to be a link, but should not be used as an link to the
197 * post. The reason you should not use it as a link, is because of moving the
198 * blog across domains.
202 * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
205 function get_the_guid( $post = 0 ) {
206 $post = get_post( $post );
208 $guid = isset( $post->guid ) ? $post->guid : '';
209 $id = isset( $post->ID ) ? $post->ID : 0;
212 * Filters the Global Unique Identifier (guid) of the post.
216 * @param string $guid Global Unique Identifier (guid) of the post.
217 * @param int $id The post ID.
219 return apply_filters( 'get_the_guid', $guid, $id );
223 * Display the post content.
227 * @param string $more_link_text Optional. Content for when there is more text.
228 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
230 function the_content( $more_link_text = null, $strip_teaser = false) {
231 $content = get_the_content( $more_link_text, $strip_teaser );
234 * Filters the post content.
238 * @param string $content Content of the current post.
240 $content = apply_filters( 'the_content', $content );
241 $content = str_replace( ']]>', ']]>', $content );
246 * Retrieve the post content.
250 * @global int $page Page number of a single post/page.
251 * @global int $more Boolean indicator for whether single post/page is being viewed.
252 * @global bool $preview Whether post/page is in preview mode.
253 * @global array $pages Array of all pages in post/page. Each array element contains part of the content separated by the <!--nextpage--> tag.
254 * @global int $multipage Boolean indicator for whether multiple pages are in play.
256 * @param string $more_link_text Optional. Content for when there is more text.
257 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
260 function get_the_content( $more_link_text = null, $strip_teaser = false ) {
261 global $page, $more, $preview, $pages, $multipage;
265 if ( null === $more_link_text ) {
266 $more_link_text = sprintf(
267 '<span aria-label="%1$s">%2$s</span>',
269 /* translators: %s: Name of current post */
270 __( 'Continue reading %s' ),
271 the_title_attribute( array( 'echo' => false ) )
273 __( '(more…)' )
280 // If post password required and it doesn't match the cookie.
281 if ( post_password_required( $post ) )
282 return get_the_password_form( $post );
284 if ( $page > count( $pages ) ) // if the requested page doesn't exist
285 $page = count( $pages ); // give them the highest numbered page that DOES exist
287 $content = $pages[$page - 1];
288 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
289 $content = explode( $matches[0], $content, 2 );
290 if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
291 $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
295 $content = array( $content );
298 if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
299 $strip_teaser = true;
301 $teaser = $content[0];
303 if ( $more && $strip_teaser && $has_teaser )
308 if ( count( $content ) > 1 ) {
310 $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
312 if ( ! empty( $more_link_text ) )
315 * Filters the Read More link text.
319 * @param string $more_link_element Read More link element.
320 * @param string $more_link_text Read More text.
322 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
323 $output = force_balance_tags( $output );
327 if ( $preview ) // Preview fix for JavaScript bug with foreign languages.
328 $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );
334 * Preview fix for JavaScript bug with foreign languages.
339 * @param array $match Match array from preg_replace_callback.
342 function _convert_urlencoded_to_entities( $match ) {
343 return '&#' . base_convert( $match[1], 16, 10 ) . ';';
347 * Display the post excerpt.
351 function the_excerpt() {
354 * Filters the displayed post excerpt.
358 * @see get_the_excerpt()
360 * @param string $post_excerpt The post excerpt.
362 echo apply_filters( 'the_excerpt', get_the_excerpt() );
366 * Retrieves the post excerpt.
369 * @since 4.5.0 Introduced the `$post` parameter.
371 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
372 * @return string Post excerpt.
374 function get_the_excerpt( $post = null ) {
375 if ( is_bool( $post ) ) {
376 _deprecated_argument( __FUNCTION__, '2.3.0' );
379 $post = get_post( $post );
380 if ( empty( $post ) ) {
384 if ( post_password_required( $post ) ) {
385 return __( 'There is no excerpt because this is a protected post.' );
389 * Filters the retrieved post excerpt.
392 * @since 4.5.0 Introduced the `$post` parameter.
394 * @param string $post_excerpt The post excerpt.
395 * @param WP_Post $post Post object.
397 return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
401 * Whether post has excerpt.
405 * @param int|WP_Post $id Optional. Post ID or post object.
408 function has_excerpt( $id = 0 ) {
409 $post = get_post( $id );
410 return ( !empty( $post->post_excerpt ) );
414 * Display the classes for the post div.
418 * @param string|array $class One or more classes to add to the class list.
419 * @param int|WP_Post $post_id Optional. Post ID or post object. Defaults to the global `$post`.
421 function post_class( $class = '', $post_id = null ) {
422 // Separates classes with a single space, collates classes for post DIV
423 echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
427 * Retrieves the classes for the post div as an array.
429 * The class names are many. If the post is a sticky, then the 'sticky'
430 * class name. The class 'hentry' is always added to each post. If the post has a
431 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
432 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
433 * eg 'category-foo' or 'my_custom_taxonomy-bar'.
435 * The 'post_tag' taxonomy is a special
436 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
437 * passed through the filter, {@see 'post_class'}, with the list of classes, followed by
438 * $class parameter value, with the post ID as the last parameter.
441 * @since 4.2.0 Custom taxonomy classes were added.
443 * @param string|array $class One or more classes to add to the class list.
444 * @param int|WP_Post $post_id Optional. Post ID or post object.
445 * @return array Array of classes.
447 function get_post_class( $class = '', $post_id = null ) {
448 $post = get_post( $post_id );
453 if ( ! is_array( $class ) ) {
454 $class = preg_split( '#\s+#', $class );
456 $classes = array_map( 'esc_attr', $class );
458 // Ensure that we always coerce class to being an array.
466 $classes[] = 'post-' . $post->ID;
468 $classes[] = $post->post_type;
469 $classes[] = 'type-' . $post->post_type;
470 $classes[] = 'status-' . $post->post_status;
473 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
474 $post_format = get_post_format( $post->ID );
476 if ( $post_format && !is_wp_error($post_format) )
477 $classes[] = 'format-' . sanitize_html_class( $post_format );
479 $classes[] = 'format-standard';
482 $post_password_required = post_password_required( $post->ID );
484 // Post requires password.
485 if ( $post_password_required ) {
486 $classes[] = 'post-password-required';
487 } elseif ( ! empty( $post->post_password ) ) {
488 $classes[] = 'post-password-protected';
492 if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) {
493 $classes[] = 'has-post-thumbnail';
496 // sticky for Sticky Posts
497 if ( is_sticky( $post->ID ) ) {
498 if ( is_home() && ! is_paged() ) {
499 $classes[] = 'sticky';
500 } elseif ( is_admin() ) {
501 $classes[] = 'status-sticky';
505 // hentry for hAtom compliance
506 $classes[] = 'hentry';
508 // All public taxonomies
509 $taxonomies = get_taxonomies( array( 'public' => true ) );
510 foreach ( (array) $taxonomies as $taxonomy ) {
511 if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
512 foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
513 if ( empty( $term->slug ) ) {
517 $term_class = sanitize_html_class( $term->slug, $term->term_id );
518 if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
519 $term_class = $term->term_id;
522 // 'post_tag' uses the 'tag' prefix for backward compatibility.
523 if ( 'post_tag' == $taxonomy ) {
524 $classes[] = 'tag-' . $term_class;
526 $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
532 $classes = array_map( 'esc_attr', $classes );
535 * Filters the list of CSS classes for the current post.
539 * @param array $classes An array of post classes.
540 * @param array $class An array of additional classes added to the post.
541 * @param int $post_id The post ID.
543 $classes = apply_filters( 'post_class', $classes, $class, $post->ID );
545 return array_unique( $classes );
549 * Display the classes for the body element.
553 * @param string|array $class One or more classes to add to the class list.
555 function body_class( $class = '' ) {
556 // Separates classes with a single space, collates classes for body element
557 echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
561 * Retrieve the classes for the body element as an array.
565 * @global WP_Query $wp_query
567 * @param string|array $class One or more classes to add to the class list.
568 * @return array Array of classes.
570 function get_body_class( $class = '' ) {
578 if ( is_front_page() )
583 $classes[] = 'archive';
587 $classes[] = 'search';
588 $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
591 $classes[] = 'paged';
592 if ( is_attachment() )
593 $classes[] = 'attachment';
595 $classes[] = 'error404';
597 if ( is_singular() ) {
598 $post_id = $wp_query->get_queried_object_id();
599 $post = $wp_query->get_queried_object();
600 $post_type = $post->post_type;
602 if ( is_page_template() ) {
603 $classes[] = "{$post_type}-template";
605 $template_slug = get_page_template_slug( $post_id );
606 $template_parts = explode( '/', $template_slug );
608 foreach ( $template_parts as $part ) {
609 $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
611 $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
613 $classes[] = "{$post_type}-template-default";
617 $classes[] = 'single';
618 if ( isset( $post->post_type ) ) {
619 $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
620 $classes[] = 'postid-' . $post_id;
623 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
624 $post_format = get_post_format( $post->ID );
626 if ( $post_format && !is_wp_error($post_format) )
627 $classes[] = 'single-format-' . sanitize_html_class( $post_format );
629 $classes[] = 'single-format-standard';
634 if ( is_attachment() ) {
635 $mime_type = get_post_mime_type($post_id);
636 $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
637 $classes[] = 'attachmentid-' . $post_id;
638 $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
639 } elseif ( is_page() ) {
642 $page_id = $wp_query->get_queried_object_id();
644 $post = get_post($page_id);
646 $classes[] = 'page-id-' . $page_id;
648 if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
649 $classes[] = 'page-parent';
652 if ( $post->post_parent ) {
653 $classes[] = 'page-child';
654 $classes[] = 'parent-pageid-' . $post->post_parent;
657 } elseif ( is_archive() ) {
658 if ( is_post_type_archive() ) {
659 $classes[] = 'post-type-archive';
660 $post_type = get_query_var( 'post_type' );
661 if ( is_array( $post_type ) )
662 $post_type = reset( $post_type );
663 $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
664 } elseif ( is_author() ) {
665 $author = $wp_query->get_queried_object();
666 $classes[] = 'author';
667 if ( isset( $author->user_nicename ) ) {
668 $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
669 $classes[] = 'author-' . $author->ID;
671 } elseif ( is_category() ) {
672 $cat = $wp_query->get_queried_object();
673 $classes[] = 'category';
674 if ( isset( $cat->term_id ) ) {
675 $cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
676 if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
677 $cat_class = $cat->term_id;
680 $classes[] = 'category-' . $cat_class;
681 $classes[] = 'category-' . $cat->term_id;
683 } elseif ( is_tag() ) {
684 $tag = $wp_query->get_queried_object();
686 if ( isset( $tag->term_id ) ) {
687 $tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
688 if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
689 $tag_class = $tag->term_id;
692 $classes[] = 'tag-' . $tag_class;
693 $classes[] = 'tag-' . $tag->term_id;
695 } elseif ( is_tax() ) {
696 $term = $wp_query->get_queried_object();
697 if ( isset( $term->term_id ) ) {
698 $term_class = sanitize_html_class( $term->slug, $term->term_id );
699 if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
700 $term_class = $term->term_id;
703 $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
704 $classes[] = 'term-' . $term_class;
705 $classes[] = 'term-' . $term->term_id;
710 if ( is_user_logged_in() )
711 $classes[] = 'logged-in';
713 if ( is_admin_bar_showing() ) {
714 $classes[] = 'admin-bar';
715 $classes[] = 'no-customize-support';
718 if ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() )
719 $classes[] = 'custom-background';
721 if ( has_custom_logo() ) {
722 $classes[] = 'wp-custom-logo';
725 $page = $wp_query->get( 'page' );
727 if ( ! $page || $page < 2 )
728 $page = $wp_query->get( 'paged' );
730 if ( $page && $page > 1 && ! is_404() ) {
731 $classes[] = 'paged-' . $page;
734 $classes[] = 'single-paged-' . $page;
736 $classes[] = 'page-paged-' . $page;
737 elseif ( is_category() )
738 $classes[] = 'category-paged-' . $page;
740 $classes[] = 'tag-paged-' . $page;
742 $classes[] = 'date-paged-' . $page;
743 elseif ( is_author() )
744 $classes[] = 'author-paged-' . $page;
745 elseif ( is_search() )
746 $classes[] = 'search-paged-' . $page;
747 elseif ( is_post_type_archive() )
748 $classes[] = 'post-type-paged-' . $page;
751 if ( ! empty( $class ) ) {
752 if ( !is_array( $class ) )
753 $class = preg_split( '#\s+#', $class );
754 $classes = array_merge( $classes, $class );
756 // Ensure that we always coerce class to being an array.
760 $classes = array_map( 'esc_attr', $classes );
763 * Filters the list of CSS body classes for the current post or page.
767 * @param array $classes An array of body classes.
768 * @param array $class An array of additional classes added to the body.
770 $classes = apply_filters( 'body_class', $classes, $class );
772 return array_unique( $classes );
776 * Whether post requires password and correct password has been provided.
780 * @param int|WP_Post|null $post An optional post. Global $post used if not provided.
781 * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
783 function post_password_required( $post = null ) {
784 $post = get_post($post);
786 if ( empty( $post->post_password ) ) {
787 /** This filter is documented in wp-includes/post.php */
788 return apply_filters( 'post_password_required', false, $post );
791 if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
792 /** This filter is documented in wp-includes/post.php */
793 return apply_filters( 'post_password_required', true, $post );
796 $hasher = new PasswordHash( 8, true );
798 $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
799 if ( 0 !== strpos( $hash, '$P$B' ) ) {
802 $required = ! $hasher->CheckPassword( $post->post_password, $hash );
806 * Filters whether a post requires the user to supply a password.
810 * @param bool $required Whether the user needs to supply a password. True if password has not been
811 * provided or is incorrect, false if password has been supplied or is not required.
812 * @param WP_Post $post Post data.
814 return apply_filters( 'post_password_required', $required, $post );
818 // Page Template Functions for usage in Themes
822 * The formatted output of a list of pages.
824 * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
825 * Quicktag one or more times). This tag must be within The Loop.
830 * @global int $numpages
831 * @global int $multipage
834 * @param string|array $args {
835 * Optional. Array or string of default arguments.
837 * @type string $before HTML or text to prepend to each link. Default is `<p> Pages:`.
838 * @type string $after HTML or text to append to each link. Default is `</p>`.
839 * @type string $link_before HTML or text to prepend to each link, inside the `<a>` tag.
840 * Also prepended to the current item, which is not linked. Default empty.
841 * @type string $link_after HTML or text to append to each Pages link inside the `<a>` tag.
842 * Also appended to the current item, which is not linked. Default empty.
843 * @type string $next_or_number Indicates whether page numbers should be used. Valid values are number
844 * and next. Default is 'number'.
845 * @type string $separator Text between pagination links. Default is ' '.
846 * @type string $nextpagelink Link text for the next page link, if available. Default is 'Next Page'.
847 * @type string $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'.
848 * @type string $pagelink Format string for page numbers. The % in the parameter string will be
849 * replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc.
850 * Defaults to '%', just the page number.
851 * @type int|bool $echo Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.
853 * @return string Formatted output in HTML.
855 function wp_link_pages( $args = '' ) {
856 global $page, $numpages, $multipage, $more;
859 'before' => '<p>' . __( 'Pages:' ),
863 'next_or_number' => 'number',
865 'nextpagelink' => __( 'Next page' ),
866 'previouspagelink' => __( 'Previous page' ),
871 $params = wp_parse_args( $args, $defaults );
874 * Filters the arguments used in retrieving page links for paginated posts.
878 * @param array $params An array of arguments for page links for paginated posts.
880 $r = apply_filters( 'wp_link_pages_args', $params );
884 if ( 'number' == $r['next_or_number'] ) {
885 $output .= $r['before'];
886 for ( $i = 1; $i <= $numpages; $i++ ) {
887 $link = $r['link_before'] . str_replace( '%', $i, $r['pagelink'] ) . $r['link_after'];
888 if ( $i != $page || ! $more && 1 == $page ) {
889 $link = _wp_link_page( $i ) . $link . '</a>';
892 * Filters the HTML output of individual page number links.
896 * @param string $link The page number HTML output.
897 * @param int $i Page number for paginated posts' page links.
899 $link = apply_filters( 'wp_link_pages_link', $link, $i );
901 // Use the custom links separator beginning with the second link.
902 $output .= ( 1 === $i ) ? ' ' : $r['separator'];
905 $output .= $r['after'];
907 $output .= $r['before'];
910 $link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '</a>';
912 /** This filter is documented in wp-includes/post-template.php */
913 $output .= apply_filters( 'wp_link_pages_link', $link, $prev );
916 if ( $next <= $numpages ) {
918 $output .= $r['separator'];
920 $link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>';
922 /** This filter is documented in wp-includes/post-template.php */
923 $output .= apply_filters( 'wp_link_pages_link', $link, $next );
925 $output .= $r['after'];
930 * Filters the HTML output of page links for paginated posts.
934 * @param string $output HTML output of paginated posts' page links.
935 * @param array $args An array of arguments.
937 $html = apply_filters( 'wp_link_pages', $output, $args );
946 * Helper function for wp_link_pages().
951 * @global WP_Rewrite $wp_rewrite
953 * @param int $i Page number.
954 * @return string Link.
956 function _wp_link_page( $i ) {
959 $query_args = array();
962 $url = get_permalink();
964 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
965 $url = add_query_arg( 'page', $i, get_permalink() );
966 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
967 $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
969 $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
972 if ( is_preview() ) {
974 if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
975 $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] );
976 $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
979 $url = get_preview_post_link( $post, $query_args, $url );
982 return '<a href="' . esc_url( $url ) . '">';
986 // Post-meta: Custom per-post fields.
990 * Retrieve post custom meta data field.
994 * @param string $key Meta data key name.
995 * @return false|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
997 function post_custom( $key = '' ) {
998 $custom = get_post_custom();
1000 if ( !isset( $custom[$key] ) )
1002 elseif ( 1 == count($custom[$key]) )
1003 return $custom[$key][0];
1005 return $custom[$key];
1009 * Display list of post custom fields.
1013 * @internal This will probably change at some point...
1016 function the_meta() {
1017 if ( $keys = get_post_custom_keys() ) {
1018 echo "<ul class='post-meta'>\n";
1019 foreach ( (array) $keys as $key ) {
1021 if ( is_protected_meta( $keyt, 'post' ) )
1023 $values = array_map('trim', get_post_custom_values($key));
1024 $value = implode($values,', ');
1027 * Filters the HTML output of the li element in the post custom fields list.
1031 * @param string $html The HTML output for the li element.
1032 * @param string $key Meta key.
1033 * @param string $value Meta value.
1035 echo apply_filters( 'the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value );
1046 * Retrieve or display list of pages as a dropdown (select list).
1049 * @since 4.2.0 The `$value_field` argument was added.
1050 * @since 4.3.0 The `$class` argument was added.
1052 * @param array|string $args {
1053 * Optional. Array or string of arguments to generate a pages drop-down element.
1055 * @type int $depth Maximum depth. Default 0.
1056 * @type int $child_of Page ID to retrieve child pages of. Default 0.
1057 * @type int|string $selected Value of the option that should be selected. Default 0.
1058 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1,
1059 * or their bool equivalents. Default 1.
1060 * @type string $name Value for the 'name' attribute of the select element.
1061 * Default 'page_id'.
1062 * @type string $id Value for the 'id' attribute of the select element.
1063 * @type string $class Value for the 'class' attribute of the select element. Default: none.
1064 * Defaults to the value of `$name`.
1065 * @type string $show_option_none Text to display for showing no pages. Default empty (does not display).
1066 * @type string $show_option_no_change Text to display for "no change" option. Default empty (does not display).
1067 * @type string $option_none_value Value to use when no page is selected. Default empty.
1068 * @type string $value_field Post field used to populate the 'value' attribute of the option
1069 * elements. Accepts any valid post field. Default 'ID'.
1071 * @return string HTML content, if not displaying.
1073 function wp_dropdown_pages( $args = '' ) {
1075 'depth' => 0, 'child_of' => 0,
1076 'selected' => 0, 'echo' => 1,
1077 'name' => 'page_id', 'id' => '',
1079 'show_option_none' => '', 'show_option_no_change' => '',
1080 'option_none_value' => '',
1081 'value_field' => 'ID',
1084 $r = wp_parse_args( $args, $defaults );
1086 $pages = get_pages( $r );
1088 // Back-compat with old system where both id and name were based on $name argument
1089 if ( empty( $r['id'] ) ) {
1090 $r['id'] = $r['name'];
1093 if ( ! empty( $pages ) ) {
1095 if ( ! empty( $r['class'] ) ) {
1096 $class = " class='" . esc_attr( $r['class'] ) . "'";
1099 $output = "<select name='" . esc_attr( $r['name'] ) . "'" . $class . " id='" . esc_attr( $r['id'] ) . "'>\n";
1100 if ( $r['show_option_no_change'] ) {
1101 $output .= "\t<option value=\"-1\">" . $r['show_option_no_change'] . "</option>\n";
1103 if ( $r['show_option_none'] ) {
1104 $output .= "\t<option value=\"" . esc_attr( $r['option_none_value'] ) . '">' . $r['show_option_none'] . "</option>\n";
1106 $output .= walk_page_dropdown_tree( $pages, $r['depth'], $r );
1107 $output .= "</select>\n";
1111 * Filters the HTML output of a list of pages as a drop down.
1114 * @since 4.4.0 `$r` and `$pages` added as arguments.
1116 * @param string $output HTML output for drop down list of pages.
1117 * @param array $r The parsed arguments array.
1118 * @param array $pages List of WP_Post objects returned by `get_pages()`
1120 $html = apply_filters( 'wp_dropdown_pages', $output, $r, $pages );
1129 * Retrieve or display list of pages in list (li) format.
1132 * @since 4.7.0 Added the `item_spacing` argument.
1136 * @global WP_Query $wp_query
1138 * @param array|string $args {
1139 * Array or string of arguments. Optional.
1141 * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages).
1142 * @type string $authors Comma-separated list of author IDs. Default empty (all authors).
1143 * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
1144 * Default is the value of 'date_format' option.
1145 * @type int $depth Number of levels in the hierarchy of pages to include in the generated list.
1146 * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
1147 * the given n depth). Default 0.
1148 * @type bool $echo Whether or not to echo the list of pages. Default true.
1149 * @type string $exclude Comma-separated list of page IDs to exclude. Default empty.
1150 * @type array $include Comma-separated list of page IDs to include. Default empty.
1151 * @type string $link_after Text or HTML to follow the page link label. Default null.
1152 * @type string $link_before Text or HTML to precede the page link label. Default null.
1153 * @type string $post_type Post type to query for. Default 'page'.
1154 * @type string|array $post_status Comma-separated list or array of post statuses to include. Default 'publish'.
1155 * @type string $show_date Whether to display the page publish or modified date for each page. Accepts
1156 * 'modified' or any other value. An empty value hides the date. Default empty.
1157 * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author',
1158 * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
1159 * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
1160 * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list
1161 * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
1162 * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'.
1163 * Default 'preserve'.
1164 * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
1166 * @return string|void HTML list of pages.
1168 function wp_list_pages( $args = '' ) {
1172 'date_format' => get_option( 'date_format' ),
1175 'title_li' => __( 'Pages' ),
1178 'sort_column' => 'menu_order, post_title',
1179 'link_before' => '',
1181 'item_spacing' => 'preserve',
1185 $r = wp_parse_args( $args, $defaults );
1187 if ( ! in_array( $r['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
1188 // invalid value, fall back to default.
1189 $r['item_spacing'] = $defaults['item_spacing'];
1195 // sanitize, mostly to keep spaces out
1196 $r['exclude'] = preg_replace( '/[^0-9,]/', '', $r['exclude'] );
1198 // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
1199 $exclude_array = ( $r['exclude'] ) ? explode( ',', $r['exclude'] ) : array();
1202 * Filters the array of pages to exclude from the pages list.
1206 * @param array $exclude_array An array of page IDs to exclude.
1208 $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );
1211 $r['hierarchical'] = 0;
1212 $pages = get_pages( $r );
1214 if ( ! empty( $pages ) ) {
1215 if ( $r['title_li'] ) {
1216 $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
1219 if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
1220 $current_page = get_queried_object_id();
1221 } elseif ( is_singular() ) {
1222 $queried_object = get_queried_object();
1223 if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
1224 $current_page = $queried_object->ID;
1228 $output .= walk_page_tree( $pages, $r['depth'], $current_page, $r );
1230 if ( $r['title_li'] ) {
1231 $output .= '</ul></li>';
1236 * Filters the HTML output of the pages to list.
1239 * @since 4.4.0 `$pages` added as arguments.
1241 * @see wp_list_pages()
1243 * @param string $output HTML output of the pages list.
1244 * @param array $r An array of page-listing arguments.
1245 * @param array $pages List of WP_Post objects returned by `get_pages()`
1247 $html = apply_filters( 'wp_list_pages', $output, $r, $pages );
1257 * Displays or retrieves a list of pages with an optional home link.
1259 * The arguments are listed below and part of the arguments are for wp_list_pages()} function.
1260 * Check that function for more info on those arguments.
1263 * @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments.
1264 * @since 4.7.0 Added the `item_spacing` argument.
1266 * @param array|string $args {
1267 * Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
1269 * @type string $sort_column How to short the list of pages. Accepts post column names.
1270 * Default 'menu_order, post_title'.
1271 * @type string $menu_id ID for the div containing the page list. Default is empty string.
1272 * @type string $menu_class Class to use for the element containing the page list. Default 'menu'.
1273 * @type string $container Element to use for the element containing the page list. Default 'div'.
1274 * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
1276 * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
1277 * you'd like shown for the home link. 1|true defaults to 'Home'.
1278 * @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
1279 * @type string $link_after The HTML or text to append to $show_home text. Default empty.
1280 * @type string $before The HTML or text to prepend to the menu. Default is '<ul>'.
1281 * @type string $after The HTML or text to append to the menu. Default is '</ul>'.
1282 * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'. Default 'discard'.
1283 * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
1285 * @return string|void HTML menu
1287 function wp_page_menu( $args = array() ) {
1289 'sort_column' => 'menu_order, post_title',
1291 'menu_class' => 'menu',
1292 'container' => 'div',
1294 'link_before' => '',
1298 'item_spacing' => 'discard',
1301 $args = wp_parse_args( $args, $defaults );
1303 if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ) ) ) {
1304 // invalid value, fall back to default.
1305 $args['item_spacing'] = $defaults['item_spacing'];
1308 if ( 'preserve' === $args['item_spacing'] ) {
1317 * Filters the arguments used to generate a page-based menu.
1321 * @see wp_page_menu()
1323 * @param array $args An array of page menu arguments.
1325 $args = apply_filters( 'wp_page_menu_args', $args );
1331 // Show Home in the menu
1332 if ( ! empty($args['show_home']) ) {
1333 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
1336 $text = $args['show_home'];
1338 if ( is_front_page() && !is_paged() )
1339 $class = 'class="current_page_item"';
1340 $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
1341 // If the front page is a page, add it to the exclude list
1342 if (get_option('show_on_front') == 'page') {
1343 if ( !empty( $list_args['exclude'] ) ) {
1344 $list_args['exclude'] .= ',';
1346 $list_args['exclude'] = '';
1348 $list_args['exclude'] .= get_option('page_on_front');
1352 $list_args['echo'] = false;
1353 $list_args['title_li'] = '';
1354 $menu .= wp_list_pages( $list_args );
1356 $container = sanitize_text_field( $args['container'] );
1358 // Fallback in case `wp_nav_menu()` was called without a container.
1359 if ( empty( $container ) ) {
1365 // wp_nav_menu doesn't set before and after
1366 if ( isset( $args['fallback_cb'] ) &&
1367 'wp_page_menu' === $args['fallback_cb'] &&
1368 'ul' !== $container ) {
1369 $args['before'] = "<ul>{$n}";
1370 $args['after'] = '</ul>';
1373 $menu = $args['before'] . $menu . $args['after'];
1377 if ( ! empty( $args['menu_id'] ) ) {
1378 $attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"';
1381 if ( ! empty( $args['menu_class'] ) ) {
1382 $attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
1385 $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}";
1388 * Filters the HTML output of a page-based menu.
1392 * @see wp_page_menu()
1394 * @param string $menu The HTML output.
1395 * @param array $args An array of arguments.
1397 $menu = apply_filters( 'wp_page_menu', $menu, $args );
1398 if ( $args['echo'] )
1409 * Retrieve HTML list content for page list.
1411 * @uses Walker_Page to create HTML list content.
1414 * @param array $pages
1416 * @param int $current_page
1420 function walk_page_tree( $pages, $depth, $current_page, $r ) {
1421 if ( empty($r['walker']) )
1422 $walker = new Walker_Page;
1424 $walker = $r['walker'];
1426 foreach ( (array) $pages as $page ) {
1427 if ( $page->post_parent )
1428 $r['pages_with_children'][ $page->post_parent ] = true;
1431 $args = array($pages, $depth, $r, $current_page);
1432 return call_user_func_array(array($walker, 'walk'), $args);
1436 * Retrieve HTML dropdown (select) content for page list.
1438 * @uses Walker_PageDropdown to create HTML dropdown content.
1440 * @see Walker_PageDropdown::walk() for parameters and return description.
1444 function walk_page_dropdown_tree() {
1445 $args = func_get_args();
1446 if ( empty($args[2]['walker']) ) // the user's options are the third parameter
1447 $walker = new Walker_PageDropdown;
1449 $walker = $args[2]['walker'];
1451 return call_user_func_array(array($walker, 'walk'), $args);
1459 * Display an attachment page link using an image or icon.
1463 * @param int|WP_Post $id Optional. Post ID or post object.
1464 * @param bool $fullsize Optional, default is false. Whether to use full size.
1465 * @param bool $deprecated Deprecated. Not used.
1466 * @param bool $permalink Optional, default is false. Whether to include permalink.
1468 function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
1469 if ( !empty( $deprecated ) )
1470 _deprecated_argument( __FUNCTION__, '2.5.0' );
1473 echo wp_get_attachment_link($id, 'full', $permalink);
1475 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
1479 * Retrieve an attachment page link using an image or icon, if possible.
1482 * @since 4.4.0 The `$id` parameter can now accept either a post ID or `WP_Post` object.
1484 * @param int|WP_Post $id Optional. Post ID or post object.
1485 * @param string|array $size Optional. Image size. Accepts any valid image size, or an array
1486 * of width and height values in pixels (in that order).
1487 * Default 'thumbnail'.
1488 * @param bool $permalink Optional, Whether to add permalink to image. Default false.
1489 * @param bool $icon Optional. Whether the attachment is an icon. Default false.
1490 * @param string|false $text Optional. Link text to use. Activated by passing a string, false otherwise.
1492 * @param array|string $attr Optional. Array or string of attributes. Default empty.
1493 * @return string HTML content.
1495 function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
1496 $_post = get_post( $id );
1498 if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) ) {
1499 return __( 'Missing Attachment' );
1503 $url = get_attachment_link( $_post->ID );
1508 } elseif ( $size && 'none' != $size ) {
1509 $link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
1514 if ( '' === trim( $link_text ) ) {
1515 $link_text = $_post->post_title;
1518 if ( '' === trim( $link_text ) ) {
1519 $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
1522 * Filters a retrieved attachment page link.
1526 * @param string $link_html The page link HTML output.
1527 * @param int $id Post ID.
1528 * @param string|array $size Size of the image. Image size or array of width and height values (in that order).
1529 * Default 'thumbnail'.
1530 * @param bool $permalink Whether to add permalink to image. Default false.
1531 * @param bool $icon Whether to include an icon. Default false.
1532 * @param string|bool $text If string, will be link text. Default false.
1534 return apply_filters( 'wp_get_attachment_link', "<a href='" . esc_url( $url ) . "'>$link_text</a>", $id, $size, $permalink, $icon, $text );
1538 * Wrap attachment in paragraph tag before content.
1542 * @param string $content
1545 function prepend_attachment($content) {
1548 if ( empty($post->post_type) || $post->post_type != 'attachment' )
1551 if ( wp_attachment_is( 'video', $post ) ) {
1552 $meta = wp_get_attachment_metadata( get_the_ID() );
1553 $atts = array( 'src' => wp_get_attachment_url() );
1554 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
1555 $atts['width'] = (int) $meta['width'];
1556 $atts['height'] = (int) $meta['height'];
1558 if ( has_post_thumbnail() ) {
1559 $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
1561 $p = wp_video_shortcode( $atts );
1562 } elseif ( wp_attachment_is( 'audio', $post ) ) {
1563 $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
1565 $p = '<p class="attachment">';
1566 // show the medium sized image representation of the attachment if available, and link to the raw file
1567 $p .= wp_get_attachment_link(0, 'medium', false);
1572 * Filters the attachment markup to be prepended to the post content.
1576 * @see prepend_attachment()
1578 * @param string $p The attachment HTML output.
1580 $p = apply_filters( 'prepend_attachment', $p );
1582 return "$p\n$content";
1590 * Retrieve protected post password form content.
1594 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
1595 * @return string HTML content for password form for password protected post.
1597 function get_the_password_form( $post = 0 ) {
1598 $post = get_post( $post );
1599 $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
1600 $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
1601 <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
1602 <p><label for="' . $label . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
1606 * Filters the HTML output for the protected post password form.
1608 * If modifying the password field, please note that the core database schema
1609 * limits the password field to 20 characters regardless of the value of the
1610 * size attribute in the form input.
1614 * @param string $output The password form HTML output.
1616 return apply_filters( 'the_password_form', $output );
1620 * Whether currently in a page template.
1622 * This template tag allows you to determine if you are in a page template.
1623 * You can optionally provide a template name or array of template names
1624 * and then the check will be specific to that template.
1627 * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
1628 * @since 4.7.0 Now works with any post type, not just pages.
1630 * @param string|array $template The specific template name or array of templates to match.
1631 * @return bool True on success, false on failure.
1633 function is_page_template( $template = '' ) {
1634 if ( ! is_singular() ) {
1638 $page_template = get_page_template_slug( get_queried_object_id() );
1640 if ( empty( $template ) )
1641 return (bool) $page_template;
1643 if ( $template == $page_template )
1646 if ( is_array( $template ) ) {
1647 if ( ( in_array( 'default', $template, true ) && ! $page_template )
1648 || in_array( $page_template, $template, true )
1654 return ( 'default' === $template && ! $page_template );
1658 * Get the specific template name for a given post.
1661 * @since 4.7.0 Now works with any post type, not just pages.
1663 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
1664 * @return string|false Page template filename. Returns an empty string when the default page template
1665 * is in use. Returns false if the post does not exist.
1667 function get_page_template_slug( $post = null ) {
1668 $post = get_post( $post );
1674 $template = get_post_meta( $post->ID, '_wp_page_template', true );
1676 if ( ! $template || 'default' == $template ) {
1684 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1688 * @param int|object $revision Revision ID or revision object.
1689 * @param bool $link Optional, default is true. Link to revisions's page?
1690 * @return string|false i18n formatted datetimestamp or localized 'Current Revision'.
1692 function wp_post_revision_title( $revision, $link = true ) {
1693 if ( !$revision = get_post( $revision ) )
1696 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1699 /* translators: revision date format, see https://secure.php.net/date */
1700 $datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
1701 /* translators: %s: revision date */
1702 $autosavef = __( '%s [Autosave]' );
1703 /* translators: %s: revision date */
1704 $currentf = __( '%s [Current Revision]' );
1706 $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1707 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1708 $date = "<a href='$link'>$date</a>";
1710 if ( !wp_is_post_revision( $revision ) )
1711 $date = sprintf( $currentf, $date );
1712 elseif ( wp_is_post_autosave( $revision ) )
1713 $date = sprintf( $autosavef, $date );
1719 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1723 * @param int|object $revision Revision ID or revision object.
1724 * @param bool $link Optional, default is true. Link to revisions's page?
1725 * @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
1727 function wp_post_revision_title_expanded( $revision, $link = true ) {
1728 if ( !$revision = get_post( $revision ) )
1731 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1734 $author = get_the_author_meta( 'display_name', $revision->post_author );
1735 /* translators: revision date format, see https://secure.php.net/date */
1736 $datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
1738 $gravatar = get_avatar( $revision->post_author, 24 );
1740 $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1741 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1742 $date = "<a href='$link'>$date</a>";
1744 $revision_date_author = sprintf(
1745 /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
1746 __( '%1$s %2$s, %3$s ago (%4$s)' ),
1749 human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
1753 /* translators: %s: revision date with author avatar */
1754 $autosavef = __( '%s [Autosave]' );
1755 /* translators: %s: revision date with author avatar */
1756 $currentf = __( '%s [Current Revision]' );
1758 if ( !wp_is_post_revision( $revision ) )
1759 $revision_date_author = sprintf( $currentf, $revision_date_author );
1760 elseif ( wp_is_post_autosave( $revision ) )
1761 $revision_date_author = sprintf( $autosavef, $revision_date_author );
1764 * Filters the formatted author and date for a revision.
1768 * @param string $revision_date_author The formatted string.
1769 * @param WP_Post $revision The revision object.
1770 * @param bool $link Whether to link to the revisions page, as passed into
1771 * wp_post_revision_title_expanded().
1773 return apply_filters( 'wp_post_revision_title_expanded', $revision_date_author, $revision, $link );
1777 * Display list of a post's revisions.
1779 * Can output either a UL with edit links or a TABLE with diff interface, and
1780 * restore action links.
1784 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
1785 * @param string $type 'all' (default), 'revision' or 'autosave'
1787 function wp_list_post_revisions( $post_id = 0, $type = 'all' ) {
1788 if ( ! $post = get_post( $post_id ) )
1791 // $args array with (parent, format, right, left, type) deprecated since 3.6
1792 if ( is_array( $type ) ) {
1793 $type = ! empty( $type['type'] ) ? $type['type'] : $type;
1794 _deprecated_argument( __FUNCTION__, '3.6.0' );
1797 if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
1801 foreach ( $revisions as $revision ) {
1802 if ( ! current_user_can( 'read_post', $revision->ID ) )
1805 $is_autosave = wp_is_post_autosave( $revision );
1806 if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) )
1809 $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n";
1812 echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n";
1814 echo "<ul class='post-revisions hide-if-no-js'>\n";