3 * WordPress Link Template Functions
10 * Display the permalink for the current post.
13 * @since 4.4.0 Added the `$post` parameter.
15 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
17 function the_permalink( $post = 0 ) {
19 * Filter the display of the permalink for the current post.
22 * @since 4.4.0 Added the `$post` parameter.
24 * @param string $permalink The permalink for the current post.
25 * @param int|WP_Post $post Post ID, WP_Post object, or 0. Default 0.
27 echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
31 * Retrieve trailing slash string, if site is set for adding trailing slashes.
33 * Conditionally adds a trailing slash if the permalink structure has a trailing
34 * slash, strips the trailing slash if not. The string is passed through the
35 * 'user_trailingslashit' filter. Will remove trailing slash from string, if
36 * site is not set to have them.
39 * @global WP_Rewrite $wp_rewrite
41 * @param string $string URL with or without a trailing slash.
42 * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
43 * @return string The URL with the trailing slash appended or stripped.
45 function user_trailingslashit($string, $type_of_url = '') {
47 if ( $wp_rewrite->use_trailing_slashes )
48 $string = trailingslashit($string);
50 $string = untrailingslashit($string);
53 * Filter the trailing slashed string, depending on whether the site is set
54 * to use training slashes.
58 * @param string $string URL with or without a trailing slash.
59 * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
60 * 'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',
61 * 'month', 'day', 'paged', 'post_type_archive'.
63 return apply_filters( 'user_trailingslashit', $string, $type_of_url );
67 * Display permalink anchor for current post.
69 * The permalink mode title will use the post title for the 'a' element 'id'
70 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
74 * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
76 function permalink_anchor( $mode = 'id' ) {
78 switch ( strtolower( $mode ) ) {
80 $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
81 echo '<a id="'.$title.'"></a>';
85 echo '<a id="post-' . $post->ID . '"></a>';
91 * Retrieve full permalink for current post or post ID.
93 * This function is an alias for get_permalink().
97 * @see get_permalink()
99 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
100 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
102 * @return string|false The permalink URL or false if post does not exist.
104 function get_the_permalink( $post = 0, $leavename = false ) {
105 return get_permalink( $post, $leavename );
109 * Retrieve full permalink for current post or post ID.
113 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
114 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
115 * @return string|false The permalink URL or false if post does not exist.
117 function get_permalink( $post = 0, $leavename = false ) {
118 $rewritecode = array(
125 $leavename? '' : '%postname%',
129 $leavename? '' : '%pagename%',
132 if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) {
135 $post = get_post( $post );
139 if ( empty($post->ID) )
142 if ( $post->post_type == 'page' )
143 return get_page_link($post, $leavename, $sample);
144 elseif ( $post->post_type == 'attachment' )
145 return get_attachment_link( $post, $leavename );
146 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
147 return get_post_permalink($post, $leavename, $sample);
149 $permalink = get_option('permalink_structure');
152 * Filter the permalink structure for a post before token replacement occurs.
154 * Only applies to posts with post_type of 'post'.
158 * @param string $permalink The site's permalink structure.
159 * @param WP_Post $post The post in question.
160 * @param bool $leavename Whether to keep the post name.
162 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
164 if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
165 $unixtime = strtotime($post->post_date);
168 if ( strpos($permalink, '%category%') !== false ) {
169 $cats = get_the_category($post->ID);
171 usort($cats, '_usort_terms_by_ID'); // order by ID
174 * Filter the category that gets used in the %category% permalink token.
178 * @param stdClass $cat The category to use in the permalink.
179 * @param array $cats Array of all categories associated with the post.
180 * @param WP_Post $post The post in question.
182 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
184 $category_object = get_term( $category_object, 'category' );
185 $category = $category_object->slug;
186 if ( $parent = $category_object->parent )
187 $category = get_category_parents($parent, false, '/', true) . $category;
189 // show default category in permalinks, without
190 // having to assign it explicitly
191 if ( empty($category) ) {
192 $default_category = get_term( get_option( 'default_category' ), 'category' );
193 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
198 if ( strpos($permalink, '%author%') !== false ) {
199 $authordata = get_userdata($post->post_author);
200 $author = $authordata->user_nicename;
203 $date = explode(" ",date('Y m d H i s', $unixtime));
218 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
219 $permalink = user_trailingslashit($permalink, 'single');
220 } else { // if they're not using the fancy permalink option
221 $permalink = home_url('?p=' . $post->ID);
225 * Filter the permalink for a post.
227 * Only applies to posts with post_type of 'post'.
231 * @param string $permalink The post's permalink.
232 * @param WP_Post $post The post in question.
233 * @param bool $leavename Whether to keep the post name.
235 return apply_filters( 'post_link', $permalink, $post, $leavename );
239 * Retrieve the permalink for a post with a custom post type.
243 * @global WP_Rewrite $wp_rewrite
245 * @param int $id Optional. Post ID.
246 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
247 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
248 * @return string|WP_Error The post permalink.
250 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
253 $post = get_post($id);
255 if ( is_wp_error( $post ) )
258 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
260 $slug = $post->post_name;
262 $draft_or_pending = get_post_status( $id ) && in_array( get_post_status( $id ), array( 'draft', 'pending', 'auto-draft', 'future' ) );
264 $post_type = get_post_type_object($post->post_type);
266 if ( $post_type->hierarchical ) {
267 $slug = get_page_uri( $id );
270 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
271 if ( ! $leavename ) {
272 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
274 $post_link = home_url( user_trailingslashit($post_link) );
276 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
277 $post_link = add_query_arg($post_type->query_var, $slug, '');
279 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
280 $post_link = home_url($post_link);
284 * Filter the permalink for a post with a custom post type.
288 * @param string $post_link The post's permalink.
289 * @param WP_Post $post The post in question.
290 * @param bool $leavename Whether to keep the post name.
291 * @param bool $sample Is it a sample permalink.
293 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
297 * Retrieve the permalink for current page or page ID.
299 * Respects page_on_front. Use this one.
303 * @param int|object $post Optional. Post ID or object.
304 * @param bool $leavename Optional, defaults to false. Whether to keep page name.
305 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
306 * @return string The page permalink.
308 function get_page_link( $post = false, $leavename = false, $sample = false ) {
309 $post = get_post( $post );
311 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
312 $link = home_url('/');
314 $link = _get_page_link( $post, $leavename, $sample );
317 * Filter the permalink for a page.
321 * @param string $link The page's permalink.
322 * @param int $post_id The ID of the page.
323 * @param bool $sample Is it a sample permalink.
325 return apply_filters( 'page_link', $link, $post->ID, $sample );
329 * Retrieve the page permalink.
331 * Ignores page_on_front. Internal use only.
336 * @global WP_Rewrite $wp_rewrite
338 * @param int|object $post Optional. Post ID or object.
339 * @param bool $leavename Optional. Leave name.
340 * @param bool $sample Optional. Sample permalink.
341 * @return string The page permalink.
343 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
346 $post = get_post( $post );
348 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
350 $link = $wp_rewrite->get_page_permastruct();
352 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
353 if ( ! $leavename ) {
354 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
357 $link = home_url($link);
358 $link = user_trailingslashit($link, 'page');
360 $link = home_url( '?page_id=' . $post->ID );
364 * Filter the permalink for a non-page_on_front page.
368 * @param string $link The page's permalink.
369 * @param int $post_id The ID of the page.
371 return apply_filters( '_get_page_link', $link, $post->ID );
375 * Retrieve permalink for attachment.
377 * This can be used in the WordPress Loop or outside of it.
381 * @global WP_Rewrite $wp_rewrite
383 * @param int|object $post Optional. Post ID or object.
384 * @param bool $leavename Optional. Leave name.
385 * @return string The attachment permalink.
387 function get_attachment_link( $post = null, $leavename = false ) {
392 $post = get_post( $post );
393 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
394 if ( $parent && ! in_array( $parent->post_type, get_post_types() ) ) {
398 if ( $wp_rewrite->using_permalinks() && $parent ) {
399 if ( 'page' == $parent->post_type )
400 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
402 $parentlink = get_permalink( $post->post_parent );
404 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
405 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
407 $name = $post->post_name;
409 if ( strpos($parentlink, '?') === false )
410 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
413 $link = str_replace( '%postname%', $name, $link );
414 } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
415 $link = home_url( user_trailingslashit( $post->post_name ) );
419 $link = home_url( '/?attachment_id=' . $post->ID );
422 * Filter the permalink for an attachment.
426 * @param string $link The attachment's permalink.
427 * @param int $post_id Attachment ID.
429 return apply_filters( 'attachment_link', $link, $post->ID );
433 * Retrieve the permalink for the year archives.
437 * @global WP_Rewrite $wp_rewrite
439 * @param int|bool $year False for current year or year for permalink.
440 * @return string The permalink for the specified year archive.
442 function get_year_link($year) {
445 $year = gmdate('Y', current_time('timestamp'));
446 $yearlink = $wp_rewrite->get_year_permastruct();
447 if ( !empty($yearlink) ) {
448 $yearlink = str_replace('%year%', $year, $yearlink);
449 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
451 $yearlink = home_url( '?m=' . $year );
455 * Filter the year archive permalink.
459 * @param string $yearlink Permalink for the year archive.
460 * @param int $year Year for the archive.
462 return apply_filters( 'year_link', $yearlink, $year );
466 * Retrieve the permalink for the month archives with year.
470 * @global WP_Rewrite $wp_rewrite
472 * @param bool|int $year False for current year. Integer of year.
473 * @param bool|int $month False for current month. Integer of month.
474 * @return string The permalink for the specified month and year archive.
476 function get_month_link($year, $month) {
479 $year = gmdate('Y', current_time('timestamp'));
481 $month = gmdate('m', current_time('timestamp'));
482 $monthlink = $wp_rewrite->get_month_permastruct();
483 if ( !empty($monthlink) ) {
484 $monthlink = str_replace('%year%', $year, $monthlink);
485 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
486 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
488 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
492 * Filter the month archive permalink.
496 * @param string $monthlink Permalink for the month archive.
497 * @param int $year Year for the archive.
498 * @param int $month The month for the archive.
500 return apply_filters( 'month_link', $monthlink, $year, $month );
504 * Retrieve the permalink for the day archives with year and month.
508 * @global WP_Rewrite $wp_rewrite
510 * @param bool|int $year False for current year. Integer of year.
511 * @param bool|int $month False for current month. Integer of month.
512 * @param bool|int $day False for current day. Integer of day.
513 * @return string The permalink for the specified day, month, and year archive.
515 function get_day_link($year, $month, $day) {
518 $year = gmdate('Y', current_time('timestamp'));
520 $month = gmdate('m', current_time('timestamp'));
522 $day = gmdate('j', current_time('timestamp'));
524 $daylink = $wp_rewrite->get_day_permastruct();
525 if ( !empty($daylink) ) {
526 $daylink = str_replace('%year%', $year, $daylink);
527 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
528 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
529 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
531 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
535 * Filter the day archive permalink.
539 * @param string $daylink Permalink for the day archive.
540 * @param int $year Year for the archive.
541 * @param int $month Month for the archive.
542 * @param int $day The day for the archive.
544 return apply_filters( 'day_link', $daylink, $year, $month, $day );
548 * Display the permalink for the feed type.
552 * @param string $anchor The link's anchor text.
553 * @param string $feed Optional, defaults to default feed. Feed type.
555 function the_feed_link( $anchor, $feed = '' ) {
556 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
559 * Filter the feed link anchor tag.
563 * @param string $link The complete anchor tag for a feed link.
564 * @param string $feed The feed type, or an empty string for the
567 echo apply_filters( 'the_feed_link', $link, $feed );
571 * Retrieve the permalink for the feed type.
575 * @global WP_Rewrite $wp_rewrite
577 * @param string $feed Optional, defaults to default feed. Feed type.
578 * @return string The feed permalink.
580 function get_feed_link($feed = '') {
583 $permalink = $wp_rewrite->get_feed_permastruct();
584 if ( '' != $permalink ) {
585 if ( false !== strpos($feed, 'comments_') ) {
586 $feed = str_replace('comments_', '', $feed);
587 $permalink = $wp_rewrite->get_comment_feed_permastruct();
590 if ( get_default_feed() == $feed )
593 $permalink = str_replace('%feed%', $feed, $permalink);
594 $permalink = preg_replace('#/+#', '/', "/$permalink");
595 $output = home_url( user_trailingslashit($permalink, 'feed') );
598 $feed = get_default_feed();
600 if ( false !== strpos($feed, 'comments_') )
601 $feed = str_replace('comments_', 'comments-', $feed);
603 $output = home_url("?feed={$feed}");
607 * Filter the feed type permalink.
611 * @param string $output The feed permalink.
612 * @param string $feed Feed type.
614 return apply_filters( 'feed_link', $output, $feed );
618 * Retrieve the permalink for the post comments feed.
622 * @param int $post_id Optional. Post ID.
623 * @param string $feed Optional. Feed type.
624 * @return string The permalink for the comments feed for the given post.
626 function get_post_comments_feed_link($post_id = 0, $feed = '') {
627 $post_id = absint( $post_id );
630 $post_id = get_the_ID();
632 if ( empty( $feed ) )
633 $feed = get_default_feed();
635 $post = get_post( $post_id );
636 $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent;
638 if ( '' != get_option('permalink_structure') ) {
639 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
640 $url = _get_page_link( $post_id );
642 $url = get_permalink($post_id);
645 $url = home_url( '/feed/' );
646 if ( $feed !== get_default_feed() ) {
649 $url = add_query_arg( 'attachment_id', $post_id, $url );
651 $url = trailingslashit($url) . 'feed';
652 if ( $feed != get_default_feed() )
654 $url = user_trailingslashit($url, 'single_feed');
658 $url = add_query_arg( array( 'feed' => $feed, 'attachment_id' => $post_id ), home_url( '/' ) );
659 } elseif ( 'page' == $post->post_type ) {
660 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
662 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
667 * Filter the post comments feed permalink.
671 * @param string $url Post comments feed permalink.
673 return apply_filters( 'post_comments_feed_link', $url );
677 * Display the comment feed link for a post.
679 * Prints out the comment feed link for a post. Link text is placed in the
680 * anchor. If no link text is specified, default text is used. If no post ID is
681 * specified, the current post is used.
685 * @param string $link_text Descriptive text.
686 * @param int $post_id Optional post ID. Default to current post.
687 * @param string $feed Optional. Feed format.
689 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
690 $url = get_post_comments_feed_link( $post_id, $feed );
691 if ( empty( $link_text ) ) {
692 $link_text = __('Comments Feed');
695 $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>';
697 * Filter the post comment feed link anchor tag.
701 * @param string $link The complete anchor tag for the comment feed link.
702 * @param int $post_id Post ID.
703 * @param string $feed The feed type, or an empty string for the default feed type.
705 echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed );
709 * Retrieve the feed link for a given author.
711 * Returns a link to the feed for all posts by a given author. A specific feed
712 * can be requested or left blank to get the default feed.
716 * @param int $author_id ID of an author.
717 * @param string $feed Optional. Feed type.
718 * @return string Link to the feed for the author specified by $author_id.
720 function get_author_feed_link( $author_id, $feed = '' ) {
721 $author_id = (int) $author_id;
722 $permalink_structure = get_option('permalink_structure');
725 $feed = get_default_feed();
727 if ( '' == $permalink_structure ) {
728 $link = home_url("?feed=$feed&author=" . $author_id);
730 $link = get_author_posts_url($author_id);
731 if ( $feed == get_default_feed() )
734 $feed_link = "feed/$feed";
736 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
740 * Filter the feed link for a given author.
744 * @param string $link The author feed link.
745 * @param string $feed Feed type.
747 $link = apply_filters( 'author_feed_link', $link, $feed );
753 * Retrieve the feed link for a category.
755 * Returns a link to the feed for all posts in a given category. A specific feed
756 * can be requested or left blank to get the default feed.
760 * @param int $cat_id ID of a category.
761 * @param string $feed Optional. Feed type.
762 * @return string Link to the feed for the category specified by $cat_id.
764 function get_category_feed_link( $cat_id, $feed = '' ) {
765 return get_term_feed_link( $cat_id, 'category', $feed );
769 * Retrieve the feed link for a term.
771 * Returns a link to the feed for all posts in a given term. A specific feed
772 * can be requested or left blank to get the default feed.
776 * @param int $term_id ID of a category.
777 * @param string $taxonomy Optional. Taxonomy of $term_id
778 * @param string $feed Optional. Feed type.
779 * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
781 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
782 $term_id = ( int ) $term_id;
784 $term = get_term( $term_id, $taxonomy );
786 if ( empty( $term ) || is_wp_error( $term ) )
789 if ( empty( $feed ) )
790 $feed = get_default_feed();
792 $permalink_structure = get_option( 'permalink_structure' );
794 if ( '' == $permalink_structure ) {
795 if ( 'category' == $taxonomy ) {
796 $link = home_url("?feed=$feed&cat=$term_id");
798 elseif ( 'post_tag' == $taxonomy ) {
799 $link = home_url("?feed=$feed&tag=$term->slug");
801 $t = get_taxonomy( $taxonomy );
802 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
805 $link = get_term_link( $term_id, $term->taxonomy );
806 if ( $feed == get_default_feed() )
809 $feed_link = "feed/$feed";
811 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
814 if ( 'category' == $taxonomy ) {
816 * Filter the category feed link.
820 * @param string $link The category feed link.
821 * @param string $feed Feed type.
823 $link = apply_filters( 'category_feed_link', $link, $feed );
824 } elseif ( 'post_tag' == $taxonomy ) {
826 * Filter the post tag feed link.
830 * @param string $link The tag feed link.
831 * @param string $feed Feed type.
833 $link = apply_filters( 'tag_feed_link', $link, $feed );
836 * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
840 * @param string $link The taxonomy feed link.
841 * @param string $feed Feed type.
842 * @param string $feed The taxonomy name.
844 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
851 * Retrieve permalink for feed of tag.
855 * @param int $tag_id Tag ID.
856 * @param string $feed Optional. Feed type.
857 * @return string The feed permalink for the given tag.
859 function get_tag_feed_link( $tag_id, $feed = '' ) {
860 return get_term_feed_link( $tag_id, 'post_tag', $feed );
864 * Retrieve edit tag link.
868 * @param int $tag_id Tag ID
869 * @param string $taxonomy Taxonomy
870 * @return string The edit tag link URL for the given tag.
872 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
874 * Filter the edit link for a tag (or term in another taxonomy).
878 * @param string $link The term edit link.
880 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
884 * Display or retrieve edit tag link with formatting.
888 * @param string $link Optional. Anchor text.
889 * @param string $before Optional. Display before edit link.
890 * @param string $after Optional. Display after edit link.
891 * @param object $tag Tag object.
893 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
894 $link = edit_term_link( $link, '', '', $tag, false );
897 * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
901 * @param string $link The anchor tag for the edit link.
903 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
907 * Retrieves the URL for editing a given term.
910 * @since 4.5.0 The `$taxonomy` argument was made optional.
912 * @param int $term_id Term ID.
913 * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified by `$term_id`.
914 * @param string $object_type Optional. The object type. Used to highlight the proper post type menu on the linked page.
915 * Defaults to the first object_type associated with the taxonomy.
916 * @return string|null The edit term link URL for the given term, or null on failure.
918 function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
919 $term = get_term( $term_id, $taxonomy );
920 if ( ! $term || is_wp_error( $term ) ) {
924 $tax = get_taxonomy( $term->taxonomy );
925 if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
930 'taxonomy' => $taxonomy,
931 'tag_ID' => $term->term_id,
934 if ( $object_type ) {
935 $args['post_type'] = $object_type;
936 } elseif ( ! empty( $tax->object_type ) ) {
937 $args['post_type'] = reset( $tax->object_type );
940 if ( $tax->show_ui ) {
941 $location = add_query_arg( $args, admin_url( 'term.php' ) );
947 * Filter the edit link for a term.
951 * @param string $location The edit link.
952 * @param int $term_id Term ID.
953 * @param string $taxonomy Taxonomy name.
954 * @param string $object_type The object type (eg. the post type).
956 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
960 * Display or retrieve edit term link with formatting.
964 * @param string $link Optional. Anchor text. Default empty.
965 * @param string $before Optional. Display before edit link. Default empty.
966 * @param string $after Optional. Display after edit link. Default empty.
967 * @param object $term Optional. Term object. If null, the queried object will be inspected. Default null.
968 * @param bool $echo Optional. Whether or not to echo the return. Default true.
969 * @return string|void HTML content.
971 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
972 if ( is_null( $term ) )
973 $term = get_queried_object();
978 $tax = get_taxonomy( $term->taxonomy );
979 if ( ! current_user_can( $tax->cap->edit_terms ) )
982 if ( empty( $link ) )
983 $link = __('Edit This');
985 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
988 * Filter the anchor tag for the edit link of a term.
992 * @param string $link The anchor tag for the edit link.
993 * @param int $term_id Term ID.
995 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
1004 * Retrieve permalink for search.
1008 * @global WP_Rewrite $wp_rewrite
1010 * @param string $query Optional. The query string to use. If empty the current query is used.
1011 * @return string The search permalink.
1013 function get_search_link( $query = '' ) {
1016 if ( empty($query) )
1017 $search = get_search_query( false );
1019 $search = stripslashes($query);
1021 $permastruct = $wp_rewrite->get_search_permastruct();
1023 if ( empty( $permastruct ) ) {
1024 $link = home_url('?s=' . urlencode($search) );
1026 $search = urlencode($search);
1027 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it un-encoded.
1028 $link = str_replace( '%search%', $search, $permastruct );
1029 $link = home_url( user_trailingslashit( $link, 'search' ) );
1033 * Filter the search permalink.
1037 * @param string $link Search permalink.
1038 * @param string $search The URL-encoded search term.
1040 return apply_filters( 'search_link', $link, $search );
1044 * Retrieve the permalink for the feed of the search results.
1048 * @global WP_Rewrite $wp_rewrite
1050 * @param string $search_query Optional. Search query.
1051 * @param string $feed Optional. Feed type.
1052 * @return string The search results feed permalink.
1054 function get_search_feed_link($search_query = '', $feed = '') {
1056 $link = get_search_link($search_query);
1059 $feed = get_default_feed();
1061 $permastruct = $wp_rewrite->get_search_permastruct();
1063 if ( empty($permastruct) ) {
1064 $link = add_query_arg('feed', $feed, $link);
1066 $link = trailingslashit($link);
1067 $link .= "feed/$feed/";
1071 * Filter the search feed link.
1075 * @param string $link Search feed link.
1076 * @param string $feed Feed type.
1077 * @param string $type The search type. One of 'posts' or 'comments'.
1079 return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1083 * Retrieve the permalink for the comments feed of the search results.
1087 * @global WP_Rewrite $wp_rewrite
1089 * @param string $search_query Optional. Search query.
1090 * @param string $feed Optional. Feed type.
1091 * @return string The comments feed search results permalink.
1093 function get_search_comments_feed_link($search_query = '', $feed = '') {
1097 $feed = get_default_feed();
1099 $link = get_search_feed_link($search_query, $feed);
1101 $permastruct = $wp_rewrite->get_search_permastruct();
1103 if ( empty($permastruct) )
1104 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1106 $link = add_query_arg('withcomments', 1, $link);
1108 /** This filter is documented in wp-includes/link-template.php */
1109 return apply_filters( 'search_feed_link', $link, $feed, 'comments' );
1113 * Retrieves the permalink for a post type archive.
1116 * @since 4.5.0 Support for posts was added.
1118 * @global WP_Rewrite $wp_rewrite
1120 * @param string $post_type Post type.
1121 * @return string|false The post type archive permalink.
1123 function get_post_type_archive_link( $post_type ) {
1125 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1128 if ( 'post' === $post_type ) {
1129 $show_on_front = get_option( 'show_on_front' );
1130 $page_for_posts = get_option( 'page_for_posts' );
1132 if ( 'page' == $show_on_front && $page_for_posts ) {
1133 $link = get_permalink( $page_for_posts );
1135 $link = get_home_url();
1137 /** This filter is documented in wp-includes/link-template.php */
1138 return apply_filters( 'post_type_archive_link', $link, $post_type );
1141 if ( ! $post_type_obj->has_archive )
1144 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1145 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1146 if ( $post_type_obj->rewrite['with_front'] )
1147 $struct = $wp_rewrite->front . $struct;
1149 $struct = $wp_rewrite->root . $struct;
1150 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1152 $link = home_url( '?post_type=' . $post_type );
1156 * Filter the post type archive permalink.
1160 * @param string $link The post type archive permalink.
1161 * @param string $post_type Post type name.
1163 return apply_filters( 'post_type_archive_link', $link, $post_type );
1167 * Retrieve the permalink for a post type archive feed.
1171 * @param string $post_type Post type
1172 * @param string $feed Optional. Feed type
1173 * @return string|false The post type feed permalink.
1175 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1176 $default_feed = get_default_feed();
1177 if ( empty( $feed ) )
1178 $feed = $default_feed;
1180 if ( ! $link = get_post_type_archive_link( $post_type ) )
1183 $post_type_obj = get_post_type_object( $post_type );
1184 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1185 $link = trailingslashit( $link );
1187 if ( $feed != $default_feed )
1190 $link = add_query_arg( 'feed', $feed, $link );
1194 * Filter the post type archive feed link.
1198 * @param string $link The post type archive feed link.
1199 * @param string $feed Feed type.
1201 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1205 * Retrieve URL used for the post preview.
1207 * Get the preview post URL. Allows additional query args to be appended.
1211 * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global post.
1212 * @param array $query_args Optional. Array of additional query args to be appended to the link.
1213 * @param string $preview_link Optional. Base preview link to be used if it should differ from the post permalink.
1214 * @return string URL used for the post preview.
1216 function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
1217 $post = get_post( $post );
1222 $post_type_object = get_post_type_object( $post->post_type );
1223 if ( is_post_type_viewable( $post_type_object ) ) {
1224 if ( ! $preview_link ) {
1225 $preview_link = set_url_scheme( get_permalink( $post ) );
1228 $query_args['preview'] = 'true';
1229 $preview_link = add_query_arg( $query_args, $preview_link );
1233 * Filter the URL used for a post preview.
1236 * @since 4.0.0 Added the `$post` parameter.
1238 * @param string $preview_link URL used for the post preview.
1239 * @param WP_Post $post Post object.
1241 return apply_filters( 'preview_post_link', $preview_link, $post );
1245 * Retrieve edit posts link for post.
1247 * Can be used within the WordPress loop or outside of it. Can be used with
1248 * pages, posts, attachments, and revisions.
1252 * @param int $id Optional. Post ID.
1253 * @param string $context Optional, defaults to display. How to write the '&', defaults to '&'.
1254 * @return string|null The edit post link for the given post. null if the post type is invalid or does
1255 * not allow an editing UI.
1257 function get_edit_post_link( $id = 0, $context = 'display' ) {
1258 if ( ! $post = get_post( $id ) )
1261 if ( 'revision' === $post->post_type )
1263 elseif ( 'display' == $context )
1264 $action = '&action=edit';
1266 $action = '&action=edit';
1268 $post_type_object = get_post_type_object( $post->post_type );
1269 if ( !$post_type_object )
1272 if ( !current_user_can( 'edit_post', $post->ID ) )
1275 if ( $post_type_object->_edit_link ) {
1276 $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
1282 * Filter the post edit link.
1286 * @param string $link The edit link.
1287 * @param int $post_id Post ID.
1288 * @param string $context The link context. If set to 'display' then ampersands
1291 return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
1295 * Display edit post link for post.
1298 * @since 4.4.0 The `$class` argument was added.
1300 * @param string $text Optional. Anchor text.
1301 * @param string $before Optional. Display before edit link.
1302 * @param string $after Optional. Display after edit link.
1303 * @param int $id Optional. Post ID.
1304 * @param string $class Optional. Add custom class to link.
1306 function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
1307 if ( ! $post = get_post( $id ) ) {
1311 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1315 if ( null === $text ) {
1316 $text = __( 'Edit This' );
1319 $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
1322 * Filter the post edit link anchor tag.
1326 * @param string $link Anchor tag for the edit link.
1327 * @param int $post_id Post ID.
1328 * @param string $text Anchor text.
1330 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1334 * Retrieve delete posts link for post.
1336 * Can be used within the WordPress loop or outside of it, with any post type.
1340 * @param int $id Optional. Post ID.
1341 * @param string $deprecated Not used.
1342 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1343 * @return string|void The delete post link URL for the given post.
1345 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1346 if ( ! empty( $deprecated ) )
1347 _deprecated_argument( __FUNCTION__, '3.0' );
1349 if ( !$post = get_post( $id ) )
1352 $post_type_object = get_post_type_object( $post->post_type );
1353 if ( !$post_type_object )
1356 if ( !current_user_can( 'delete_post', $post->ID ) )
1359 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1361 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1364 * Filter the post delete link.
1368 * @param string $link The delete link.
1369 * @param int $post_id Post ID.
1370 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1372 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1376 * Retrieve edit comment link.
1380 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
1381 * @return string|void The edit comment link URL for the given comment.
1383 function get_edit_comment_link( $comment_id = 0 ) {
1384 $comment = get_comment( $comment_id );
1386 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1389 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1392 * Filter the comment edit link.
1396 * @param string $location The edit link.
1398 return apply_filters( 'get_edit_comment_link', $location );
1402 * Display edit comment link with formatting.
1406 * @param string $text Optional. Anchor text.
1407 * @param string $before Optional. Display before edit link.
1408 * @param string $after Optional. Display after edit link.
1410 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1411 $comment = get_comment();
1413 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1417 if ( null === $text ) {
1418 $text = __( 'Edit This' );
1421 $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
1424 * Filter the comment edit link anchor tag.
1428 * @param string $link Anchor tag for the edit link.
1429 * @param int $comment_id Comment ID.
1430 * @param string $text Anchor text.
1432 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1436 * Display edit bookmark link.
1440 * @param int|stdClass $link Optional. Bookmark ID.
1441 * @return string|void The edit bookmark link URL.
1443 function get_edit_bookmark_link( $link = 0 ) {
1444 $link = get_bookmark( $link );
1446 if ( !current_user_can('manage_links') )
1449 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1452 * Filter the bookmark edit link.
1456 * @param string $location The edit link.
1457 * @param int $link_id Bookmark ID.
1459 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1463 * Display edit bookmark link anchor content.
1467 * @param string $link Optional. Anchor text.
1468 * @param string $before Optional. Display before edit link.
1469 * @param string $after Optional. Display after edit link.
1470 * @param int $bookmark Optional. Bookmark ID.
1472 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1473 $bookmark = get_bookmark($bookmark);
1475 if ( !current_user_can('manage_links') )
1479 $link = __('Edit This');
1481 $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>';
1484 * Filter the bookmark edit link anchor tag.
1488 * @param string $link Anchor tag for the edit link.
1489 * @param int $link_id Bookmark ID.
1491 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1495 * Retrieve edit user link
1499 * @param int $user_id Optional. User ID. Defaults to the current user.
1500 * @return string URL to edit user page or empty string.
1502 function get_edit_user_link( $user_id = null ) {
1504 $user_id = get_current_user_id();
1506 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1509 $user = get_userdata( $user_id );
1514 if ( get_current_user_id() == $user->ID )
1515 $link = get_edit_profile_url( $user->ID );
1517 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1520 * Filter the user edit link.
1524 * @param string $link The edit link.
1525 * @param int $user_id User ID.
1527 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1533 * Retrieve previous post that is adjacent to current post.
1537 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1538 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1539 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1540 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1542 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1543 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1547 * Retrieve next post that is adjacent to current post.
1551 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1552 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1553 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1554 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1556 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1557 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1561 * Retrieve adjacent post.
1563 * Can either be next or previous post.
1567 * @global wpdb $wpdb WordPress database abstraction object.
1569 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1570 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1571 * @param bool $previous Optional. Whether to retrieve previous post.
1572 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1573 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1575 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1578 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1581 $current_post_date = $post->post_date;
1585 $adjacent = $previous ? 'previous' : 'next';
1587 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1588 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1589 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1590 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1591 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1592 $excluded_terms = explode( ' and ', $excluded_terms );
1594 $excluded_terms = explode( ',', $excluded_terms );
1597 $excluded_terms = array_map( 'intval', $excluded_terms );
1600 if ( $in_same_term ) {
1601 $join .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
1602 $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1604 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1606 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1608 // Remove any exclusions from the term array to include.
1609 $term_array = array_diff( $term_array, (array) $excluded_terms );
1610 $term_array = array_map( 'intval', $term_array );
1612 if ( ! $term_array || is_wp_error( $term_array ) )
1615 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1619 * Filter the IDs of terms excluded from adjacent post queries.
1621 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1622 * of adjacency, 'next' or 'previous'.
1626 * @param string $excluded_terms Array of excluded term IDs.
1628 $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
1630 if ( ! empty( $excluded_terms ) ) {
1631 $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
1635 // 'post_status' clause depends on the current user.
1636 if ( is_user_logged_in() ) {
1637 $user_id = get_current_user_id();
1639 $post_type_object = get_post_type_object( $post->post_type );
1640 if ( empty( $post_type_object ) ) {
1641 $post_type_cap = $post->post_type;
1642 $read_private_cap = 'read_private_' . $post_type_cap . 's';
1644 $read_private_cap = $post_type_object->cap->read_private_posts;
1648 * Results should include private posts belonging to the current user, or private posts where the
1649 * current user has the 'read_private_posts' cap.
1651 $private_states = get_post_stati( array( 'private' => true ) );
1652 $where .= " AND ( p.post_status = 'publish'";
1653 foreach ( (array) $private_states as $state ) {
1654 if ( current_user_can( $read_private_cap ) ) {
1655 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1657 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1662 $where .= " AND p.post_status = 'publish'";
1665 $op = $previous ? '<' : '>';
1666 $order = $previous ? 'DESC' : 'ASC';
1669 * Filter the JOIN clause in the SQL for an adjacent post query.
1671 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1672 * of adjacency, 'next' or 'previous'.
1675 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1677 * @param string $join The JOIN clause in the SQL.
1678 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1679 * @param array $excluded_terms Array of excluded term IDs.
1680 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1681 * @param WP_Post $post WP_Post object.
1683 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
1686 * Filter the WHERE clause in the SQL for an adjacent post query.
1688 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1689 * of adjacency, 'next' or 'previous'.
1692 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1694 * @param string $where The `WHERE` clause in the SQL.
1695 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1696 * @param array $excluded_terms Array of excluded term IDs.
1697 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1698 * @param WP_Post $post WP_Post object.
1700 $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );
1703 * Filter the ORDER BY clause in the SQL for an adjacent post query.
1705 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1706 * of adjacency, 'next' or 'previous'.
1709 * @since 4.4.0 Added the `$post` parameter.
1711 * @param string $order_by The `ORDER BY` clause in the SQL.
1712 * @param WP_Post $post WP_Post object.
1714 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post );
1716 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1717 $query_key = 'adjacent_post_' . md5( $query );
1718 $result = wp_cache_get( $query_key, 'counts' );
1719 if ( false !== $result ) {
1721 $result = get_post( $result );
1725 $result = $wpdb->get_var( $query );
1726 if ( null === $result )
1729 wp_cache_set( $query_key, $result, 'counts' );
1732 $result = get_post( $result );
1738 * Get adjacent post relational link.
1740 * Can either be next or previous post relational link.
1744 * @param string $title Optional. Link title format.
1745 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1746 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1747 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1748 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1749 * @return string|void The adjacent post relational link URL.
1751 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1752 if ( $previous && is_attachment() && $post = get_post() )
1753 $post = get_post( $post->post_parent );
1755 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1757 if ( empty( $post ) )
1760 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1762 if ( empty( $post_title ) )
1763 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1765 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1767 $title = str_replace( '%title', $post_title, $title );
1768 $title = str_replace( '%date', $date, $title );
1770 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1771 $link .= esc_attr( $title );
1772 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1774 $adjacent = $previous ? 'previous' : 'next';
1777 * Filter the adjacent post relational link.
1779 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1780 * of adjacency, 'next' or 'previous'.
1784 * @param string $link The relational link.
1786 return apply_filters( "{$adjacent}_post_rel_link", $link );
1790 * Display relational links for the posts adjacent to the current post.
1794 * @param string $title Optional. Link title format.
1795 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1796 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1797 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1799 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1800 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1801 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1805 * Display relational links for the posts adjacent to the current post for single post pages.
1807 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1811 function adjacent_posts_rel_link_wp_head() {
1812 if ( ! is_single() || is_attachment() ) {
1815 adjacent_posts_rel_link();
1819 * Display relational link for the next post adjacent to the current post.
1823 * @param string $title Optional. Link title format.
1824 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1825 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1826 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1828 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1829 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1833 * Display relational link for the previous post adjacent to the current post.
1837 * @param string $title Optional. Link title format.
1838 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1839 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1840 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1842 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1843 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1847 * Retrieve boundary post.
1849 * Boundary being either the first or last post by publish date within the constraints specified
1850 * by $in_same_term or $excluded_terms.
1854 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1855 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1856 * @param bool $start Optional. Whether to retrieve first or last post.
1857 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1858 * @return null|array Array containing the boundary post object if successful, null otherwise.
1860 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1862 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1865 $query_args = array(
1866 'posts_per_page' => 1,
1867 'order' => $start ? 'ASC' : 'DESC',
1868 'update_post_term_cache' => false,
1869 'update_post_meta_cache' => false
1872 $term_array = array();
1874 if ( ! is_array( $excluded_terms ) ) {
1875 if ( ! empty( $excluded_terms ) )
1876 $excluded_terms = explode( ',', $excluded_terms );
1878 $excluded_terms = array();
1881 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1882 if ( $in_same_term )
1883 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1885 if ( ! empty( $excluded_terms ) ) {
1886 $excluded_terms = array_map( 'intval', $excluded_terms );
1887 $excluded_terms = array_diff( $excluded_terms, $term_array );
1889 $inverse_terms = array();
1890 foreach ( $excluded_terms as $excluded_term )
1891 $inverse_terms[] = $excluded_term * -1;
1892 $excluded_terms = $inverse_terms;
1895 $query_args[ 'tax_query' ] = array( array(
1896 'taxonomy' => $taxonomy,
1897 'terms' => array_merge( $term_array, $excluded_terms )
1901 return get_posts( $query_args );
1905 * Get previous post link that is adjacent to the current post.
1909 * @param string $format Optional. Link anchor format.
1910 * @param string $link Optional. Link permalink format.
1911 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1912 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1913 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1914 * @return string The link URL of the previous post in relation to the current post.
1916 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1917 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1921 * Display previous post link that is adjacent to the current post.
1924 * @see get_previous_post_link()
1926 * @param string $format Optional. Link anchor format.
1927 * @param string $link Optional. Link permalink format.
1928 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1929 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1930 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1932 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1933 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1937 * Get next post link that is adjacent to the current post.
1941 * @param string $format Optional. Link anchor format.
1942 * @param string $link Optional. Link permalink format.
1943 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1944 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1945 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1946 * @return string The link URL of the next post in relation to the current post.
1948 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1949 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1953 * Display next post link that is adjacent to the current post.
1956 * @see get_next_post_link()
1958 * @param string $format Optional. Link anchor format.
1959 * @param string $link Optional. Link permalink format.
1960 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1961 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1962 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1964 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1965 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1969 * Get adjacent post link.
1971 * Can be either next post link or previous.
1975 * @param string $format Link anchor format.
1976 * @param string $link Link permalink format.
1977 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1978 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1979 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1980 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1981 * @return string The link URL of the previous or next post in relation to the current post.
1983 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1984 if ( $previous && is_attachment() )
1985 $post = get_post( get_post()->post_parent );
1987 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1992 $title = $post->post_title;
1994 if ( empty( $post->post_title ) )
1995 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1997 /** This filter is documented in wp-includes/post-template.php */
1998 $title = apply_filters( 'the_title', $title, $post->ID );
2000 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
2001 $rel = $previous ? 'prev' : 'next';
2003 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
2004 $inlink = str_replace( '%title', $title, $link );
2005 $inlink = str_replace( '%date', $date, $inlink );
2006 $inlink = $string . $inlink . '</a>';
2008 $output = str_replace( '%link', $inlink, $format );
2011 $adjacent = $previous ? 'previous' : 'next';
2014 * Filter the adjacent post link.
2016 * The dynamic portion of the hook name, `$adjacent`, refers to the type
2017 * of adjacency, 'next' or 'previous'.
2020 * @since 4.2.0 Added the `$adjacent` parameter.
2022 * @param string $output The adjacent post link.
2023 * @param string $format Link anchor format.
2024 * @param string $link Link permalink format.
2025 * @param WP_Post $post The adjacent post.
2026 * @param string $adjacent Whether the post is previous or next.
2028 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
2032 * Display adjacent post link.
2034 * Can be either next post link or previous.
2038 * @param string $format Link anchor format.
2039 * @param string $link Link permalink format.
2040 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
2041 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
2042 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
2043 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
2045 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
2046 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
2050 * Retrieve links for page numbers.
2054 * @global WP_Rewrite $wp_rewrite
2056 * @param int $pagenum Optional. Page ID.
2057 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
2058 * Otherwise, prepares the URL with esc_url_raw().
2059 * @return string The link URL for the given page number.
2061 function get_pagenum_link($pagenum = 1, $escape = true ) {
2064 $pagenum = (int) $pagenum;
2066 $request = remove_query_arg( 'paged' );
2068 $home_root = parse_url(home_url());
2069 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
2070 $home_root = preg_quote( $home_root, '|' );
2072 $request = preg_replace('|^'. $home_root . '|i', '', $request);
2073 $request = preg_replace('|^/+|', '', $request);
2075 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
2076 $base = trailingslashit( get_bloginfo( 'url' ) );
2078 if ( $pagenum > 1 ) {
2079 $result = add_query_arg( 'paged', $pagenum, $base . $request );
2081 $result = $base . $request;
2084 $qs_regex = '|\?.*?$|';
2085 preg_match( $qs_regex, $request, $qs_match );
2087 if ( !empty( $qs_match[0] ) ) {
2088 $query_string = $qs_match[0];
2089 $request = preg_replace( $qs_regex, '', $request );
2094 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
2095 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
2096 $request = ltrim($request, '/');
2098 $base = trailingslashit( get_bloginfo( 'url' ) );
2100 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
2101 $base .= $wp_rewrite->index . '/';
2103 if ( $pagenum > 1 ) {
2104 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
2107 $result = $base . $request . $query_string;
2111 * Filter the page number link for the current request.
2115 * @param string $result The page number link.
2117 $result = apply_filters( 'get_pagenum_link', $result );
2120 return esc_url( $result );
2122 return esc_url_raw( $result );
2126 * Retrieve next posts page link.
2128 * Backported from 2.1.3 to 2.0.10.
2132 * @global int $paged
2134 * @param int $max_page Optional. Max pages.
2135 * @return string|void The link URL for next posts page.
2137 function get_next_posts_page_link($max_page = 0) {
2140 if ( !is_single() ) {
2143 $nextpage = intval($paged) + 1;
2144 if ( !$max_page || $max_page >= $nextpage )
2145 return get_pagenum_link($nextpage);
2150 * Display or return the next posts page link.
2154 * @param int $max_page Optional. Max pages.
2155 * @param bool $echo Optional. Echo or return;
2156 * @return string|void The link URL for next posts page if `$echo = false`.
2158 function next_posts( $max_page = 0, $echo = true ) {
2159 $output = esc_url( get_next_posts_page_link( $max_page ) );
2168 * Return the next posts page link.
2172 * @global int $paged
2173 * @global WP_Query $wp_query
2175 * @param string $label Content for link text.
2176 * @param int $max_page Optional. Max pages.
2177 * @return string|void HTML-formatted next posts page link.
2179 function get_next_posts_link( $label = null, $max_page = 0 ) {
2180 global $paged, $wp_query;
2183 $max_page = $wp_query->max_num_pages;
2188 $nextpage = intval($paged) + 1;
2190 if ( null === $label )
2191 $label = __( 'Next Page »' );
2193 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2195 * Filter the anchor tag attributes for the next posts page link.
2199 * @param string $attributes Attributes for the anchor tag.
2201 $attr = apply_filters( 'next_posts_link_attributes', '' );
2203 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2208 * Display the next posts page link.
2212 * @param string $label Content for link text.
2213 * @param int $max_page Optional. Max pages.
2215 function next_posts_link( $label = null, $max_page = 0 ) {
2216 echo get_next_posts_link( $label, $max_page );
2220 * Retrieve previous posts page link.
2222 * Will only return string, if not on a single page or post.
2224 * Backported to 2.0.10 from 2.1.3.
2228 * @global int $paged
2230 * @return string|void The link for the previous posts page.
2232 function get_previous_posts_page_link() {
2235 if ( !is_single() ) {
2236 $nextpage = intval($paged) - 1;
2237 if ( $nextpage < 1 )
2239 return get_pagenum_link($nextpage);
2244 * Display or return the previous posts page link.
2248 * @param bool $echo Optional. Echo or return;
2249 * @return string|void The previous posts page link if `$echo = false`.
2251 function previous_posts( $echo = true ) {
2252 $output = esc_url( get_previous_posts_page_link() );
2261 * Return the previous posts page link.
2265 * @global int $paged
2267 * @param string $label Optional. Previous page link text.
2268 * @return string|void HTML-formatted previous page link.
2270 function get_previous_posts_link( $label = null ) {
2273 if ( null === $label )
2274 $label = __( '« Previous Page' );
2276 if ( !is_single() && $paged > 1 ) {
2278 * Filter the anchor tag attributes for the previous posts page link.
2282 * @param string $attributes Attributes for the anchor tag.
2284 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2285 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2290 * Display the previous posts page link.
2294 * @param string $label Optional. Previous page link text.
2296 function previous_posts_link( $label = null ) {
2297 echo get_previous_posts_link( $label );
2301 * Return post pages link navigation for previous and next pages.
2305 * @global WP_Query $wp_query
2307 * @param string|array $args Optional args.
2308 * @return string The posts link navigation.
2310 function get_posts_nav_link( $args = array() ) {
2315 if ( !is_singular() ) {
2317 'sep' => ' — ',
2318 'prelabel' => __('« Previous Page'),
2319 'nxtlabel' => __('Next Page »'),
2321 $args = wp_parse_args( $args, $defaults );
2323 $max_num_pages = $wp_query->max_num_pages;
2324 $paged = get_query_var('paged');
2326 //only have sep if there's both prev and next results
2327 if ($paged < 2 || $paged >= $max_num_pages) {
2331 if ( $max_num_pages > 1 ) {
2332 $return = get_previous_posts_link($args['prelabel']);
2333 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2334 $return .= get_next_posts_link($args['nxtlabel']);
2342 * Display post pages link navigation for previous and next pages.
2346 * @param string $sep Optional. Separator for posts navigation links.
2347 * @param string $prelabel Optional. Label for previous pages.
2348 * @param string $nxtlabel Optional Label for next pages.
2350 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2351 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2352 echo get_posts_nav_link($args);
2356 * Return navigation to next/previous post when applicable.
2359 * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments.
2361 * @param array $args {
2362 * Optional. Default post navigation arguments. Default empty array.
2364 * @type string $prev_text Anchor text to display in the previous post link. Default '%title'.
2365 * @type string $next_text Anchor text to display in the next post link. Default '%title'.
2366 * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false.
2367 * @type array|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty.
2368 * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'.
2369 * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2371 * @return string Markup for post links.
2373 function get_the_post_navigation( $args = array() ) {
2374 $args = wp_parse_args( $args, array(
2375 'prev_text' => '%title',
2376 'next_text' => '%title',
2377 'in_same_term' => false,
2378 'excluded_terms' => '',
2379 'taxonomy' => 'category',
2380 'screen_reader_text' => __( 'Post navigation' ),
2385 $previous = get_previous_post_link(
2386 '<div class="nav-previous">%link</div>',
2388 $args['in_same_term'],
2389 $args['excluded_terms'],
2393 $next = get_next_post_link(
2394 '<div class="nav-next">%link</div>',
2396 $args['in_same_term'],
2397 $args['excluded_terms'],
2401 // Only add markup if there's somewhere to navigate to.
2402 if ( $previous || $next ) {
2403 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2410 * Display navigation to next/previous post when applicable.
2414 * @param array $args Optional. See {@see get_the_post_navigation()} for available
2415 * arguments. Default empty array.
2417 function the_post_navigation( $args = array() ) {
2418 echo get_the_post_navigation( $args );
2422 * Return navigation to next/previous set of posts when applicable.
2426 * @global WP_Query $wp_query WordPress Query object.
2428 * @param array $args {
2429 * Optional. Default posts navigation arguments. Default empty array.
2431 * @type string $prev_text Anchor text to display in the previous posts link.
2432 * Default 'Older posts'.
2433 * @type string $next_text Anchor text to display in the next posts link.
2434 * Default 'Newer posts'.
2435 * @type string $screen_reader_text Screen reader text for nav element.
2436 * Default 'Posts navigation'.
2438 * @return string Markup for posts links.
2440 function get_the_posts_navigation( $args = array() ) {
2443 // Don't print empty markup if there's only one page.
2444 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2445 $args = wp_parse_args( $args, array(
2446 'prev_text' => __( 'Older posts' ),
2447 'next_text' => __( 'Newer posts' ),
2448 'screen_reader_text' => __( 'Posts navigation' ),
2451 $next_link = get_previous_posts_link( $args['next_text'] );
2452 $prev_link = get_next_posts_link( $args['prev_text'] );
2455 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2459 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2462 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2469 * Display navigation to next/previous set of posts when applicable.
2473 * @param array $args Optional. See {@see get_the_posts_navigation()} for available
2474 * arguments. Default empty array.
2476 function the_posts_navigation( $args = array() ) {
2477 echo get_the_posts_navigation( $args );
2481 * Return a paginated navigation to next/previous set of posts,
2486 * @param array $args {
2487 * Optional. Default pagination arguments, {@see paginate_links()}.
2489 * @type string $screen_reader_text Screen reader text for navigation element.
2490 * Default 'Posts navigation'.
2492 * @return string Markup for pagination links.
2494 function get_the_posts_pagination( $args = array() ) {
2497 // Don't print empty markup if there's only one page.
2498 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2499 $args = wp_parse_args( $args, array(
2501 'prev_text' => _x( 'Previous', 'previous post' ),
2502 'next_text' => _x( 'Next', 'next post' ),
2503 'screen_reader_text' => __( 'Posts navigation' ),
2506 // Make sure we get a string back. Plain is the next best thing.
2507 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2508 $args['type'] = 'plain';
2511 // Set up paginated links.
2512 $links = paginate_links( $args );
2515 $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2523 * Display a paginated navigation to next/previous set of posts,
2528 * @param array $args Optional. See {@see get_the_posts_pagination()} for available arguments.
2529 * Default empty array.
2531 function the_posts_pagination( $args = array() ) {
2532 echo get_the_posts_pagination( $args );
2536 * Wraps passed links in navigational markup.
2541 * @param string $links Navigational links.
2542 * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'.
2543 * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2544 * @return string Navigation template tag.
2546 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2547 if ( empty( $screen_reader_text ) ) {
2548 $screen_reader_text = __( 'Posts navigation' );
2552 <nav class="navigation %1$s" role="navigation">
2553 <h2 class="screen-reader-text">%2$s</h2>
2554 <div class="nav-links">%3$s</div>
2558 * Filter the navigation markup template.
2560 * Note: The filtered template HTML must contain specifiers for the navigation
2561 * class (%1$s), the screen-reader-text value (%2$s), and placement of the
2562 * navigation links (%3$s):
2564 * <nav class="navigation %1$s" role="navigation">
2565 * <h2 class="screen-reader-text">%2$s</h2>
2566 * <div class="nav-links">%3$s</div>
2571 * @param string $template The default template.
2572 * @param string $class The class passed by the calling function.
2573 * @return string Navigation template.
2575 $template = apply_filters( 'navigation_markup_template', $template, $class );
2577 return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2581 * Retrieve comments page number link.
2585 * @global WP_Rewrite $wp_rewrite
2587 * @param int $pagenum Optional. Page number.
2588 * @param int $max_page Optional. The maximum number of comment pages.
2589 * @return string The comments page number link URL.
2591 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2594 $pagenum = (int) $pagenum;
2596 $result = get_permalink();
2598 if ( 'newest' == get_option('default_comments_page') ) {
2599 if ( $pagenum != $max_page ) {
2600 if ( $wp_rewrite->using_permalinks() )
2601 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2603 $result = add_query_arg( 'cpage', $pagenum, $result );
2605 } elseif ( $pagenum > 1 ) {
2606 if ( $wp_rewrite->using_permalinks() )
2607 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2609 $result = add_query_arg( 'cpage', $pagenum, $result );
2612 $result .= '#comments';
2615 * Filter the comments page number link for the current request.
2619 * @param string $result The comments page number link.
2621 return apply_filters( 'get_comments_pagenum_link', $result );
2625 * Return the link to next comments page.
2629 * @global WP_Query $wp_query
2631 * @param string $label Optional. Label for link text.
2632 * @param int $max_page Optional. Max page.
2633 * @return string|void HTML-formatted link for the next page of comments.
2635 function get_next_comments_link( $label = '', $max_page = 0 ) {
2638 if ( ! is_singular() )
2641 $page = get_query_var('cpage');
2647 $nextpage = intval($page) + 1;
2649 if ( empty($max_page) )
2650 $max_page = $wp_query->max_num_comment_pages;
2652 if ( empty($max_page) )
2653 $max_page = get_comment_pages_count();
2655 if ( $nextpage > $max_page )
2658 if ( empty($label) )
2659 $label = __('Newer Comments »');
2662 * Filter the anchor tag attributes for the next comments page link.
2666 * @param string $attributes Attributes for the anchor tag.
2668 return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
2672 * Display the link to next comments page.
2676 * @param string $label Optional. Label for link text.
2677 * @param int $max_page Optional. Max page.
2679 function next_comments_link( $label = '', $max_page = 0 ) {
2680 echo get_next_comments_link( $label, $max_page );
2684 * Return the previous comments page link.
2688 * @param string $label Optional. Label for comments link text.
2689 * @return string|void HTML-formatted link for the previous page of comments.
2691 function get_previous_comments_link( $label = '' ) {
2692 if ( ! is_singular() )
2695 $page = get_query_var('cpage');
2697 if ( intval($page) <= 1 )
2700 $prevpage = intval($page) - 1;
2702 if ( empty($label) )
2703 $label = __('« Older Comments');
2706 * Filter the anchor tag attributes for the previous comments page link.
2710 * @param string $attributes Attributes for the anchor tag.
2712 return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
2716 * Display the previous comments page link.
2720 * @param string $label Optional. Label for comments link text.
2722 function previous_comments_link( $label = '' ) {
2723 echo get_previous_comments_link( $label );
2727 * Create pagination links for the comments on the current post.
2729 * @see paginate_links()
2732 * @global WP_Rewrite $wp_rewrite
2734 * @param string|array $args Optional args. See paginate_links().
2735 * @return string|void Markup for pagination links.
2737 function paginate_comments_links($args = array()) {
2740 if ( ! is_singular() )
2743 $page = get_query_var('cpage');
2746 $max_page = get_comment_pages_count();
2748 'base' => add_query_arg( 'cpage', '%#%' ),
2750 'total' => $max_page,
2753 'add_fragment' => '#comments'
2755 if ( $wp_rewrite->using_permalinks() )
2756 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2758 $args = wp_parse_args( $args, $defaults );
2759 $page_links = paginate_links( $args );
2761 if ( $args['echo'] )
2768 * Returns navigation to next/previous set of comments when applicable.
2772 * @param array $args {
2773 * Optional. Default comments navigation arguments.
2775 * @type string $prev_text Anchor text to display in the previous comments link. Default 'Older comments'.
2776 * @type string $next_text Anchor text to display in the next comments link. Default 'Newer comments'.
2777 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2779 * @return string Markup for comments links.
2781 function get_the_comments_navigation( $args = array() ) {
2784 // Are there comments to navigate through?
2785 if ( get_comment_pages_count() > 1 ) {
2786 $args = wp_parse_args( $args, array(
2787 'prev_text' => __( 'Older comments' ),
2788 'next_text' => __( 'Newer comments' ),
2789 'screen_reader_text' => __( 'Comments navigation' ),
2792 $prev_link = get_previous_comments_link( $args['prev_text'] );
2793 $next_link = get_next_comments_link( $args['next_text'] );
2796 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2800 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2803 $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] );
2810 * Displays navigation to next/previous set of comments when applicable.
2814 * @param array $args See {@see get_the_comments_navigation()} for available arguments.
2816 function the_comments_navigation( $args = array() ) {
2817 echo get_the_comments_navigation( $args );
2821 * Returns a paginated navigation to next/previous set of comments,
2826 * @see paginate_comments_links()
2828 * @param array $args {
2829 * Optional. Default pagination arguments.
2831 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2833 * @return string Markup for pagination links.
2835 function get_the_comments_pagination( $args = array() ) {
2837 $args = wp_parse_args( $args, array(
2838 'screen_reader_text' => __( 'Comments navigation' ),
2840 $args['echo'] = false;
2842 // Make sure we get plain links, so we get a string we can work with.
2843 $args['type'] = 'plain';
2845 $links = paginate_comments_links( $args );
2848 $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
2855 * Displays a paginated navigation to next/previous set of comments,
2860 * @param array $args See {@see get_the_comments_pagination()} for available arguments.
2862 function the_comments_pagination( $args = array() ) {
2863 echo get_the_comments_pagination( $args );
2867 * Retrieve the Press This bookmarklet link.
2869 * Use this in 'a' element 'href' attribute.
2873 * @global bool $is_IE
2874 * @global string $wp_version
2875 * @global WP_Press_This $wp_press_this
2877 * @return string The Press This bookmarklet link URL.
2879 function get_shortcut_link() {
2880 global $is_IE, $wp_version;
2882 include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2883 $bookmarklet_version = $GLOBALS['wp_press_this']->version;
2888 * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2889 * since they only support a max length of ~2000 characters for
2890 * bookmark[let] URLs, which is way to small for our smarter one.
2891 * Do update the version number so users do not get the "upgrade your
2892 * bookmarklet" notice when using PT in those browsers.
2894 $ua = $_SERVER['HTTP_USER_AGENT'];
2896 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2897 $url = wp_json_encode( admin_url( 'press-this.php' ) );
2899 $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2900 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2901 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' .
2902 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2903 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2907 if ( empty( $link ) ) {
2908 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2911 $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version );
2912 $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2916 $link = str_replace( array( "\r", "\n", "\t" ), '', $link );
2919 * Filter the Press This bookmarklet link.
2923 * @param string $link The Press This bookmarklet link.
2925 return apply_filters( 'shortcut_link', $link );
2929 * Retrieve the URL for the current site where the front end is accessible.
2931 * Returns the 'home' option with the appropriate protocol, 'https' if
2932 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2933 * `is_ssl()` is overridden.
2937 * @param string $path Optional. Path relative to the home url. Default empty.
2938 * @param string|null $scheme Optional. Scheme to give the home url context. Accepts
2939 * 'http', 'https', 'relative', 'rest', or null. Default null.
2940 * @return string Home url link with optional path appended.
2942 function home_url( $path = '', $scheme = null ) {
2943 return get_home_url( null, $path, $scheme );
2947 * Retrieve the URL for a given site where the front end is accessible.
2949 * Returns the 'home' option with the appropriate protocol, 'https' if
2950 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2956 * @global string $pagenow
2958 * @param int $blog_id Optional. Site ID. Default null (current site).
2959 * @param string $path Optional. Path relative to the home URL. Default empty.
2960 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
2961 * 'http', 'https', 'relative', 'rest', or null. Default null.
2962 * @return string Home URL link with optional path appended.
2964 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2967 $orig_scheme = $scheme;
2969 if ( empty( $blog_id ) || !is_multisite() ) {
2970 $url = get_option( 'home' );
2972 switch_to_blog( $blog_id );
2973 $url = get_option( 'home' );
2974 restore_current_blog();
2977 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2978 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow )
2981 $scheme = parse_url( $url, PHP_URL_SCHEME );
2984 $url = set_url_scheme( $url, $scheme );
2986 if ( $path && is_string( $path ) )
2987 $url .= '/' . ltrim( $path, '/' );
2990 * Filter the home URL.
2994 * @param string $url The complete home URL including scheme and path.
2995 * @param string $path Path relative to the home URL. Blank string if no path is specified.
2996 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
2997 * 'relative', 'rest', or null.
2998 * @param int|null $blog_id Site ID, or null for the current site.
3000 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
3004 * Retrieve the URL for the current site where WordPress application files
3005 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3007 * Returns the 'site_url' option with the appropriate protocol, 'https' if
3008 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3013 * @param string $path Optional. Path relative to the site url.
3014 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
3015 * @return string Site url link with optional path appended.
3017 function site_url( $path = '', $scheme = null ) {
3018 return get_site_url( null, $path, $scheme );
3022 * Retrieve the URL for a given site where WordPress application files
3023 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3025 * Returns the 'site_url' option with the appropriate protocol, 'https' if
3026 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
3027 * `is_ssl()` is overridden.
3031 * @param int $blog_id Optional. Site ID. Default null (current site).
3032 * @param string $path Optional. Path relative to the site url. Default empty.
3033 * @param string $scheme Optional. Scheme to give the site url context. Accepts
3034 * 'http', 'https', 'login', 'login_post', 'admin', or
3035 * 'relative'. Default null.
3036 * @return string Site url link with optional path appended.
3038 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
3039 if ( empty( $blog_id ) || !is_multisite() ) {
3040 $url = get_option( 'siteurl' );
3042 switch_to_blog( $blog_id );
3043 $url = get_option( 'siteurl' );
3044 restore_current_blog();
3047 $url = set_url_scheme( $url, $scheme );
3049 if ( $path && is_string( $path ) )
3050 $url .= '/' . ltrim( $path, '/' );
3053 * Filter the site URL.
3057 * @param string $url The complete site URL including scheme and path.
3058 * @param string $path Path relative to the site URL. Blank string if no path is specified.
3059 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
3060 * 'login_post', 'admin', 'relative' or null.
3061 * @param int|null $blog_id Site ID, or null for the current site.
3063 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
3067 * Retrieve the url to the admin area for the current site.
3071 * @param string $path Optional path relative to the admin url.
3072 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3073 * @return string Admin url link with optional path appended.
3075 function admin_url( $path = '', $scheme = 'admin' ) {
3076 return get_admin_url( null, $path, $scheme );
3080 * Retrieves the url to the admin area for a given site.
3084 * @param int $blog_id Optional. Site ID. Default null (current site).
3085 * @param string $path Optional. Path relative to the admin url. Default empty.
3086 * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
3087 * to force those schemes. Default 'admin', which obeys
3088 * {@see force_ssl_admin()} and {@see is_ssl()}.
3089 * @return string Admin url link with optional path appended.
3091 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
3092 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
3094 if ( $path && is_string( $path ) )
3095 $url .= ltrim( $path, '/' );
3098 * Filter the admin area URL.
3102 * @param string $url The complete admin area URL including scheme and path.
3103 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
3104 * @param int|null $blog_id Site ID, or null for the current site.
3106 return apply_filters( 'admin_url', $url, $path, $blog_id );
3110 * Retrieve the url to the includes directory.
3114 * @param string $path Optional. Path relative to the includes url.
3115 * @param string $scheme Optional. Scheme to give the includes url context.
3116 * @return string Includes url link with optional path appended.
3118 function includes_url( $path = '', $scheme = null ) {
3119 $url = site_url( '/' . WPINC . '/', $scheme );
3121 if ( $path && is_string( $path ) )
3122 $url .= ltrim($path, '/');
3125 * Filter the URL to the includes directory.
3129 * @param string $url The complete URL to the includes directory including scheme and path.
3130 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
3131 * if no path is specified.
3133 return apply_filters( 'includes_url', $url, $path );
3137 * Retrieve the url to the content directory.
3141 * @param string $path Optional. Path relative to the content url.
3142 * @return string Content url link with optional path appended.
3144 function content_url($path = '') {
3145 $url = set_url_scheme( WP_CONTENT_URL );
3147 if ( $path && is_string( $path ) )
3148 $url .= '/' . ltrim($path, '/');
3151 * Filter the URL to the content directory.
3155 * @param string $url The complete URL to the content directory including scheme and path.
3156 * @param string $path Path relative to the URL to the content directory. Blank string
3157 * if no path is specified.
3159 return apply_filters( 'content_url', $url, $path);
3163 * Retrieve a URL within the plugins or mu-plugins directory.
3165 * Defaults to the plugins directory URL if no arguments are supplied.
3169 * @param string $path Optional. Extra path appended to the end of the URL, including
3170 * the relative directory if $plugin is supplied. Default empty.
3171 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
3172 * The URL will be relative to its directory. Default empty.
3173 * Typically this is done by passing `__FILE__` as the argument.
3174 * @return string Plugins URL link with optional paths appended.
3176 function plugins_url( $path = '', $plugin = '' ) {
3178 $path = wp_normalize_path( $path );
3179 $plugin = wp_normalize_path( $plugin );
3180 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
3182 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
3183 $url = WPMU_PLUGIN_URL;
3185 $url = WP_PLUGIN_URL;
3188 $url = set_url_scheme( $url );
3190 if ( !empty($plugin) && is_string($plugin) ) {
3191 $folder = dirname(plugin_basename($plugin));
3192 if ( '.' != $folder )
3193 $url .= '/' . ltrim($folder, '/');
3196 if ( $path && is_string( $path ) )
3197 $url .= '/' . ltrim($path, '/');
3200 * Filter the URL to the plugins directory.
3204 * @param string $url The complete URL to the plugins directory including scheme and path.
3205 * @param string $path Path relative to the URL to the plugins directory. Blank string
3206 * if no path is specified.
3207 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
3210 return apply_filters( 'plugins_url', $url, $path, $plugin );
3214 * Retrieve the site url for the current network.
3216 * Returns the site url with the appropriate protocol, 'https' if
3217 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3222 * @param string $path Optional. Path relative to the site url.
3223 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
3224 * @return string Site url link with optional path appended.
3226 function network_site_url( $path = '', $scheme = null ) {
3227 if ( ! is_multisite() )
3228 return site_url($path, $scheme);
3230 $current_site = get_current_site();
3232 if ( 'relative' == $scheme )
3233 $url = $current_site->path;
3235 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3237 if ( $path && is_string( $path ) )
3238 $url .= ltrim( $path, '/' );
3241 * Filter the network site URL.
3245 * @param string $url The complete network site URL including scheme and path.
3246 * @param string $path Path relative to the network site URL. Blank string if
3247 * no path is specified.
3248 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3249 * 'relative' or null.
3251 return apply_filters( 'network_site_url', $url, $path, $scheme );
3255 * Retrieves the home url for the current network.
3257 * Returns the home url with the appropriate protocol, 'https' {@see is_ssl()}
3258 * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3263 * @param string $path Optional. Path relative to the home url. Default empty.
3264 * @param string $scheme Optional. Scheme to give the home url context. Accepts
3265 * 'http', 'https', or 'relative'. Default null.
3266 * @return string Home url link with optional path appended.
3268 function network_home_url( $path = '', $scheme = null ) {
3269 if ( ! is_multisite() )
3270 return home_url($path, $scheme);
3272 $current_site = get_current_site();
3273 $orig_scheme = $scheme;
3275 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
3276 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3278 if ( 'relative' == $scheme )
3279 $url = $current_site->path;
3281 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3283 if ( $path && is_string( $path ) )
3284 $url .= ltrim( $path, '/' );
3287 * Filter the network home URL.
3291 * @param string $url The complete network home URL including scheme and path.
3292 * @param string $path Path relative to the network home URL. Blank string
3293 * if no path is specified.
3294 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3295 * 'relative' or null.
3297 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3301 * Retrieve the url to the admin area for the network.
3305 * @param string $path Optional path relative to the admin url.
3306 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3307 * @return string Admin url link with optional path appended.
3309 function network_admin_url( $path = '', $scheme = 'admin' ) {
3310 if ( ! is_multisite() )
3311 return admin_url( $path, $scheme );
3313 $url = network_site_url('wp-admin/network/', $scheme);
3315 if ( $path && is_string( $path ) )
3316 $url .= ltrim($path, '/');
3319 * Filter the network admin URL.
3323 * @param string $url The complete network admin URL including scheme and path.
3324 * @param string $path Path relative to the network admin URL. Blank string if
3325 * no path is specified.
3327 return apply_filters( 'network_admin_url', $url, $path );
3331 * Retrieve the url to the admin area for the current user.
3335 * @param string $path Optional path relative to the admin url.
3336 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3337 * @return string Admin url link with optional path appended.
3339 function user_admin_url( $path = '', $scheme = 'admin' ) {
3340 $url = network_site_url('wp-admin/user/', $scheme);
3342 if ( $path && is_string( $path ) )
3343 $url .= ltrim($path, '/');
3346 * Filter the user admin URL for the current user.
3350 * @param string $url The complete URL including scheme and path.
3351 * @param string $path Path relative to the URL. Blank string if
3352 * no path is specified.
3354 return apply_filters( 'user_admin_url', $url, $path );
3358 * Retrieve the url to the admin area for either the current site or the network depending on context.
3362 * @param string $path Optional path relative to the admin url.
3363 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3364 * @return string Admin url link with optional path appended.
3366 function self_admin_url($path = '', $scheme = 'admin') {
3367 if ( is_network_admin() )
3368 return network_admin_url($path, $scheme);
3369 elseif ( is_user_admin() )
3370 return user_admin_url($path, $scheme);
3372 return admin_url($path, $scheme);
3376 * Sets the scheme for a URL.
3379 * @since 4.4.0 The 'rest' scheme was added.
3381 * @param string $url Absolute url that includes a scheme
3382 * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
3383 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
3384 * @return string $url URL with chosen scheme.
3386 function set_url_scheme( $url, $scheme = null ) {
3387 $orig_scheme = $scheme;
3390 $scheme = is_ssl() ? 'https' : 'http';
3391 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3392 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3393 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3394 $scheme = is_ssl() ? 'https' : 'http';
3397 $url = trim( $url );
3398 if ( substr( $url, 0, 2 ) === '//' )
3399 $url = 'http:' . $url;
3401 if ( 'relative' == $scheme ) {
3402 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3403 if ( $url !== '' && $url[0] === '/' )
3404 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3406 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3410 * Filter the resulting URL after setting the scheme.
3414 * @param string $url The complete URL including scheme and path.
3415 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3416 * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3417 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
3419 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3423 * Get the URL to the user's dashboard.
3425 * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
3426 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
3427 * primary site is returned.
3431 * @param int $user_id Optional. User ID. Defaults to current user.
3432 * @param string $path Optional path relative to the dashboard. Use only paths known to both site and user admins.
3433 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3434 * @return string Dashboard url link with optional path appended.
3436 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3437 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3439 $blogs = get_blogs_of_user( $user_id );
3440 if ( ! is_super_admin() && empty($blogs) ) {
3441 $url = user_admin_url( $path, $scheme );
3442 } elseif ( ! is_multisite() ) {
3443 $url = admin_url( $path, $scheme );
3445 $current_blog = get_current_blog_id();
3446 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3447 $url = admin_url( $path, $scheme );
3449 $active = get_active_blog_for_user( $user_id );
3451 $url = get_admin_url( $active->blog_id, $path, $scheme );
3453 $url = user_admin_url( $path, $scheme );
3458 * Filter the dashboard URL for a user.
3462 * @param string $url The complete URL including scheme and path.
3463 * @param int $user_id The user ID.
3464 * @param string $path Path relative to the URL. Blank string if no path is specified.
3465 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3466 * 'login_post', 'admin', 'relative' or null.
3468 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3472 * Get the URL to the user's profile editor.
3476 * @param int $user_id Optional. User ID. Defaults to current user.
3477 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3478 * 'http' or 'https' can be passed to force those schemes.
3479 * @return string Dashboard url link with optional path appended.
3481 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3482 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3484 if ( is_user_admin() )
3485 $url = user_admin_url( 'profile.php', $scheme );
3486 elseif ( is_network_admin() )
3487 $url = network_admin_url( 'profile.php', $scheme );
3489 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3492 * Filter the URL for a user's profile editor.
3496 * @param string $url The complete URL including scheme and path.
3497 * @param int $user_id The user ID.
3498 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3499 * 'login_post', 'admin', 'relative' or null.
3501 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3505 * Output rel=canonical for singular queries.
3509 function rel_canonical() {
3510 if ( ! is_singular() ) {
3514 if ( ! $id = get_queried_object_id() ) {
3518 $url = get_permalink( $id );
3520 $page = get_query_var( 'page' );
3522 if ( '' == get_option( 'permalink_structure' ) ) {
3523 $url = add_query_arg( 'page', $page, $url );
3525 $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' );
3529 $cpage = get_query_var( 'cpage' );
3531 $url = get_comments_pagenum_link( $cpage );
3533 echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n";
3537 * Return a shortlink for a post, page, attachment, or a site.
3539 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
3540 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
3541 * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
3542 * via the get_shortlink filter.
3546 * @param int $id A post or site id. Default is 0, which means the current post or site.
3547 * @param string $context Whether the id is a 'site' id, 'post' id, or 'media' id.
3548 * If 'post', the post_type of the post is consulted.
3549 * If 'query', the current query is consulted to determine the id and context.
3550 * Default is 'post'.
3551 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3552 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3554 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3556 * Filter whether to preempt generating a shortlink for the given post.
3558 * Passing a truthy value to the filter will effectively short-circuit the
3559 * shortlink-generation process, returning that value instead.
3563 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3564 * @param int $id Post ID, or 0 for the current post.
3565 * @param string $context The context for the link. One of 'post' or 'query',
3566 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3568 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3570 if ( false !== $shortlink ) {
3575 if ( 'query' == $context && is_singular() ) {
3576 $post_id = get_queried_object_id();
3577 $post = get_post( $post_id );
3578 } elseif ( 'post' == $context ) {
3579 $post = get_post( $id );
3580 if ( ! empty( $post->ID ) )
3581 $post_id = $post->ID;
3586 // Return p= link for all public post types.
3587 if ( ! empty( $post_id ) ) {
3588 $post_type = get_post_type_object( $post->post_type );
3590 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3591 $shortlink = home_url( '/' );
3592 } elseif ( $post_type->public ) {
3593 $shortlink = home_url( '?p=' . $post_id );
3598 * Filter the shortlink for a post.
3602 * @param string $shortlink Shortlink URL.
3603 * @param int $id Post ID, or 0 for the current post.
3604 * @param string $context The context for the link. One of 'post' or 'query',
3605 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3607 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3611 * Inject rel=shortlink into head if a shortlink is defined for the current page.
3613 * Attached to the wp_head action.
3617 function wp_shortlink_wp_head() {
3618 $shortlink = wp_get_shortlink( 0, 'query' );
3620 if ( empty( $shortlink ) )
3623 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3627 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3629 * Attached to the wp action.
3633 function wp_shortlink_header() {
3634 if ( headers_sent() )
3637 $shortlink = wp_get_shortlink(0, 'query');
3639 if ( empty($shortlink) )
3642 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3646 * Display the Short Link for a Post
3648 * Must be called from inside "The Loop"
3650 * Call like the_shortlink(__('Shortlinkage FTW'))
3654 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3655 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3656 * @param string $before Optional HTML to display before the link.
3657 * @param string $after Optional HTML to display after the link.
3659 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3662 if ( empty( $text ) )
3663 $text = __('This is the short link.');
3665 if ( empty( $title ) )
3666 $title = the_title_attribute( array( 'echo' => false ) );
3668 $shortlink = wp_get_shortlink( $post->ID );
3670 if ( !empty( $shortlink ) ) {
3671 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3674 * Filter the shortlink anchor tag for a post.
3678 * @param string $link Shortlink anchor tag.
3679 * @param string $shortlink Shortlink URL.
3680 * @param string $text Shortlink's text.
3681 * @param string $title Shortlink's title attribute.
3683 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3684 echo $before, $link, $after;
3690 * Retrieve the avatar URL.
3694 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3695 * user email, WP_User object, WP_Post object, or WP_Comment object.
3696 * @param array $args {
3697 * Optional. Arguments to return instead of the default arguments.
3699 * @type int $size Height and width of the avatar in pixels. Default 96.
3700 * @type string $default URL for the default image or a default type. Accepts '404' (return
3701 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3702 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3703 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3704 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3705 * 'avatar_default' option, with a fallback of 'mystery'.
3706 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3707 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3708 * judged in that order. Default is the value of the 'avatar_rating' option.
3709 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3711 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3712 * plus a "found_avatar" guess. Pass as a reference. Default null.
3714 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3716 function get_avatar_url( $id_or_email, $args = null ) {
3717 $args = get_avatar_data( $id_or_email, $args );
3718 return $args['url'];
3722 * Retrieve default data about the avatar.
3726 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3727 * user email, WP_User object, WP_Post object, or WP_Comment object.
3728 * @param array $args {
3729 * Optional. Arguments to return instead of the default arguments.
3731 * @type int $size Height and width of the avatar image file in pixels. Default 96.
3732 * @type int $height Display height of the avatar in pixels. Defaults to $size.
3733 * @type int $width Display width of the avatar in pixels. Defaults to $size.
3734 * @type string $default URL for the default image or a default type. Accepts '404' (return
3735 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3736 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3737 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3738 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3739 * 'avatar_default' option, with a fallback of 'mystery'.
3740 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3741 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3742 * judged in that order. Default is the value of the 'avatar_rating' option.
3743 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3745 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3746 * plus a "found_avatar" guess. Pass as a reference. Default null.
3747 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3749 * @return array $processed_args {
3750 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3752 * @type bool $found_avatar True if we were able to find an avatar for this user,
3753 * false or not set if we couldn't.
3754 * @type string $url The URL of the avatar we found.
3757 function get_avatar_data( $id_or_email, $args = null ) {
3758 $args = wp_parse_args( $args, array(
3762 'default' => get_option( 'avatar_default', 'mystery' ),
3763 'force_default' => false,
3764 'rating' => get_option( 'avatar_rating' ),
3766 'processed_args' => null, // if used, should be a reference
3770 if ( is_numeric( $args['size'] ) ) {
3771 $args['size'] = absint( $args['size'] );
3772 if ( ! $args['size'] ) {
3779 if ( is_numeric( $args['height'] ) ) {
3780 $args['height'] = absint( $args['height'] );
3781 if ( ! $args['height'] ) {
3782 $args['height'] = $args['size'];
3785 $args['height'] = $args['size'];
3788 if ( is_numeric( $args['width'] ) ) {
3789 $args['width'] = absint( $args['width'] );
3790 if ( ! $args['width'] ) {
3791 $args['width'] = $args['size'];
3794 $args['width'] = $args['size'];
3797 if ( empty( $args['default'] ) ) {
3798 $args['default'] = get_option( 'avatar_default', 'mystery' );
3801 switch ( $args['default'] ) {
3805 $args['default'] = 'mm';
3807 case 'gravatar_default' :
3808 $args['default'] = false;
3812 $args['force_default'] = (bool) $args['force_default'];
3814 $args['rating'] = strtolower( $args['rating'] );
3816 $args['found_avatar'] = false;
3819 * Filter whether to retrieve the avatar URL early.
3821 * Passing a non-null value in the 'url' member of the return array will
3822 * effectively short circuit get_avatar_data(), passing the value through
3823 * the {@see 'get_avatar_data'} filter and returning early.
3827 * @param array $args Arguments passed to get_avatar_data(), after processing.
3828 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3829 * user email, WP_User object, WP_Post object, or WP_Comment object.
3831 $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3833 if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3834 /** This filter is documented in wp-includes/link-template.php */
3835 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3839 $user = $email = false;
3841 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3842 $id_or_email = get_comment( $id_or_email );
3845 // Process the user identifier.
3846 if ( is_numeric( $id_or_email ) ) {
3847 $user = get_user_by( 'id', absint( $id_or_email ) );
3848 } elseif ( is_string( $id_or_email ) ) {
3849 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3851 list( $email_hash ) = explode( '@', $id_or_email );
3854 $email = $id_or_email;
3856 } elseif ( $id_or_email instanceof WP_User ) {
3858 $user = $id_or_email;
3859 } elseif ( $id_or_email instanceof WP_Post ) {
3861 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3862 } elseif ( $id_or_email instanceof WP_Comment ) {
3864 * Filter the list of allowed comment types for retrieving avatars.
3868 * @param array $types An array of content types. Default only contains 'comment'.
3870 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3871 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3872 $args['url'] = false;
3873 /** This filter is documented in wp-includes/link-template.php */
3874 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3877 if ( ! empty( $id_or_email->user_id ) ) {
3878 $user = get_user_by( 'id', (int) $id_or_email->user_id );
3880 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3881 $email = $id_or_email->comment_author_email;
3885 if ( ! $email_hash ) {
3887 $email = $user->user_email;
3891 $email_hash = md5( strtolower( trim( $email ) ) );
3895 if ( $email_hash ) {
3896 $args['found_avatar'] = true;
3897 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3899 $gravatar_server = rand( 0, 2 );
3903 's' => $args['size'],
3904 'd' => $args['default'],
3905 'f' => $args['force_default'] ? 'y' : false,
3906 'r' => $args['rating'],
3910 $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
3912 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
3915 $url = add_query_arg(
3916 rawurlencode_deep( array_filter( $url_args ) ),
3917 set_url_scheme( $url, $args['scheme'] )
3921 * Filter the avatar URL.
3925 * @param string $url The URL of the avatar.
3926 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3927 * user email, WP_User object, WP_Post object, or WP_Comment object.
3928 * @param array $args Arguments passed to get_avatar_data(), after processing.
3930 $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
3933 * Filter the avatar data.
3937 * @param array $args Arguments passed to get_avatar_data(), after processing.
3938 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3939 * user email, WP_User object, WP_Post object, or WP_Comment object.
3941 return apply_filters( 'get_avatar_data', $args, $id_or_email );