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 blog 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 * blog 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 * Retrieve edit term url.
911 * @param int $term_id Term ID.
912 * @param string $taxonomy Taxonomy.
913 * @param string $object_type The object type. Used to highlight the proper post type menu on the linked page.
914 * Defaults to the first object_type associated with the taxonomy.
915 * @return string|null The edit term link URL for the given term, or null on failure.
917 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
918 $tax = get_taxonomy( $taxonomy );
919 if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
923 $term = get_term( $term_id, $taxonomy );
924 if ( ! $term || is_wp_error( $term ) ) {
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( 'edit-tags.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 * Retrieve the permalink for a post type archive.
1117 * @global WP_Rewrite $wp_rewrite
1119 * @param string $post_type Post type
1120 * @return string|false The post type archive permalink.
1122 function get_post_type_archive_link( $post_type ) {
1124 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1127 if ( ! $post_type_obj->has_archive )
1130 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1131 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1132 if ( $post_type_obj->rewrite['with_front'] )
1133 $struct = $wp_rewrite->front . $struct;
1135 $struct = $wp_rewrite->root . $struct;
1136 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1138 $link = home_url( '?post_type=' . $post_type );
1142 * Filter the post type archive permalink.
1146 * @param string $link The post type archive permalink.
1147 * @param string $post_type Post type name.
1149 return apply_filters( 'post_type_archive_link', $link, $post_type );
1153 * Retrieve the permalink for a post type archive feed.
1157 * @param string $post_type Post type
1158 * @param string $feed Optional. Feed type
1159 * @return string|false The post type feed permalink.
1161 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1162 $default_feed = get_default_feed();
1163 if ( empty( $feed ) )
1164 $feed = $default_feed;
1166 if ( ! $link = get_post_type_archive_link( $post_type ) )
1169 $post_type_obj = get_post_type_object( $post_type );
1170 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1171 $link = trailingslashit( $link );
1173 if ( $feed != $default_feed )
1176 $link = add_query_arg( 'feed', $feed, $link );
1180 * Filter the post type archive feed link.
1184 * @param string $link The post type archive feed link.
1185 * @param string $feed Feed type.
1187 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1191 * Retrieve URL used for the post preview.
1193 * Get the preview post URL. Allows additional query args to be appended.
1197 * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global post.
1198 * @param array $query_args Optional. Array of additional query args to be appended to the link.
1199 * @param string $preview_link Optional. Base preview link to be used if it should differ from the post permalink.
1200 * @return string URL used for the post preview.
1202 function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
1203 $post = get_post( $post );
1208 $post_type_object = get_post_type_object( $post->post_type );
1209 if ( is_post_type_viewable( $post_type_object ) ) {
1210 if ( ! $preview_link ) {
1211 $preview_link = get_permalink( $post );
1214 $query_args['preview'] = 'true';
1215 $preview_link = add_query_arg( $query_args, $preview_link );
1219 * Filter the URL used for a post preview.
1222 * @since 4.0.0 Added the `$post` parameter.
1224 * @param string $preview_link URL used for the post preview.
1225 * @param WP_Post $post Post object.
1227 return apply_filters( 'preview_post_link', $preview_link, $post );
1231 * Retrieve edit posts link for post.
1233 * Can be used within the WordPress loop or outside of it. Can be used with
1234 * pages, posts, attachments, and revisions.
1238 * @param int $id Optional. Post ID.
1239 * @param string $context Optional, defaults to display. How to write the '&', defaults to '&'.
1240 * @return string|null The edit post link for the given post. null if the post type is invalid or does
1241 * not allow an editing UI.
1243 function get_edit_post_link( $id = 0, $context = 'display' ) {
1244 if ( ! $post = get_post( $id ) )
1247 if ( 'revision' === $post->post_type )
1249 elseif ( 'display' == $context )
1250 $action = '&action=edit';
1252 $action = '&action=edit';
1254 $post_type_object = get_post_type_object( $post->post_type );
1255 if ( !$post_type_object )
1258 if ( !current_user_can( 'edit_post', $post->ID ) )
1261 if ( $post_type_object->_edit_link ) {
1262 $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
1268 * Filter the post edit link.
1272 * @param string $link The edit link.
1273 * @param int $post_id Post ID.
1274 * @param string $context The link context. If set to 'display' then ampersands
1277 return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
1281 * Display edit post link for post.
1284 * @since 4.4.0 The `$class` argument was added.
1286 * @param string $text Optional. Anchor text.
1287 * @param string $before Optional. Display before edit link.
1288 * @param string $after Optional. Display after edit link.
1289 * @param int $id Optional. Post ID.
1290 * @param string $class Optional. Add custom class to link.
1292 function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
1293 if ( ! $post = get_post( $id ) ) {
1297 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1301 if ( null === $text ) {
1302 $text = __( 'Edit This' );
1305 $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
1308 * Filter the post edit link anchor tag.
1312 * @param string $link Anchor tag for the edit link.
1313 * @param int $post_id Post ID.
1314 * @param string $text Anchor text.
1316 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1320 * Retrieve delete posts link for post.
1322 * Can be used within the WordPress loop or outside of it, with any post type.
1326 * @param int $id Optional. Post ID.
1327 * @param string $deprecated Not used.
1328 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1329 * @return string|void The delete post link URL for the given post.
1331 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1332 if ( ! empty( $deprecated ) )
1333 _deprecated_argument( __FUNCTION__, '3.0' );
1335 if ( !$post = get_post( $id ) )
1338 $post_type_object = get_post_type_object( $post->post_type );
1339 if ( !$post_type_object )
1342 if ( !current_user_can( 'delete_post', $post->ID ) )
1345 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1347 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1350 * Filter the post delete link.
1354 * @param string $link The delete link.
1355 * @param int $post_id Post ID.
1356 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1358 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1362 * Retrieve edit comment link.
1366 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
1367 * @return string|void The edit comment link URL for the given comment.
1369 function get_edit_comment_link( $comment_id = 0 ) {
1370 $comment = get_comment( $comment_id );
1372 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1375 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1378 * Filter the comment edit link.
1382 * @param string $location The edit link.
1384 return apply_filters( 'get_edit_comment_link', $location );
1388 * Display edit comment link with formatting.
1392 * @param string $text Optional. Anchor text.
1393 * @param string $before Optional. Display before edit link.
1394 * @param string $after Optional. Display after edit link.
1396 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1397 $comment = get_comment();
1399 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1403 if ( null === $text ) {
1404 $text = __( 'Edit This' );
1407 $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
1410 * Filter the comment edit link anchor tag.
1414 * @param string $link Anchor tag for the edit link.
1415 * @param int $comment_id Comment ID.
1416 * @param string $text Anchor text.
1418 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1422 * Display edit bookmark (literally a URL external to blog) link.
1426 * @param int|stdClass $link Optional. Bookmark ID.
1427 * @return string|void The edit bookmark link URL.
1429 function get_edit_bookmark_link( $link = 0 ) {
1430 $link = get_bookmark( $link );
1432 if ( !current_user_can('manage_links') )
1435 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1438 * Filter the bookmark (link) edit link.
1442 * @param string $location The edit link.
1443 * @param int $link_id Bookmark ID.
1445 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1449 * Display edit bookmark (literally a URL external to blog) link anchor content.
1453 * @param string $link Optional. Anchor text.
1454 * @param string $before Optional. Display before edit link.
1455 * @param string $after Optional. Display after edit link.
1456 * @param int $bookmark Optional. Bookmark ID.
1458 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1459 $bookmark = get_bookmark($bookmark);
1461 if ( !current_user_can('manage_links') )
1465 $link = __('Edit This');
1467 $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>';
1470 * Filter the bookmark edit link anchor tag.
1474 * @param string $link Anchor tag for the edit link.
1475 * @param int $link_id Bookmark ID.
1477 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1481 * Retrieve edit user link
1485 * @param int $user_id Optional. User ID. Defaults to the current user.
1486 * @return string URL to edit user page or empty string.
1488 function get_edit_user_link( $user_id = null ) {
1490 $user_id = get_current_user_id();
1492 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1495 $user = get_userdata( $user_id );
1500 if ( get_current_user_id() == $user->ID )
1501 $link = get_edit_profile_url( $user->ID );
1503 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1506 * Filter the user edit link.
1510 * @param string $link The edit link.
1511 * @param int $user_id User ID.
1513 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1519 * Retrieve previous post that is adjacent to current post.
1523 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1524 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1525 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1526 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1528 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1529 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1533 * Retrieve next 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_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1543 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1547 * Retrieve adjacent post.
1549 * Can either be next or previous post.
1553 * @global wpdb $wpdb WordPress database abstraction object.
1555 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1556 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1557 * @param bool $previous Optional. Whether to retrieve previous post.
1558 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1559 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1561 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1564 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1567 $current_post_date = $post->post_date;
1572 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1573 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1574 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1575 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1576 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1577 $excluded_terms = explode( ' and ', $excluded_terms );
1579 $excluded_terms = explode( ',', $excluded_terms );
1582 $excluded_terms = array_map( 'intval', $excluded_terms );
1585 if ( $in_same_term ) {
1586 $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";
1587 $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1589 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1591 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1593 // Remove any exclusions from the term array to include.
1594 $term_array = array_diff( $term_array, (array) $excluded_terms );
1595 $term_array = array_map( 'intval', $term_array );
1597 if ( ! $term_array || is_wp_error( $term_array ) )
1600 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1603 if ( ! empty( $excluded_terms ) ) {
1604 $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( $excluded_terms, ',' ) . ') )';
1608 // 'post_status' clause depends on the current user.
1609 if ( is_user_logged_in() ) {
1610 $user_id = get_current_user_id();
1612 $post_type_object = get_post_type_object( $post->post_type );
1613 if ( empty( $post_type_object ) ) {
1614 $post_type_cap = $post->post_type;
1615 $read_private_cap = 'read_private_' . $post_type_cap . 's';
1617 $read_private_cap = $post_type_object->cap->read_private_posts;
1621 * Results should include private posts belonging to the current user, or private posts where the
1622 * current user has the 'read_private_posts' cap.
1624 $private_states = get_post_stati( array( 'private' => true ) );
1625 $where .= " AND ( p.post_status = 'publish'";
1626 foreach ( (array) $private_states as $state ) {
1627 if ( current_user_can( $read_private_cap ) ) {
1628 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1630 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1635 $where .= " AND p.post_status = 'publish'";
1638 $adjacent = $previous ? 'previous' : 'next';
1639 $op = $previous ? '<' : '>';
1640 $order = $previous ? 'DESC' : 'ASC';
1643 * Filter the excluded term ids
1645 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1646 * of adjacency, 'next' or 'previous'.
1650 * @param string $excluded_terms Array of excluded term IDs.
1652 $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
1655 * Filter the JOIN clause in the SQL for an adjacent post query.
1657 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1658 * of adjacency, 'next' or 'previous'.
1661 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1663 * @param string $join The JOIN clause in the SQL.
1664 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1665 * @param array $excluded_terms Array of excluded term IDs.
1666 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1667 * @param WP_Post $post WP_Post object.
1669 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
1672 * Filter the WHERE clause in the SQL for an adjacent post query.
1674 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1675 * of adjacency, 'next' or 'previous'.
1678 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1680 * @param string $where The `WHERE` clause in the SQL.
1681 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1682 * @param array $excluded_terms Array of excluded term IDs.
1683 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1684 * @param WP_Post $post WP_Post object.
1686 $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 );
1689 * Filter the ORDER BY clause in the SQL for an adjacent post query.
1691 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1692 * of adjacency, 'next' or 'previous'.
1695 * @since 4.4.0 Added the `$post` parameter.
1697 * @param string $order_by The `ORDER BY` clause in the SQL.
1698 * @param WP_Post $post WP_Post object.
1700 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post );
1702 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1703 $query_key = 'adjacent_post_' . md5( $query );
1704 $result = wp_cache_get( $query_key, 'counts' );
1705 if ( false !== $result ) {
1707 $result = get_post( $result );
1711 $result = $wpdb->get_var( $query );
1712 if ( null === $result )
1715 wp_cache_set( $query_key, $result, 'counts' );
1718 $result = get_post( $result );
1724 * Get adjacent post relational link.
1726 * Can either be next or previous post relational link.
1730 * @param string $title Optional. Link title format.
1731 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1732 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1733 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1734 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1735 * @return string|void The adjacent post relational link URL.
1737 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1738 if ( $previous && is_attachment() && $post = get_post() )
1739 $post = get_post( $post->post_parent );
1741 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1743 if ( empty( $post ) )
1746 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1748 if ( empty( $post_title ) )
1749 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1751 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1753 $title = str_replace( '%title', $post_title, $title );
1754 $title = str_replace( '%date', $date, $title );
1756 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1757 $link .= esc_attr( $title );
1758 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1760 $adjacent = $previous ? 'previous' : 'next';
1763 * Filter the adjacent post relational link.
1765 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1766 * of adjacency, 'next' or 'previous'.
1770 * @param string $link The relational link.
1772 return apply_filters( "{$adjacent}_post_rel_link", $link );
1776 * Display relational links for the posts adjacent to the current post.
1780 * @param string $title Optional. Link title format.
1781 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1782 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1783 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1785 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1786 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1787 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1791 * Display relational links for the posts adjacent to the current post for single post pages.
1793 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1797 function adjacent_posts_rel_link_wp_head() {
1798 if ( ! is_single() || is_attachment() ) {
1801 adjacent_posts_rel_link();
1805 * Display relational link for the next post adjacent to the current post.
1809 * @param string $title Optional. Link title format.
1810 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1811 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1812 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1814 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1815 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1819 * Display relational link for the previous 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. Default true.
1826 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1828 function prev_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, true, $taxonomy );
1833 * Retrieve boundary post.
1835 * Boundary being either the first or last post by publish date within the constraints specified
1836 * by $in_same_term or $excluded_terms.
1840 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1841 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1842 * @param bool $start Optional. Whether to retrieve first or last post.
1843 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1844 * @return null|array Array containing the boundary post object if successful, null otherwise.
1846 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1848 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1851 $query_args = array(
1852 'posts_per_page' => 1,
1853 'order' => $start ? 'ASC' : 'DESC',
1854 'update_post_term_cache' => false,
1855 'update_post_meta_cache' => false
1858 $term_array = array();
1860 if ( ! is_array( $excluded_terms ) ) {
1861 if ( ! empty( $excluded_terms ) )
1862 $excluded_terms = explode( ',', $excluded_terms );
1864 $excluded_terms = array();
1867 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1868 if ( $in_same_term )
1869 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1871 if ( ! empty( $excluded_terms ) ) {
1872 $excluded_terms = array_map( 'intval', $excluded_terms );
1873 $excluded_terms = array_diff( $excluded_terms, $term_array );
1875 $inverse_terms = array();
1876 foreach ( $excluded_terms as $excluded_term )
1877 $inverse_terms[] = $excluded_term * -1;
1878 $excluded_terms = $inverse_terms;
1881 $query_args[ 'tax_query' ] = array( array(
1882 'taxonomy' => $taxonomy,
1883 'terms' => array_merge( $term_array, $excluded_terms )
1887 return get_posts( $query_args );
1891 * Get previous post link that is adjacent to the current post.
1895 * @param string $format Optional. Link anchor format.
1896 * @param string $link Optional. Link permalink format.
1897 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1898 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1899 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1900 * @return string The link URL of the previous post in relation to the current post.
1902 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1903 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1907 * Display previous post link that is adjacent to the current post.
1910 * @see get_previous_post_link()
1912 * @param string $format Optional. Link anchor format.
1913 * @param string $link Optional. Link permalink format.
1914 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1915 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1916 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1918 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1919 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1923 * Get next post link that is adjacent to the current post.
1927 * @param string $format Optional. Link anchor format.
1928 * @param string $link Optional. Link permalink format.
1929 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1930 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1931 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1932 * @return string The link URL of the next post in relation to the current post.
1934 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1935 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1939 * Display next post link that is adjacent to the current post.
1942 * @see get_next_post_link()
1944 * @param string $format Optional. Link anchor format.
1945 * @param string $link Optional. Link permalink format.
1946 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1947 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1948 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1950 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1951 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1955 * Get adjacent post link.
1957 * Can be either next post link or previous.
1961 * @param string $format Link anchor format.
1962 * @param string $link Link permalink format.
1963 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1964 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1965 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1966 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1967 * @return string The link URL of the previous or next post in relation to the current post.
1969 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1970 if ( $previous && is_attachment() )
1971 $post = get_post( get_post()->post_parent );
1973 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1978 $title = $post->post_title;
1980 if ( empty( $post->post_title ) )
1981 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1983 /** This filter is documented in wp-includes/post-template.php */
1984 $title = apply_filters( 'the_title', $title, $post->ID );
1986 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1987 $rel = $previous ? 'prev' : 'next';
1989 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1990 $inlink = str_replace( '%title', $title, $link );
1991 $inlink = str_replace( '%date', $date, $inlink );
1992 $inlink = $string . $inlink . '</a>';
1994 $output = str_replace( '%link', $inlink, $format );
1997 $adjacent = $previous ? 'previous' : 'next';
2000 * Filter the adjacent post link.
2002 * The dynamic portion of the hook name, `$adjacent`, refers to the type
2003 * of adjacency, 'next' or 'previous'.
2006 * @since 4.2.0 Added the `$adjacent` parameter.
2008 * @param string $output The adjacent post link.
2009 * @param string $format Link anchor format.
2010 * @param string $link Link permalink format.
2011 * @param WP_Post $post The adjacent post.
2012 * @param string $adjacent Whether the post is previous or next.
2014 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
2018 * Display adjacent post link.
2020 * Can be either next post link or previous.
2024 * @param string $format Link anchor format.
2025 * @param string $link Link permalink format.
2026 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
2027 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
2028 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
2029 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
2031 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
2032 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
2036 * Retrieve links for page numbers.
2040 * @global WP_Rewrite $wp_rewrite
2042 * @param int $pagenum Optional. Page ID.
2043 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
2044 * Otherwise, prepares the URL with esc_url_raw().
2045 * @return string The link URL for the given page number.
2047 function get_pagenum_link($pagenum = 1, $escape = true ) {
2050 $pagenum = (int) $pagenum;
2052 $request = remove_query_arg( 'paged' );
2054 $home_root = parse_url(home_url());
2055 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
2056 $home_root = preg_quote( $home_root, '|' );
2058 $request = preg_replace('|^'. $home_root . '|i', '', $request);
2059 $request = preg_replace('|^/+|', '', $request);
2061 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
2062 $base = trailingslashit( get_bloginfo( 'url' ) );
2064 if ( $pagenum > 1 ) {
2065 $result = add_query_arg( 'paged', $pagenum, $base . $request );
2067 $result = $base . $request;
2070 $qs_regex = '|\?.*?$|';
2071 preg_match( $qs_regex, $request, $qs_match );
2073 if ( !empty( $qs_match[0] ) ) {
2074 $query_string = $qs_match[0];
2075 $request = preg_replace( $qs_regex, '', $request );
2080 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
2081 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
2082 $request = ltrim($request, '/');
2084 $base = trailingslashit( get_bloginfo( 'url' ) );
2086 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
2087 $base .= $wp_rewrite->index . '/';
2089 if ( $pagenum > 1 ) {
2090 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
2093 $result = $base . $request . $query_string;
2097 * Filter the page number link for the current request.
2101 * @param string $result The page number link.
2103 $result = apply_filters( 'get_pagenum_link', $result );
2106 return esc_url( $result );
2108 return esc_url_raw( $result );
2112 * Retrieve next posts page link.
2114 * Backported from 2.1.3 to 2.0.10.
2118 * @global int $paged
2120 * @param int $max_page Optional. Max pages.
2121 * @return string|void The link URL for next posts page.
2123 function get_next_posts_page_link($max_page = 0) {
2126 if ( !is_single() ) {
2129 $nextpage = intval($paged) + 1;
2130 if ( !$max_page || $max_page >= $nextpage )
2131 return get_pagenum_link($nextpage);
2136 * Display or return the next posts page link.
2140 * @param int $max_page Optional. Max pages.
2141 * @param bool $echo Optional. Echo or return;
2142 * @return string|void The link URL for next posts page if `$echo = false`.
2144 function next_posts( $max_page = 0, $echo = true ) {
2145 $output = esc_url( get_next_posts_page_link( $max_page ) );
2154 * Return the next posts page link.
2158 * @global int $paged
2159 * @global WP_Query $wp_query
2161 * @param string $label Content for link text.
2162 * @param int $max_page Optional. Max pages.
2163 * @return string|void HTML-formatted next posts page link.
2165 function get_next_posts_link( $label = null, $max_page = 0 ) {
2166 global $paged, $wp_query;
2169 $max_page = $wp_query->max_num_pages;
2174 $nextpage = intval($paged) + 1;
2176 if ( null === $label )
2177 $label = __( 'Next Page »' );
2179 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2181 * Filter the anchor tag attributes for the next posts page link.
2185 * @param string $attributes Attributes for the anchor tag.
2187 $attr = apply_filters( 'next_posts_link_attributes', '' );
2189 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2194 * Display the next posts page link.
2198 * @param string $label Content for link text.
2199 * @param int $max_page Optional. Max pages.
2201 function next_posts_link( $label = null, $max_page = 0 ) {
2202 echo get_next_posts_link( $label, $max_page );
2206 * Retrieve previous posts page link.
2208 * Will only return string, if not on a single page or post.
2210 * Backported to 2.0.10 from 2.1.3.
2214 * @global int $paged
2216 * @return string|void The link for the previous posts page.
2218 function get_previous_posts_page_link() {
2221 if ( !is_single() ) {
2222 $nextpage = intval($paged) - 1;
2223 if ( $nextpage < 1 )
2225 return get_pagenum_link($nextpage);
2230 * Display or return the previous posts page link.
2234 * @param bool $echo Optional. Echo or return;
2235 * @return string|void The previous posts page link if `$echo = false`.
2237 function previous_posts( $echo = true ) {
2238 $output = esc_url( get_previous_posts_page_link() );
2247 * Return the previous posts page link.
2251 * @global int $paged
2253 * @param string $label Optional. Previous page link text.
2254 * @return string|void HTML-formatted previous page link.
2256 function get_previous_posts_link( $label = null ) {
2259 if ( null === $label )
2260 $label = __( '« Previous Page' );
2262 if ( !is_single() && $paged > 1 ) {
2264 * Filter the anchor tag attributes for the previous posts page link.
2268 * @param string $attributes Attributes for the anchor tag.
2270 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2271 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2276 * Display the previous posts page link.
2280 * @param string $label Optional. Previous page link text.
2282 function previous_posts_link( $label = null ) {
2283 echo get_previous_posts_link( $label );
2287 * Return post pages link navigation for previous and next pages.
2291 * @global WP_Query $wp_query
2293 * @param string|array $args Optional args.
2294 * @return string The posts link navigation.
2296 function get_posts_nav_link( $args = array() ) {
2301 if ( !is_singular() ) {
2303 'sep' => ' — ',
2304 'prelabel' => __('« Previous Page'),
2305 'nxtlabel' => __('Next Page »'),
2307 $args = wp_parse_args( $args, $defaults );
2309 $max_num_pages = $wp_query->max_num_pages;
2310 $paged = get_query_var('paged');
2312 //only have sep if there's both prev and next results
2313 if ($paged < 2 || $paged >= $max_num_pages) {
2317 if ( $max_num_pages > 1 ) {
2318 $return = get_previous_posts_link($args['prelabel']);
2319 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2320 $return .= get_next_posts_link($args['nxtlabel']);
2328 * Display post pages link navigation for previous and next pages.
2332 * @param string $sep Optional. Separator for posts navigation links.
2333 * @param string $prelabel Optional. Label for previous pages.
2334 * @param string $nxtlabel Optional Label for next pages.
2336 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2337 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2338 echo get_posts_nav_link($args);
2342 * Return navigation to next/previous post when applicable.
2345 * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments.
2347 * @param array $args {
2348 * Optional. Default post navigation arguments. Default empty array.
2350 * @type string $prev_text Anchor text to display in the previous post link. Default '%title'.
2351 * @type string $next_text Anchor text to display in the next post link. Default '%title'.
2352 * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false.
2353 * @type array|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty.
2354 * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'.
2355 * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2357 * @return string Markup for post links.
2359 function get_the_post_navigation( $args = array() ) {
2360 $args = wp_parse_args( $args, array(
2361 'prev_text' => '%title',
2362 'next_text' => '%title',
2363 'in_same_term' => false,
2364 'excluded_terms' => '',
2365 'taxonomy' => 'category',
2366 'screen_reader_text' => __( 'Post navigation' ),
2371 $previous = get_previous_post_link(
2372 '<div class="nav-previous">%link</div>',
2374 $args['in_same_term'],
2375 $args['excluded_terms'],
2379 $next = get_next_post_link(
2380 '<div class="nav-next">%link</div>',
2382 $args['in_same_term'],
2383 $args['excluded_terms'],
2387 // Only add markup if there's somewhere to navigate to.
2388 if ( $previous || $next ) {
2389 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2396 * Display navigation to next/previous post when applicable.
2400 * @param array $args Optional. See {@see get_the_post_navigation()} for available
2401 * arguments. Default empty array.
2403 function the_post_navigation( $args = array() ) {
2404 echo get_the_post_navigation( $args );
2408 * Return navigation to next/previous set of posts when applicable.
2412 * @global WP_Query $wp_query WordPress Query object.
2414 * @param array $args {
2415 * Optional. Default posts navigation arguments. Default empty array.
2417 * @type string $prev_text Anchor text to display in the previous posts link.
2418 * Default 'Older posts'.
2419 * @type string $next_text Anchor text to display in the next posts link.
2420 * Default 'Newer posts'.
2421 * @type string $screen_reader_text Screen reader text for nav element.
2422 * Default 'Posts navigation'.
2424 * @return string Markup for posts links.
2426 function get_the_posts_navigation( $args = array() ) {
2429 // Don't print empty markup if there's only one page.
2430 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2431 $args = wp_parse_args( $args, array(
2432 'prev_text' => __( 'Older posts' ),
2433 'next_text' => __( 'Newer posts' ),
2434 'screen_reader_text' => __( 'Posts navigation' ),
2437 $next_link = get_previous_posts_link( $args['next_text'] );
2438 $prev_link = get_next_posts_link( $args['prev_text'] );
2441 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2445 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2448 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2455 * Display navigation to next/previous set of posts when applicable.
2459 * @param array $args Optional. See {@see get_the_posts_navigation()} for available
2460 * arguments. Default empty array.
2462 function the_posts_navigation( $args = array() ) {
2463 echo get_the_posts_navigation( $args );
2467 * Return a paginated navigation to next/previous set of posts,
2472 * @param array $args {
2473 * Optional. Default pagination arguments, {@see paginate_links()}.
2475 * @type string $screen_reader_text Screen reader text for navigation element.
2476 * Default 'Posts navigation'.
2478 * @return string Markup for pagination links.
2480 function get_the_posts_pagination( $args = array() ) {
2483 // Don't print empty markup if there's only one page.
2484 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2485 $args = wp_parse_args( $args, array(
2487 'prev_text' => _x( 'Previous', 'previous post' ),
2488 'next_text' => _x( 'Next', 'next post' ),
2489 'screen_reader_text' => __( 'Posts navigation' ),
2492 // Make sure we get a string back. Plain is the next best thing.
2493 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2494 $args['type'] = 'plain';
2497 // Set up paginated links.
2498 $links = paginate_links( $args );
2501 $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2509 * Display a paginated navigation to next/previous set of posts,
2514 * @param array $args Optional. See {@see get_the_posts_pagination()} for available arguments.
2515 * Default empty array.
2517 function the_posts_pagination( $args = array() ) {
2518 echo get_the_posts_pagination( $args );
2522 * Wraps passed links in navigational markup.
2527 * @param string $links Navigational links.
2528 * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'.
2529 * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2530 * @return string Navigation template tag.
2532 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2533 if ( empty( $screen_reader_text ) ) {
2534 $screen_reader_text = __( 'Posts navigation' );
2538 <nav class="navigation %1$s" role="navigation">
2539 <h2 class="screen-reader-text">%2$s</h2>
2540 <div class="nav-links">%3$s</div>
2544 * Filter the navigation markup template.
2546 * Note: The filtered template HTML must contain specifiers for the navigation
2547 * class (%1$s), the screen-reader-text value (%2$s), and placement of the
2548 * navigation links (%3$s):
2550 * <nav class="navigation %1$s" role="navigation">
2551 * <h2 class="screen-reader-text">%2$s</h2>
2552 * <div class="nav-links">%3$s</div>
2557 * @param string $template The default template.
2558 * @param string $class The class passed by the calling function.
2559 * @return string Navigation template.
2561 $template = apply_filters( 'navigation_markup_template', $template, $class );
2563 return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2567 * Retrieve comments page number link.
2571 * @global WP_Rewrite $wp_rewrite
2573 * @param int $pagenum Optional. Page number.
2574 * @param int $max_page Optional. The maximum number of comment pages.
2575 * @return string The comments page number link URL.
2577 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2580 $pagenum = (int) $pagenum;
2582 $result = get_permalink();
2584 if ( 'newest' == get_option('default_comments_page') ) {
2585 if ( $pagenum != $max_page ) {
2586 if ( $wp_rewrite->using_permalinks() )
2587 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2589 $result = add_query_arg( 'cpage', $pagenum, $result );
2591 } elseif ( $pagenum > 1 ) {
2592 if ( $wp_rewrite->using_permalinks() )
2593 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2595 $result = add_query_arg( 'cpage', $pagenum, $result );
2598 $result .= '#comments';
2601 * Filter the comments page number link for the current request.
2605 * @param string $result The comments page number link.
2607 return apply_filters( 'get_comments_pagenum_link', $result );
2611 * Return the link to next comments page.
2615 * @global WP_Query $wp_query
2617 * @param string $label Optional. Label for link text.
2618 * @param int $max_page Optional. Max page.
2619 * @return string|void HTML-formatted link for the next page of comments.
2621 function get_next_comments_link( $label = '', $max_page = 0 ) {
2624 if ( ! is_singular() )
2627 $page = get_query_var('cpage');
2633 $nextpage = intval($page) + 1;
2635 if ( empty($max_page) )
2636 $max_page = $wp_query->max_num_comment_pages;
2638 if ( empty($max_page) )
2639 $max_page = get_comment_pages_count();
2641 if ( $nextpage > $max_page )
2644 if ( empty($label) )
2645 $label = __('Newer Comments »');
2648 * Filter the anchor tag attributes for the next comments page link.
2652 * @param string $attributes Attributes for the anchor tag.
2654 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>';
2658 * Display the link to next comments page.
2662 * @param string $label Optional. Label for link text.
2663 * @param int $max_page Optional. Max page.
2665 function next_comments_link( $label = '', $max_page = 0 ) {
2666 echo get_next_comments_link( $label, $max_page );
2670 * Return the previous comments page link.
2674 * @param string $label Optional. Label for comments link text.
2675 * @return string|void HTML-formatted link for the previous page of comments.
2677 function get_previous_comments_link( $label = '' ) {
2678 if ( ! is_singular() )
2681 $page = get_query_var('cpage');
2683 if ( intval($page) <= 1 )
2686 $prevpage = intval($page) - 1;
2688 if ( empty($label) )
2689 $label = __('« Older Comments');
2692 * Filter the anchor tag attributes for the previous comments page link.
2696 * @param string $attributes Attributes for the anchor tag.
2698 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>';
2702 * Display the previous comments page link.
2706 * @param string $label Optional. Label for comments link text.
2708 function previous_comments_link( $label = '' ) {
2709 echo get_previous_comments_link( $label );
2713 * Create pagination links for the comments on the current post.
2715 * @see paginate_links()
2718 * @global WP_Rewrite $wp_rewrite
2720 * @param string|array $args Optional args. See paginate_links().
2721 * @return string|void Markup for pagination links.
2723 function paginate_comments_links($args = array()) {
2726 if ( ! is_singular() )
2729 $page = get_query_var('cpage');
2732 $max_page = get_comment_pages_count();
2734 'base' => add_query_arg( 'cpage', '%#%' ),
2736 'total' => $max_page,
2739 'add_fragment' => '#comments'
2741 if ( $wp_rewrite->using_permalinks() )
2742 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2744 $args = wp_parse_args( $args, $defaults );
2745 $page_links = paginate_links( $args );
2747 if ( $args['echo'] )
2754 * Returns navigation to next/previous set of comments when applicable.
2758 * @param array $args {
2759 * Optional. Default comments navigation arguments.
2761 * @type string $prev_text Anchor text to display in the previous comments link. Default 'Older comments'.
2762 * @type string $next_text Anchor text to display in the next comments link. Default 'Newer comments'.
2763 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2765 * @return string Markup for comments links.
2767 function get_the_comments_navigation( $args = array() ) {
2770 // Are there comments to navigate through?
2771 if ( get_comment_pages_count() > 1 ) {
2772 $args = wp_parse_args( $args, array(
2773 'prev_text' => __( 'Older comments' ),
2774 'next_text' => __( 'Newer comments' ),
2775 'screen_reader_text' => __( 'Comments navigation' ),
2778 $prev_link = get_previous_comments_link( $args['prev_text'] );
2779 $next_link = get_next_comments_link( $args['next_text'] );
2782 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2786 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2789 $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] );
2796 * Displays navigation to next/previous set of comments when applicable.
2800 * @param array $args See {@see get_the_comments_navigation()} for available arguments.
2802 function the_comments_navigation( $args = array() ) {
2803 echo get_the_comments_navigation( $args );
2807 * Returns a paginated navigation to next/previous set of comments,
2812 * @see paginate_comments_links()
2814 * @param array $args {
2815 * Optional. Default pagination arguments.
2817 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2819 * @return string Markup for pagination links.
2821 function get_the_comments_pagination( $args = array() ) {
2823 $args = wp_parse_args( $args, array(
2824 'screen_reader_text' => __( 'Comments navigation' ),
2826 $args['echo'] = false;
2828 // Make sure we get plain links, so we get a string we can work with.
2829 $args['type'] = 'plain';
2831 $links = paginate_comments_links( $args );
2834 $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
2841 * Displays a paginated navigation to next/previous set of comments,
2846 * @param array $args See {@see get_the_comments_pagination()} for available arguments.
2848 function the_comments_pagination( $args = array() ) {
2849 echo get_the_comments_pagination( $args );
2853 * Retrieve the Press This bookmarklet link.
2855 * Use this in 'a' element 'href' attribute.
2859 * @global bool $is_IE
2860 * @global string $wp_version
2861 * @global WP_Press_This $wp_press_this
2863 * @return string The Press This bookmarklet link URL.
2865 function get_shortcut_link() {
2866 global $is_IE, $wp_version;
2868 include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2869 $bookmarklet_version = $GLOBALS['wp_press_this']->version;
2874 * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2875 * since they only support a max length of ~2000 characters for
2876 * bookmark[let] URLs, which is way to small for our smarter one.
2877 * Do update the version number so users do not get the "upgrade your
2878 * bookmarklet" notice when using PT in those browsers.
2880 $ua = $_SERVER['HTTP_USER_AGENT'];
2882 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2883 $url = wp_json_encode( admin_url( 'press-this.php' ) );
2885 $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2886 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2887 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' .
2888 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2889 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2893 if ( empty( $link ) ) {
2894 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2897 $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version );
2898 $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2902 $link = str_replace( array( "\r", "\n", "\t" ), '', $link );
2905 * Filter the Press This bookmarklet link.
2909 * @param string $link The Press This bookmarklet link.
2911 return apply_filters( 'shortcut_link', $link );
2915 * Retrieve the home url for the current site.
2917 * Returns the 'home' option with the appropriate protocol, 'https' if
2918 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2919 * `is_ssl()` is overridden.
2923 * @param string $path Optional. Path relative to the home url. Default empty.
2924 * @param string|null $scheme Optional. Scheme to give the home url context. Accepts
2925 * 'http', 'https', 'relative', 'rest', or null. Default null.
2926 * @return string Home url link with optional path appended.
2928 function home_url( $path = '', $scheme = null ) {
2929 return get_home_url( null, $path, $scheme );
2933 * Retrieve the home url for a given site.
2935 * Returns the 'home' option with the appropriate protocol, 'https' if
2936 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2942 * @global string $pagenow
2944 * @param int $blog_id Optional. Blog ID. Default null (current blog).
2945 * @param string $path Optional. Path relative to the home URL. Default empty.
2946 * @param string|null $orig_scheme Optional. Scheme to give the home URL context. Accepts
2947 * 'http', 'https', 'relative', 'rest', or null. Default null.
2948 * @return string Home URL link with optional path appended.
2950 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2953 $orig_scheme = $scheme;
2955 if ( empty( $blog_id ) || !is_multisite() ) {
2956 $url = get_option( 'home' );
2958 switch_to_blog( $blog_id );
2959 $url = get_option( 'home' );
2960 restore_current_blog();
2963 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2964 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow )
2967 $scheme = parse_url( $url, PHP_URL_SCHEME );
2970 $url = set_url_scheme( $url, $scheme );
2972 if ( $path && is_string( $path ) )
2973 $url .= '/' . ltrim( $path, '/' );
2976 * Filter the home URL.
2980 * @param string $url The complete home URL including scheme and path.
2981 * @param string $path Path relative to the home URL. Blank string if no path is specified.
2982 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
2983 * 'relative', 'rest', or null.
2984 * @param int|null $blog_id Blog ID, or null for the current blog.
2986 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
2990 * Retrieve the site url for the current site.
2992 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2993 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2998 * @param string $path Optional. Path relative to the site url.
2999 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
3000 * @return string Site url link with optional path appended.
3002 function site_url( $path = '', $scheme = null ) {
3003 return get_site_url( null, $path, $scheme );
3007 * Retrieve the site url for a given site.
3009 * Returns the 'site_url' option with the appropriate protocol, 'https' if
3010 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
3011 * `is_ssl()` is overridden.
3015 * @param int $blog_id Optional. Blog ID. Default null (current site).
3016 * @param string $path Optional. Path relative to the site url. Default empty.
3017 * @param string $scheme Optional. Scheme to give the site url context. Accepts
3018 * 'http', 'https', 'login', 'login_post', 'admin', or
3019 * 'relative'. Default null.
3020 * @return string Site url link with optional path appended.
3022 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
3023 if ( empty( $blog_id ) || !is_multisite() ) {
3024 $url = get_option( 'siteurl' );
3026 switch_to_blog( $blog_id );
3027 $url = get_option( 'siteurl' );
3028 restore_current_blog();
3031 $url = set_url_scheme( $url, $scheme );
3033 if ( $path && is_string( $path ) )
3034 $url .= '/' . ltrim( $path, '/' );
3037 * Filter the site URL.
3041 * @param string $url The complete site URL including scheme and path.
3042 * @param string $path Path relative to the site URL. Blank string if no path is specified.
3043 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
3044 * 'login_post', 'admin', 'relative' or null.
3045 * @param int|null $blog_id Blog ID, or null for the current blog.
3047 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
3051 * Retrieve the url to the admin area for the current site.
3055 * @param string $path Optional path relative to the admin url.
3056 * @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.
3057 * @return string Admin url link with optional path appended.
3059 function admin_url( $path = '', $scheme = 'admin' ) {
3060 return get_admin_url( null, $path, $scheme );
3064 * Retrieves the url to the admin area for a given site.
3068 * @param int $blog_id Optional. Blog ID. Default null (current site).
3069 * @param string $path Optional. Path relative to the admin url. Default empty.
3070 * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
3071 * to force those schemes. Default 'admin', which obeys
3072 * {@see force_ssl_admin()} and {@see is_ssl()}.
3073 * @return string Admin url link with optional path appended.
3075 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
3076 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
3078 if ( $path && is_string( $path ) )
3079 $url .= ltrim( $path, '/' );
3082 * Filter the admin area URL.
3086 * @param string $url The complete admin area URL including scheme and path.
3087 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
3088 * @param int|null $blog_id Blog ID, or null for the current blog.
3090 return apply_filters( 'admin_url', $url, $path, $blog_id );
3094 * Retrieve the url to the includes directory.
3098 * @param string $path Optional. Path relative to the includes url.
3099 * @param string $scheme Optional. Scheme to give the includes url context.
3100 * @return string Includes url link with optional path appended.
3102 function includes_url( $path = '', $scheme = null ) {
3103 $url = site_url( '/' . WPINC . '/', $scheme );
3105 if ( $path && is_string( $path ) )
3106 $url .= ltrim($path, '/');
3109 * Filter the URL to the includes directory.
3113 * @param string $url The complete URL to the includes directory including scheme and path.
3114 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
3115 * if no path is specified.
3117 return apply_filters( 'includes_url', $url, $path );
3121 * Retrieve the url to the content directory.
3125 * @param string $path Optional. Path relative to the content url.
3126 * @return string Content url link with optional path appended.
3128 function content_url($path = '') {
3129 $url = set_url_scheme( WP_CONTENT_URL );
3131 if ( $path && is_string( $path ) )
3132 $url .= '/' . ltrim($path, '/');
3135 * Filter the URL to the content directory.
3139 * @param string $url The complete URL to the content directory including scheme and path.
3140 * @param string $path Path relative to the URL to the content directory. Blank string
3141 * if no path is specified.
3143 return apply_filters( 'content_url', $url, $path);
3147 * Retrieve a URL within the plugins or mu-plugins directory.
3149 * Defaults to the plugins directory URL if no arguments are supplied.
3153 * @param string $path Optional. Extra path appended to the end of the URL, including
3154 * the relative directory if $plugin is supplied. Default empty.
3155 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
3156 * The URL will be relative to its directory. Default empty.
3157 * Typically this is done by passing `__FILE__` as the argument.
3158 * @return string Plugins URL link with optional paths appended.
3160 function plugins_url( $path = '', $plugin = '' ) {
3162 $path = wp_normalize_path( $path );
3163 $plugin = wp_normalize_path( $plugin );
3164 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
3166 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
3167 $url = WPMU_PLUGIN_URL;
3169 $url = WP_PLUGIN_URL;
3172 $url = set_url_scheme( $url );
3174 if ( !empty($plugin) && is_string($plugin) ) {
3175 $folder = dirname(plugin_basename($plugin));
3176 if ( '.' != $folder )
3177 $url .= '/' . ltrim($folder, '/');
3180 if ( $path && is_string( $path ) )
3181 $url .= '/' . ltrim($path, '/');
3184 * Filter the URL to the plugins directory.
3188 * @param string $url The complete URL to the plugins directory including scheme and path.
3189 * @param string $path Path relative to the URL to the plugins directory. Blank string
3190 * if no path is specified.
3191 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
3194 return apply_filters( 'plugins_url', $url, $path, $plugin );
3198 * Retrieve the site url for the current network.
3200 * Returns the site url with the appropriate protocol, 'https' if
3201 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3206 * @param string $path Optional. Path relative to the site url.
3207 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
3208 * @return string Site url link with optional path appended.
3210 function network_site_url( $path = '', $scheme = null ) {
3211 if ( ! is_multisite() )
3212 return site_url($path, $scheme);
3214 $current_site = get_current_site();
3216 if ( 'relative' == $scheme )
3217 $url = $current_site->path;
3219 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3221 if ( $path && is_string( $path ) )
3222 $url .= ltrim( $path, '/' );
3225 * Filter the network site URL.
3229 * @param string $url The complete network site URL including scheme and path.
3230 * @param string $path Path relative to the network site URL. Blank string if
3231 * no path is specified.
3232 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3233 * 'relative' or null.
3235 return apply_filters( 'network_site_url', $url, $path, $scheme );
3239 * Retrieves the home url for the current network.
3241 * Returns the home url with the appropriate protocol, 'https' {@see is_ssl()}
3242 * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3247 * @param string $path Optional. Path relative to the home url. Default empty.
3248 * @param string $scheme Optional. Scheme to give the home url context. Accepts
3249 * 'http', 'https', or 'relative'. Default null.
3250 * @return string Home url link with optional path appended.
3252 function network_home_url( $path = '', $scheme = null ) {
3253 if ( ! is_multisite() )
3254 return home_url($path, $scheme);
3256 $current_site = get_current_site();
3257 $orig_scheme = $scheme;
3259 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
3260 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3262 if ( 'relative' == $scheme )
3263 $url = $current_site->path;
3265 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3267 if ( $path && is_string( $path ) )
3268 $url .= ltrim( $path, '/' );
3271 * Filter the network home URL.
3275 * @param string $url The complete network home URL including scheme and path.
3276 * @param string $path Path relative to the network home URL. Blank string
3277 * if no path is specified.
3278 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3279 * 'relative' or null.
3281 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3285 * Retrieve the url to the admin area for the network.
3289 * @param string $path Optional path relative to the admin url.
3290 * @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.
3291 * @return string Admin url link with optional path appended.
3293 function network_admin_url( $path = '', $scheme = 'admin' ) {
3294 if ( ! is_multisite() )
3295 return admin_url( $path, $scheme );
3297 $url = network_site_url('wp-admin/network/', $scheme);
3299 if ( $path && is_string( $path ) )
3300 $url .= ltrim($path, '/');
3303 * Filter the network admin URL.
3307 * @param string $url The complete network admin URL including scheme and path.
3308 * @param string $path Path relative to the network admin URL. Blank string if
3309 * no path is specified.
3311 return apply_filters( 'network_admin_url', $url, $path );
3315 * Retrieve the url to the admin area for the current user.
3319 * @param string $path Optional path relative to the admin url.
3320 * @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.
3321 * @return string Admin url link with optional path appended.
3323 function user_admin_url( $path = '', $scheme = 'admin' ) {
3324 $url = network_site_url('wp-admin/user/', $scheme);
3326 if ( $path && is_string( $path ) )
3327 $url .= ltrim($path, '/');
3330 * Filter the user admin URL for the current user.
3334 * @param string $url The complete URL including scheme and path.
3335 * @param string $path Path relative to the URL. Blank string if
3336 * no path is specified.
3338 return apply_filters( 'user_admin_url', $url, $path );
3342 * Retrieve the url to the admin area for either the current blog or the network depending on context.
3346 * @param string $path Optional path relative to the admin url.
3347 * @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.
3348 * @return string Admin url link with optional path appended.
3350 function self_admin_url($path = '', $scheme = 'admin') {
3351 if ( is_network_admin() )
3352 return network_admin_url($path, $scheme);
3353 elseif ( is_user_admin() )
3354 return user_admin_url($path, $scheme);
3356 return admin_url($path, $scheme);
3360 * Sets the scheme for a URL.
3363 * @since 4.4.0 The 'rest' scheme was added.
3365 * @param string $url Absolute url that includes a scheme
3366 * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
3367 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
3368 * @return string $url URL with chosen scheme.
3370 function set_url_scheme( $url, $scheme = null ) {
3371 $orig_scheme = $scheme;
3374 $scheme = is_ssl() ? 'https' : 'http';
3375 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3376 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3377 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3378 $scheme = is_ssl() ? 'https' : 'http';
3381 $url = trim( $url );
3382 if ( substr( $url, 0, 2 ) === '//' )
3383 $url = 'http:' . $url;
3385 if ( 'relative' == $scheme ) {
3386 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3387 if ( $url !== '' && $url[0] === '/' )
3388 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3390 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3394 * Filter the resulting URL after setting the scheme.
3398 * @param string $url The complete URL including scheme and path.
3399 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3400 * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3401 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
3403 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3407 * Get the URL to the user's dashboard.
3409 * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
3410 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
3411 * primary blog is returned.
3415 * @param int $user_id Optional. User ID. Defaults to current user.
3416 * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
3417 * @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.
3418 * @return string Dashboard url link with optional path appended.
3420 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3421 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3423 $blogs = get_blogs_of_user( $user_id );
3424 if ( ! is_super_admin() && empty($blogs) ) {
3425 $url = user_admin_url( $path, $scheme );
3426 } elseif ( ! is_multisite() ) {
3427 $url = admin_url( $path, $scheme );
3429 $current_blog = get_current_blog_id();
3430 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3431 $url = admin_url( $path, $scheme );
3433 $active = get_active_blog_for_user( $user_id );
3435 $url = get_admin_url( $active->blog_id, $path, $scheme );
3437 $url = user_admin_url( $path, $scheme );
3442 * Filter the dashboard URL for a user.
3446 * @param string $url The complete URL including scheme and path.
3447 * @param int $user_id The user ID.
3448 * @param string $path Path relative to the URL. Blank string if no path is specified.
3449 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3450 * 'login_post', 'admin', 'relative' or null.
3452 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3456 * Get the URL to the user's profile editor.
3460 * @param int $user_id Optional. User ID. Defaults to current user.
3461 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3462 * 'http' or 'https' can be passed to force those schemes.
3463 * @return string Dashboard url link with optional path appended.
3465 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3466 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3468 if ( is_user_admin() )
3469 $url = user_admin_url( 'profile.php', $scheme );
3470 elseif ( is_network_admin() )
3471 $url = network_admin_url( 'profile.php', $scheme );
3473 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3476 * Filter the URL for a user's profile editor.
3480 * @param string $url The complete URL including scheme and path.
3481 * @param int $user_id The user ID.
3482 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3483 * 'login_post', 'admin', 'relative' or null.
3485 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3489 * Output rel=canonical for singular queries.
3493 function rel_canonical() {
3494 if ( ! is_singular() ) {
3498 if ( ! $id = get_queried_object_id() ) {
3502 $url = get_permalink( $id );
3504 $page = get_query_var( 'page' );
3506 if ( '' == get_option( 'permalink_structure' ) ) {
3507 $url = add_query_arg( 'page', $page, $url );
3509 $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' );
3513 $cpage = get_query_var( 'cpage' );
3515 $url = get_comments_pagenum_link( $cpage );
3517 echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n";
3521 * Return a shortlink for a post, page, attachment, or blog.
3523 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
3524 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
3525 * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
3526 * via the get_shortlink filter.
3530 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
3531 * @param string $context Whether the id is a 'blog' id, 'post' id, or 'media' id.
3532 * If 'post', the post_type of the post is consulted.
3533 * If 'query', the current query is consulted to determine the id and context.
3534 * Default is 'post'.
3535 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3536 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3538 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3540 * Filter whether to preempt generating a shortlink for the given post.
3542 * Passing a truthy value to the filter will effectively short-circuit the
3543 * shortlink-generation process, returning that value instead.
3547 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3548 * @param int $id Post ID, or 0 for the current post.
3549 * @param string $context The context for the link. One of 'post' or 'query',
3550 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3552 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3554 if ( false !== $shortlink ) {
3559 if ( 'query' == $context && is_singular() ) {
3560 $post_id = get_queried_object_id();
3561 $post = get_post( $post_id );
3562 } elseif ( 'post' == $context ) {
3563 $post = get_post( $id );
3564 if ( ! empty( $post->ID ) )
3565 $post_id = $post->ID;
3570 // Return p= link for all public post types.
3571 if ( ! empty( $post_id ) ) {
3572 $post_type = get_post_type_object( $post->post_type );
3574 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3575 $shortlink = home_url( '/' );
3576 } elseif ( $post_type->public ) {
3577 $shortlink = home_url( '?p=' . $post_id );
3582 * Filter the shortlink for a post.
3586 * @param string $shortlink Shortlink URL.
3587 * @param int $id Post ID, or 0 for the current post.
3588 * @param string $context The context for the link. One of 'post' or 'query',
3589 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3591 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3595 * Inject rel=shortlink into head if a shortlink is defined for the current page.
3597 * Attached to the wp_head action.
3601 function wp_shortlink_wp_head() {
3602 $shortlink = wp_get_shortlink( 0, 'query' );
3604 if ( empty( $shortlink ) )
3607 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3611 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3613 * Attached to the wp action.
3617 function wp_shortlink_header() {
3618 if ( headers_sent() )
3621 $shortlink = wp_get_shortlink(0, 'query');
3623 if ( empty($shortlink) )
3626 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3630 * Display the Short Link for a Post
3632 * Must be called from inside "The Loop"
3634 * Call like the_shortlink(__('Shortlinkage FTW'))
3638 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3639 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3640 * @param string $before Optional HTML to display before the link.
3641 * @param string $after Optional HTML to display after the link.
3643 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3646 if ( empty( $text ) )
3647 $text = __('This is the short link.');
3649 if ( empty( $title ) )
3650 $title = the_title_attribute( array( 'echo' => false ) );
3652 $shortlink = wp_get_shortlink( $post->ID );
3654 if ( !empty( $shortlink ) ) {
3655 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3658 * Filter the shortlink anchor tag for a post.
3662 * @param string $link Shortlink anchor tag.
3663 * @param string $shortlink Shortlink URL.
3664 * @param string $text Shortlink's text.
3665 * @param string $title Shortlink's title attribute.
3667 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3668 echo $before, $link, $after;
3674 * Retrieve the avatar URL.
3678 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3679 * user email, WP_User object, WP_Post object, or WP_Comment object.
3680 * @param array $args {
3681 * Optional. Arguments to return instead of the default arguments.
3683 * @type int $size Height and width of the avatar in pixels. Default 96.
3684 * @type string $default URL for the default image or a default type. Accepts '404' (return
3685 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3686 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3687 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3688 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3689 * 'avatar_default' option, with a fallback of 'mystery'.
3690 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3691 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3692 * judged in that order. Default is the value of the 'avatar_rating' option.
3693 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3695 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3696 * plus a "found_avatar" guess. Pass as a reference. Default null.
3698 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3700 function get_avatar_url( $id_or_email, $args = null ) {
3701 $args = get_avatar_data( $id_or_email, $args );
3702 return $args['url'];
3706 * Retrieve default data about the avatar.
3710 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3711 * user email, WP_User object, WP_Post object, or WP_Comment object.
3712 * @param array $args {
3713 * Optional. Arguments to return instead of the default arguments.
3715 * @type int $size Height and width of the avatar image file in pixels. Default 96.
3716 * @type int $height Display height of the avatar in pixels. Defaults to $size.
3717 * @type int $width Display width of the avatar in pixels. Defaults to $size.
3718 * @type string $default URL for the default image or a default type. Accepts '404' (return
3719 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3720 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3721 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3722 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3723 * 'avatar_default' option, with a fallback of 'mystery'.
3724 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3725 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3726 * judged in that order. Default is the value of the 'avatar_rating' option.
3727 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3729 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3730 * plus a "found_avatar" guess. Pass as a reference. Default null.
3731 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3733 * @return array $processed_args {
3734 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3736 * @type bool $found_avatar True if we were able to find an avatar for this user,
3737 * false or not set if we couldn't.
3738 * @type string $url The URL of the avatar we found.
3741 function get_avatar_data( $id_or_email, $args = null ) {
3742 $args = wp_parse_args( $args, array(
3746 'default' => get_option( 'avatar_default', 'mystery' ),
3747 'force_default' => false,
3748 'rating' => get_option( 'avatar_rating' ),
3750 'processed_args' => null, // if used, should be a reference
3754 if ( is_numeric( $args['size'] ) ) {
3755 $args['size'] = absint( $args['size'] );
3756 if ( ! $args['size'] ) {
3763 if ( is_numeric( $args['height'] ) ) {
3764 $args['height'] = absint( $args['height'] );
3765 if ( ! $args['height'] ) {
3766 $args['height'] = $args['size'];
3769 $args['height'] = $args['size'];
3772 if ( is_numeric( $args['width'] ) ) {
3773 $args['width'] = absint( $args['width'] );
3774 if ( ! $args['width'] ) {
3775 $args['width'] = $args['size'];
3778 $args['width'] = $args['size'];
3781 if ( empty( $args['default'] ) ) {
3782 $args['default'] = get_option( 'avatar_default', 'mystery' );
3785 switch ( $args['default'] ) {
3789 $args['default'] = 'mm';
3791 case 'gravatar_default' :
3792 $args['default'] = false;
3796 $args['force_default'] = (bool) $args['force_default'];
3798 $args['rating'] = strtolower( $args['rating'] );
3800 $args['found_avatar'] = false;
3803 * Filter whether to retrieve the avatar URL early.
3805 * Passing a non-null value in the 'url' member of the return array will
3806 * effectively short circuit get_avatar_data(), passing the value through
3807 * the {@see 'get_avatar_data'} filter and returning early.
3811 * @param array $args Arguments passed to get_avatar_data(), after processing.
3812 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3813 * user email, WP_User object, WP_Post object, or WP_Comment object.
3815 $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3817 if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3818 /** This filter is documented in wp-includes/link-template.php */
3819 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3823 $user = $email = false;
3825 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3826 $id_or_email = get_comment( $id_or_email );
3829 // Process the user identifier.
3830 if ( is_numeric( $id_or_email ) ) {
3831 $user = get_user_by( 'id', absint( $id_or_email ) );
3832 } elseif ( is_string( $id_or_email ) ) {
3833 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3835 list( $email_hash ) = explode( '@', $id_or_email );
3838 $email = $id_or_email;
3840 } elseif ( $id_or_email instanceof WP_User ) {
3842 $user = $id_or_email;
3843 } elseif ( $id_or_email instanceof WP_Post ) {
3845 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3846 } elseif ( $id_or_email instanceof WP_Comment ) {
3848 * Filter the list of allowed comment types for retrieving avatars.
3852 * @param array $types An array of content types. Default only contains 'comment'.
3854 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3855 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3856 $args['url'] = false;
3857 /** This filter is documented in wp-includes/link-template.php */
3858 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3861 if ( ! empty( $id_or_email->user_id ) ) {
3862 $user = get_user_by( 'id', (int) $id_or_email->user_id );
3864 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3865 $email = $id_or_email->comment_author_email;
3869 if ( ! $email_hash ) {
3871 $email = $user->user_email;
3875 $email_hash = md5( strtolower( trim( $email ) ) );
3879 if ( $email_hash ) {
3880 $args['found_avatar'] = true;
3881 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3883 $gravatar_server = rand( 0, 2 );
3887 's' => $args['size'],
3888 'd' => $args['default'],
3889 'f' => $args['force_default'] ? 'y' : false,
3890 'r' => $args['rating'],
3894 $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
3896 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
3899 $url = add_query_arg(
3900 rawurlencode_deep( array_filter( $url_args ) ),
3901 set_url_scheme( $url, $args['scheme'] )
3905 * Filter the avatar URL.
3909 * @param string $url The URL of the avatar.
3910 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3911 * user email, WP_User object, WP_Post object, or WP_Comment object.
3912 * @param array $args Arguments passed to get_avatar_data(), after processing.
3914 $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
3917 * Filter the avatar data.
3921 * @param array $args Arguments passed to get_avatar_data(), after processing.
3922 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3923 * user email, WP_User object, WP_Post object, or WP_Comment object.
3925 return apply_filters( 'get_avatar_data', $args, $id_or_email );