3 * WordPress Link Template Functions
10 * Displays 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 * Filters 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 * Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
33 * Conditionally adds a trailing slash if the permalink structure has a trailing
34 * slash, strips the trailing slash if not. The string is passed through the
35 * {@see 'user_trailingslashit'} filter. Will remove trailing slash from string, if
36 * site is not set to have them.
40 * @global WP_Rewrite $wp_rewrite
42 * @param string $string URL with or without a trailing slash.
43 * @param string $type_of_url Optional. The type of URL being considered (e.g. single, category, etc)
44 * for use in the filter. Default empty string.
45 * @return string The URL with the trailing slash appended or stripped.
47 function user_trailingslashit($string, $type_of_url = '') {
49 if ( $wp_rewrite->use_trailing_slashes )
50 $string = trailingslashit($string);
52 $string = untrailingslashit($string);
55 * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes.
59 * @param string $string URL with or without a trailing slash.
60 * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
61 * 'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',
62 * 'month', 'day', 'paged', 'post_type_archive'.
64 return apply_filters( 'user_trailingslashit', $string, $type_of_url );
68 * Displays the permalink anchor for the current post.
70 * The permalink mode title will use the post title for the 'a' element 'id'
71 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
75 * @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'.
77 function permalink_anchor( $mode = 'id' ) {
79 switch ( strtolower( $mode ) ) {
81 $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
82 echo '<a id="'.$title.'"></a>';
86 echo '<a id="post-' . $post->ID . '"></a>';
92 * Retrieves the full permalink for the current post or post ID.
94 * This function is an alias for get_permalink().
98 * @see get_permalink()
100 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
101 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
103 * @return string|false The permalink URL or false if post does not exist.
105 function get_the_permalink( $post = 0, $leavename = false ) {
106 return get_permalink( $post, $leavename );
110 * Retrieves the full permalink for the current post or post ID.
114 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
115 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
116 * @return string|false The permalink URL or false if post does not exist.
118 function get_permalink( $post = 0, $leavename = false ) {
119 $rewritecode = array(
126 $leavename? '' : '%postname%',
130 $leavename? '' : '%pagename%',
133 if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) {
136 $post = get_post( $post );
140 if ( empty($post->ID) )
143 if ( $post->post_type == 'page' )
144 return get_page_link($post, $leavename, $sample);
145 elseif ( $post->post_type == 'attachment' )
146 return get_attachment_link( $post, $leavename );
147 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
148 return get_post_permalink($post, $leavename, $sample);
150 $permalink = get_option('permalink_structure');
153 * Filters the permalink structure for a post before token replacement occurs.
155 * Only applies to posts with post_type of 'post'.
159 * @param string $permalink The site's permalink structure.
160 * @param WP_Post $post The post in question.
161 * @param bool $leavename Whether to keep the post name.
163 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
165 if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
166 $unixtime = strtotime($post->post_date);
169 if ( strpos($permalink, '%category%') !== false ) {
170 $cats = get_the_category($post->ID);
172 usort($cats, '_usort_terms_by_ID'); // order by ID
175 * Filters the category that gets used in the %category% permalink token.
179 * @param stdClass $cat The category to use in the permalink.
180 * @param array $cats Array of all categories associated with the post.
181 * @param WP_Post $post The post in question.
183 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
185 $category_object = get_term( $category_object, 'category' );
186 $category = $category_object->slug;
187 if ( $parent = $category_object->parent )
188 $category = get_category_parents($parent, false, '/', true) . $category;
190 // show default category in permalinks, without
191 // having to assign it explicitly
192 if ( empty($category) ) {
193 $default_category = get_term( get_option( 'default_category' ), 'category' );
194 if ( $default_category && ! is_wp_error( $default_category ) ) {
195 $category = $default_category->slug;
201 if ( strpos($permalink, '%author%') !== false ) {
202 $authordata = get_userdata($post->post_author);
203 $author = $authordata->user_nicename;
206 $date = explode(" ",date('Y m d H i s', $unixtime));
221 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
222 $permalink = user_trailingslashit($permalink, 'single');
223 } else { // if they're not using the fancy permalink option
224 $permalink = home_url('?p=' . $post->ID);
228 * Filters the permalink for a post.
230 * Only applies to posts with post_type of 'post'.
234 * @param string $permalink The post's permalink.
235 * @param WP_Post $post The post in question.
236 * @param bool $leavename Whether to keep the post name.
238 return apply_filters( 'post_link', $permalink, $post, $leavename );
242 * Retrieves the permalink for a post of a custom post type.
246 * @global WP_Rewrite $wp_rewrite
248 * @param int $id Optional. Post ID. Default uses the global `$post`.
249 * @param bool $leavename Optional, defaults to false. Whether to keep post name. Default false.
250 * @param bool $sample Optional, defaults to false. Is it a sample permalink. Default false.
251 * @return string|WP_Error The post permalink.
253 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
256 $post = get_post($id);
258 if ( is_wp_error( $post ) )
261 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
263 $slug = $post->post_name;
265 $draft_or_pending = get_post_status( $id ) && in_array( get_post_status( $id ), array( 'draft', 'pending', 'auto-draft', 'future' ) );
267 $post_type = get_post_type_object($post->post_type);
269 if ( $post_type->hierarchical ) {
270 $slug = get_page_uri( $id );
273 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
274 if ( ! $leavename ) {
275 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
277 $post_link = home_url( user_trailingslashit($post_link) );
279 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
280 $post_link = add_query_arg($post_type->query_var, $slug, '');
282 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
283 $post_link = home_url($post_link);
287 * Filters the permalink for a post of a custom post type.
291 * @param string $post_link The post's permalink.
292 * @param WP_Post $post The post in question.
293 * @param bool $leavename Whether to keep the post name.
294 * @param bool $sample Is it a sample permalink.
296 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
300 * Retrieves the permalink for the current page or page ID.
302 * Respects page_on_front. Use this one.
306 * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
307 * @param bool $leavename Optional. Whether to keep the page name. Default false.
308 * @param bool $sample Optional. Whether it should be treated as a sample permalink.
310 * @return string The page permalink.
312 function get_page_link( $post = false, $leavename = false, $sample = false ) {
313 $post = get_post( $post );
315 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
316 $link = home_url('/');
318 $link = _get_page_link( $post, $leavename, $sample );
321 * Filters the permalink for a page.
325 * @param string $link The page's permalink.
326 * @param int $post_id The ID of the page.
327 * @param bool $sample Is it a sample permalink.
329 return apply_filters( 'page_link', $link, $post->ID, $sample );
333 * Retrieves the page permalink.
335 * Ignores page_on_front. Internal use only.
340 * @global WP_Rewrite $wp_rewrite
342 * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
343 * @param bool $leavename Optional. Whether to keep the page name. Default false.
344 * @param bool $sample Optional. Whether it should be treated as a sample permalink.
346 * @return string The page permalink.
348 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
351 $post = get_post( $post );
353 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
355 $link = $wp_rewrite->get_page_permastruct();
357 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
358 if ( ! $leavename ) {
359 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
362 $link = home_url($link);
363 $link = user_trailingslashit($link, 'page');
365 $link = home_url( '?page_id=' . $post->ID );
369 * Filters the permalink for a non-page_on_front page.
373 * @param string $link The page's permalink.
374 * @param int $post_id The ID of the page.
376 return apply_filters( '_get_page_link', $link, $post->ID );
380 * Retrieves the permalink for an attachment.
382 * This can be used in the WordPress Loop or outside of it.
386 * @global WP_Rewrite $wp_rewrite
388 * @param int|object $post Optional. Post ID or object. Default uses the global `$post`.
389 * @param bool $leavename Optional. Whether to keep the page name. Default false.
390 * @return string The attachment permalink.
392 function get_attachment_link( $post = null, $leavename = false ) {
397 $post = get_post( $post );
398 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
399 if ( $parent && ! in_array( $parent->post_type, get_post_types() ) ) {
403 if ( $wp_rewrite->using_permalinks() && $parent ) {
404 if ( 'page' == $parent->post_type )
405 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
407 $parentlink = get_permalink( $post->post_parent );
409 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
410 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
412 $name = $post->post_name;
414 if ( strpos($parentlink, '?') === false )
415 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
418 $link = str_replace( '%postname%', $name, $link );
419 } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
420 $link = home_url( user_trailingslashit( $post->post_name ) );
424 $link = home_url( '/?attachment_id=' . $post->ID );
427 * Filters the permalink for an attachment.
431 * @param string $link The attachment's permalink.
432 * @param int $post_id Attachment ID.
434 return apply_filters( 'attachment_link', $link, $post->ID );
438 * Retrieves the permalink for the year archives.
442 * @global WP_Rewrite $wp_rewrite
444 * @param int|bool $year False for current year or year for permalink.
445 * @return string The permalink for the specified year archive.
447 function get_year_link( $year ) {
450 $year = gmdate('Y', current_time('timestamp'));
451 $yearlink = $wp_rewrite->get_year_permastruct();
452 if ( !empty($yearlink) ) {
453 $yearlink = str_replace('%year%', $year, $yearlink);
454 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
456 $yearlink = home_url( '?m=' . $year );
460 * Filters the year archive permalink.
464 * @param string $yearlink Permalink for the year archive.
465 * @param int $year Year for the archive.
467 return apply_filters( 'year_link', $yearlink, $year );
471 * Retrieves the permalink for the month archives with year.
475 * @global WP_Rewrite $wp_rewrite
477 * @param bool|int $year False for current year. Integer of year.
478 * @param bool|int $month False for current month. Integer of month.
479 * @return string The permalink for the specified month and year archive.
481 function get_month_link($year, $month) {
484 $year = gmdate('Y', current_time('timestamp'));
486 $month = gmdate('m', current_time('timestamp'));
487 $monthlink = $wp_rewrite->get_month_permastruct();
488 if ( !empty($monthlink) ) {
489 $monthlink = str_replace('%year%', $year, $monthlink);
490 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
491 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
493 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
497 * Filters the month archive permalink.
501 * @param string $monthlink Permalink for the month archive.
502 * @param int $year Year for the archive.
503 * @param int $month The month for the archive.
505 return apply_filters( 'month_link', $monthlink, $year, $month );
509 * Retrieves the permalink for the day archives with year and month.
513 * @global WP_Rewrite $wp_rewrite
515 * @param bool|int $year False for current year. Integer of year.
516 * @param bool|int $month False for current month. Integer of month.
517 * @param bool|int $day False for current day. Integer of day.
518 * @return string The permalink for the specified day, month, and year archive.
520 function get_day_link($year, $month, $day) {
523 $year = gmdate('Y', current_time('timestamp'));
525 $month = gmdate('m', current_time('timestamp'));
527 $day = gmdate('j', current_time('timestamp'));
529 $daylink = $wp_rewrite->get_day_permastruct();
530 if ( !empty($daylink) ) {
531 $daylink = str_replace('%year%', $year, $daylink);
532 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
533 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
534 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
536 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
540 * Filters the day archive permalink.
544 * @param string $daylink Permalink for the day archive.
545 * @param int $year Year for the archive.
546 * @param int $month Month for the archive.
547 * @param int $day The day for the archive.
549 return apply_filters( 'day_link', $daylink, $year, $month, $day );
553 * Displays the permalink for the feed type.
557 * @param string $anchor The link's anchor text.
558 * @param string $feed Optional. Feed type. Default empty.
560 function the_feed_link( $anchor, $feed = '' ) {
561 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
564 * Filters the feed link anchor tag.
568 * @param string $link The complete anchor tag for a feed link.
569 * @param string $feed The feed type, or an empty string for the
572 echo apply_filters( 'the_feed_link', $link, $feed );
576 * Retrieves the permalink for the feed type.
580 * @global WP_Rewrite $wp_rewrite
582 * @param string $feed Optional. Feed type. Default empty.
583 * @return string The feed permalink.
585 function get_feed_link( $feed = '' ) {
588 $permalink = $wp_rewrite->get_feed_permastruct();
589 if ( '' != $permalink ) {
590 if ( false !== strpos($feed, 'comments_') ) {
591 $feed = str_replace('comments_', '', $feed);
592 $permalink = $wp_rewrite->get_comment_feed_permastruct();
595 if ( get_default_feed() == $feed )
598 $permalink = str_replace('%feed%', $feed, $permalink);
599 $permalink = preg_replace('#/+#', '/', "/$permalink");
600 $output = home_url( user_trailingslashit($permalink, 'feed') );
603 $feed = get_default_feed();
605 if ( false !== strpos($feed, 'comments_') )
606 $feed = str_replace('comments_', 'comments-', $feed);
608 $output = home_url("?feed={$feed}");
612 * Filters the feed type permalink.
616 * @param string $output The feed permalink.
617 * @param string $feed Feed type.
619 return apply_filters( 'feed_link', $output, $feed );
623 * Retrieves the permalink for the post comments feed.
627 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
628 * @param string $feed Optional. Feed type. Default empty.
629 * @return string The permalink for the comments feed for the given post.
631 function get_post_comments_feed_link( $post_id = 0, $feed = '' ) {
632 $post_id = absint( $post_id );
635 $post_id = get_the_ID();
637 if ( empty( $feed ) )
638 $feed = get_default_feed();
640 $post = get_post( $post_id );
641 $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent;
643 if ( '' != get_option('permalink_structure') ) {
644 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
645 $url = _get_page_link( $post_id );
647 $url = get_permalink($post_id);
650 $url = home_url( '/feed/' );
651 if ( $feed !== get_default_feed() ) {
654 $url = add_query_arg( 'attachment_id', $post_id, $url );
656 $url = trailingslashit($url) . 'feed';
657 if ( $feed != get_default_feed() )
659 $url = user_trailingslashit($url, 'single_feed');
663 $url = add_query_arg( array( 'feed' => $feed, 'attachment_id' => $post_id ), home_url( '/' ) );
664 } elseif ( 'page' == $post->post_type ) {
665 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
667 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
672 * Filters the post comments feed permalink.
676 * @param string $url Post comments feed permalink.
678 return apply_filters( 'post_comments_feed_link', $url );
682 * Displays the comment feed link for a post.
684 * Prints out the comment feed link for a post. Link text is placed in the
685 * anchor. If no link text is specified, default text is used. If no post ID is
686 * specified, the current post is used.
690 * @param string $link_text Optional. Descriptive link text. Default 'Comments Feed'.
691 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
692 * @param string $feed Optional. Feed format. Default empty.
694 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
695 $url = get_post_comments_feed_link( $post_id, $feed );
696 if ( empty( $link_text ) ) {
697 $link_text = __('Comments Feed');
700 $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>';
702 * Filters the post comment feed link anchor tag.
706 * @param string $link The complete anchor tag for the comment feed link.
707 * @param int $post_id Post ID.
708 * @param string $feed The feed type, or an empty string for the default feed type.
710 echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed );
714 * Retrieves the feed link for a given author.
716 * Returns a link to the feed for all posts by a given author. A specific feed
717 * can be requested or left blank to get the default feed.
721 * @param int $author_id Author ID.
722 * @param string $feed Optional. Feed type. Default empty.
723 * @return string Link to the feed for the author specified by $author_id.
725 function get_author_feed_link( $author_id, $feed = '' ) {
726 $author_id = (int) $author_id;
727 $permalink_structure = get_option('permalink_structure');
730 $feed = get_default_feed();
732 if ( '' == $permalink_structure ) {
733 $link = home_url("?feed=$feed&author=" . $author_id);
735 $link = get_author_posts_url($author_id);
736 if ( $feed == get_default_feed() )
739 $feed_link = "feed/$feed";
741 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
745 * Filters the feed link for a given author.
749 * @param string $link The author feed link.
750 * @param string $feed Feed type.
752 $link = apply_filters( 'author_feed_link', $link, $feed );
758 * Retrieves the feed link for a category.
760 * Returns a link to the feed for all posts in a given category. A specific feed
761 * can be requested or left blank to get the default feed.
765 * @param int $cat_id Category ID.
766 * @param string $feed Optional. Feed type. Default empty.
767 * @return string Link to the feed for the category specified by $cat_id.
769 function get_category_feed_link( $cat_id, $feed = '' ) {
770 return get_term_feed_link( $cat_id, 'category', $feed );
774 * Retrieves the feed link for a term.
776 * Returns a link to the feed for all posts in a given term. A specific feed
777 * can be requested or left blank to get the default feed.
781 * @param int $term_id Term ID.
782 * @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'.
783 * @param string $feed Optional. Feed type. Default empty.
784 * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
786 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
787 $term_id = ( int ) $term_id;
789 $term = get_term( $term_id, $taxonomy );
791 if ( empty( $term ) || is_wp_error( $term ) )
794 if ( empty( $feed ) )
795 $feed = get_default_feed();
797 $permalink_structure = get_option( 'permalink_structure' );
799 if ( '' == $permalink_structure ) {
800 if ( 'category' == $taxonomy ) {
801 $link = home_url("?feed=$feed&cat=$term_id");
803 elseif ( 'post_tag' == $taxonomy ) {
804 $link = home_url("?feed=$feed&tag=$term->slug");
806 $t = get_taxonomy( $taxonomy );
807 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
810 $link = get_term_link( $term_id, $term->taxonomy );
811 if ( $feed == get_default_feed() )
814 $feed_link = "feed/$feed";
816 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
819 if ( 'category' == $taxonomy ) {
821 * Filters the category feed link.
825 * @param string $link The category feed link.
826 * @param string $feed Feed type.
828 $link = apply_filters( 'category_feed_link', $link, $feed );
829 } elseif ( 'post_tag' == $taxonomy ) {
831 * Filters the post tag feed link.
835 * @param string $link The tag feed link.
836 * @param string $feed Feed type.
838 $link = apply_filters( 'tag_feed_link', $link, $feed );
841 * Filters the feed link for a taxonomy other than 'category' or 'post_tag'.
845 * @param string $link The taxonomy feed link.
846 * @param string $feed Feed type.
847 * @param string $feed The taxonomy name.
849 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
856 * Retrieves the permalink for a tag feed.
860 * @param int $tag_id Tag ID.
861 * @param string $feed Optional. Feed type. Default empty.
862 * @return string The feed permalink for the given tag.
864 function get_tag_feed_link( $tag_id, $feed = '' ) {
865 return get_term_feed_link( $tag_id, 'post_tag', $feed );
869 * Retrieves the edit link for a tag.
873 * @param int $tag_id Tag ID.
874 * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'.
875 * @return string The edit tag link URL for the given tag.
877 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
879 * Filters the edit link for a tag (or term in another taxonomy).
883 * @param string $link The term edit link.
885 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
889 * Displays or retrieves the edit link for a tag with formatting.
893 * @param string $link Optional. Anchor text. Default empty.
894 * @param string $before Optional. Display before edit link. Default empty.
895 * @param string $after Optional. Display after edit link. Default empty.
896 * @param WP_Term $tag Optional. Term object. If null, the queried object will be inspected.
899 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
900 $link = edit_term_link( $link, '', '', $tag, false );
903 * Filters the anchor tag for the edit link for a tag (or term in another taxonomy).
907 * @param string $link The anchor tag for the edit link.
909 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
913 * Retrieves the URL for editing a given term.
916 * @since 4.5.0 The `$taxonomy` argument was made optional.
918 * @param int $term_id Term ID.
919 * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified
921 * @param string $object_type Optional. The object type. Used to highlight the proper post type
922 * menu on the linked page. Defaults to the first object_type associated
924 * @return string|null The edit term link URL for the given term, or null on failure.
926 function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
927 $term = get_term( $term_id, $taxonomy );
928 if ( ! $term || is_wp_error( $term ) ) {
932 $tax = get_taxonomy( $term->taxonomy );
933 if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
938 'taxonomy' => $taxonomy,
939 'tag_ID' => $term->term_id,
942 if ( $object_type ) {
943 $args['post_type'] = $object_type;
944 } elseif ( ! empty( $tax->object_type ) ) {
945 $args['post_type'] = reset( $tax->object_type );
948 if ( $tax->show_ui ) {
949 $location = add_query_arg( $args, admin_url( 'term.php' ) );
955 * Filters the edit link for a term.
959 * @param string $location The edit link.
960 * @param int $term_id Term ID.
961 * @param string $taxonomy Taxonomy name.
962 * @param string $object_type The object type (eg. the post type).
964 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
968 * Displays or retrieves the edit term link with formatting.
972 * @param string $link Optional. Anchor text. Default empty.
973 * @param string $before Optional. Display before edit link. Default empty.
974 * @param string $after Optional. Display after edit link. Default empty.
975 * @param object $term Optional. Term object. If null, the queried object will be inspected. Default null.
976 * @param bool $echo Optional. Whether or not to echo the return. Default true.
977 * @return string|void HTML content.
979 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
980 if ( is_null( $term ) )
981 $term = get_queried_object();
986 $tax = get_taxonomy( $term->taxonomy );
987 if ( ! current_user_can( $tax->cap->edit_terms ) )
990 if ( empty( $link ) )
991 $link = __('Edit This');
993 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
996 * Filters the anchor tag for the edit link of a term.
1000 * @param string $link The anchor tag for the edit link.
1001 * @param int $term_id Term ID.
1003 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
1012 * Retrieves the permalink for a search.
1016 * @global WP_Rewrite $wp_rewrite
1018 * @param string $query Optional. The query string to use. If empty the current query is used. Default empty.
1019 * @return string The search permalink.
1021 function get_search_link( $query = '' ) {
1024 if ( empty($query) )
1025 $search = get_search_query( false );
1027 $search = stripslashes($query);
1029 $permastruct = $wp_rewrite->get_search_permastruct();
1031 if ( empty( $permastruct ) ) {
1032 $link = home_url('?s=' . urlencode($search) );
1034 $search = urlencode($search);
1035 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it un-encoded.
1036 $link = str_replace( '%search%', $search, $permastruct );
1037 $link = home_url( user_trailingslashit( $link, 'search' ) );
1041 * Filters the search permalink.
1045 * @param string $link Search permalink.
1046 * @param string $search The URL-encoded search term.
1048 return apply_filters( 'search_link', $link, $search );
1052 * Retrieves the permalink for the search results feed.
1056 * @global WP_Rewrite $wp_rewrite
1058 * @param string $search_query Optional. Search query. Default empty.
1059 * @param string $feed Optional. Feed type. Default empty.
1060 * @return string The search results feed permalink.
1062 function get_search_feed_link($search_query = '', $feed = '') {
1064 $link = get_search_link($search_query);
1067 $feed = get_default_feed();
1069 $permastruct = $wp_rewrite->get_search_permastruct();
1071 if ( empty($permastruct) ) {
1072 $link = add_query_arg('feed', $feed, $link);
1074 $link = trailingslashit($link);
1075 $link .= "feed/$feed/";
1079 * Filters the search feed link.
1083 * @param string $link Search feed link.
1084 * @param string $feed Feed type.
1085 * @param string $type The search type. One of 'posts' or 'comments'.
1087 return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1091 * Retrieves the permalink for the search results comments feed.
1095 * @global WP_Rewrite $wp_rewrite
1097 * @param string $search_query Optional. Search query. Default empty.
1098 * @param string $feed Optional. Feed type. Default empty.
1099 * @return string The comments feed search results permalink.
1101 function get_search_comments_feed_link($search_query = '', $feed = '') {
1105 $feed = get_default_feed();
1107 $link = get_search_feed_link($search_query, $feed);
1109 $permastruct = $wp_rewrite->get_search_permastruct();
1111 if ( empty($permastruct) )
1112 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1114 $link = add_query_arg('withcomments', 1, $link);
1116 /** This filter is documented in wp-includes/link-template.php */
1117 return apply_filters( 'search_feed_link', $link, $feed, 'comments' );
1121 * Retrieves the permalink for a post type archive.
1124 * @since 4.5.0 Support for posts was added.
1126 * @global WP_Rewrite $wp_rewrite
1128 * @param string $post_type Post type.
1129 * @return string|false The post type archive permalink.
1131 function get_post_type_archive_link( $post_type ) {
1133 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1136 if ( 'post' === $post_type ) {
1137 $show_on_front = get_option( 'show_on_front' );
1138 $page_for_posts = get_option( 'page_for_posts' );
1140 if ( 'page' == $show_on_front && $page_for_posts ) {
1141 $link = get_permalink( $page_for_posts );
1143 $link = get_home_url();
1145 /** This filter is documented in wp-includes/link-template.php */
1146 return apply_filters( 'post_type_archive_link', $link, $post_type );
1149 if ( ! $post_type_obj->has_archive )
1152 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1153 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1154 if ( $post_type_obj->rewrite['with_front'] )
1155 $struct = $wp_rewrite->front . $struct;
1157 $struct = $wp_rewrite->root . $struct;
1158 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1160 $link = home_url( '?post_type=' . $post_type );
1164 * Filters the post type archive permalink.
1168 * @param string $link The post type archive permalink.
1169 * @param string $post_type Post type name.
1171 return apply_filters( 'post_type_archive_link', $link, $post_type );
1175 * Retrieves the permalink for a post type archive feed.
1179 * @param string $post_type Post type
1180 * @param string $feed Optional. Feed type. Default empty.
1181 * @return string|false The post type feed permalink.
1183 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1184 $default_feed = get_default_feed();
1185 if ( empty( $feed ) )
1186 $feed = $default_feed;
1188 if ( ! $link = get_post_type_archive_link( $post_type ) )
1191 $post_type_obj = get_post_type_object( $post_type );
1192 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1193 $link = trailingslashit( $link );
1195 if ( $feed != $default_feed )
1198 $link = add_query_arg( 'feed', $feed, $link );
1202 * Filters the post type archive feed link.
1206 * @param string $link The post type archive feed link.
1207 * @param string $feed Feed type.
1209 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1213 * Retrieves the URL used for the post preview.
1215 * Allows additional query args to be appended.
1219 * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global `$post`.
1220 * @param array $query_args Optional. Array of additional query args to be appended to the link.
1221 * Default empty array.
1222 * @param string $preview_link Optional. Base preview link to be used if it should differ from the
1223 * post permalink. Default empty.
1224 * @return string|null URL used for the post preview, or null if the post does not exist.
1226 function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
1227 $post = get_post( $post );
1232 $post_type_object = get_post_type_object( $post->post_type );
1233 if ( is_post_type_viewable( $post_type_object ) ) {
1234 if ( ! $preview_link ) {
1235 $preview_link = set_url_scheme( get_permalink( $post ) );
1238 $query_args['preview'] = 'true';
1239 $preview_link = add_query_arg( $query_args, $preview_link );
1243 * Filters the URL used for a post preview.
1246 * @since 4.0.0 Added the `$post` parameter.
1248 * @param string $preview_link URL used for the post preview.
1249 * @param WP_Post $post Post object.
1251 return apply_filters( 'preview_post_link', $preview_link, $post );
1255 * Retrieves the edit post link for post.
1257 * Can be used within the WordPress loop or outside of it. Can be used with
1258 * pages, posts, attachments, and revisions.
1262 * @param int $id Optional. Post ID. Default is the ID of the global `$post`.
1263 * @param string $context Optional. How to output the '&' character. Default '&'.
1264 * @return string|null The edit post link for the given post. null if the post type is invalid or does
1265 * not allow an editing UI.
1267 function get_edit_post_link( $id = 0, $context = 'display' ) {
1268 if ( ! $post = get_post( $id ) )
1271 if ( 'revision' === $post->post_type )
1273 elseif ( 'display' == $context )
1274 $action = '&action=edit';
1276 $action = '&action=edit';
1278 $post_type_object = get_post_type_object( $post->post_type );
1279 if ( !$post_type_object )
1282 if ( !current_user_can( 'edit_post', $post->ID ) )
1285 if ( $post_type_object->_edit_link ) {
1286 $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
1292 * Filters the post edit link.
1296 * @param string $link The edit link.
1297 * @param int $post_id Post ID.
1298 * @param string $context The link context. If set to 'display' then ampersands
1301 return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
1305 * Displays the edit post link for post.
1308 * @since 4.4.0 The `$class` argument was added.
1310 * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null.
1311 * @param string $before Optional. Display before edit link. Default empty.
1312 * @param string $after Optional. Display after edit link. Default empty.
1313 * @param int $id Optional. Post ID. Default is the ID of the global `$post`.
1314 * @param string $class Optional. Add custom class to link. Default 'post-edit-link'.
1316 function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
1317 if ( ! $post = get_post( $id ) ) {
1321 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1325 if ( null === $text ) {
1326 $text = __( 'Edit This' );
1329 $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
1332 * Filters the post edit link anchor tag.
1336 * @param string $link Anchor tag for the edit link.
1337 * @param int $post_id Post ID.
1338 * @param string $text Anchor text.
1340 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1344 * Retrieves the delete posts link for post.
1346 * Can be used within the WordPress loop or outside of it, with any post type.
1350 * @param int $id Optional. Post ID. Default is the ID of the global `$post`.
1351 * @param string $deprecated Not used.
1352 * @param bool $force_delete Optional. Whether to bypass trash and force deletion. Default false.
1353 * @return string|void The delete post link URL for the given post.
1355 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1356 if ( ! empty( $deprecated ) )
1357 _deprecated_argument( __FUNCTION__, '3.0.0' );
1359 if ( !$post = get_post( $id ) )
1362 $post_type_object = get_post_type_object( $post->post_type );
1363 if ( !$post_type_object )
1366 if ( !current_user_can( 'delete_post', $post->ID ) )
1369 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1371 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1374 * Filters the post delete link.
1378 * @param string $link The delete link.
1379 * @param int $post_id Post ID.
1380 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1382 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1386 * Retrieves the edit comment link.
1390 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
1391 * @return string|void The edit comment link URL for the given comment.
1393 function get_edit_comment_link( $comment_id = 0 ) {
1394 $comment = get_comment( $comment_id );
1396 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1399 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1402 * Filters the comment edit link.
1406 * @param string $location The edit link.
1408 return apply_filters( 'get_edit_comment_link', $location );
1412 * Displays the edit comment link with formatting.
1416 * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null.
1417 * @param string $before Optional. Display before edit link. Default empty.
1418 * @param string $after Optional. Display after edit link. Default empty.
1420 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1421 $comment = get_comment();
1423 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1427 if ( null === $text ) {
1428 $text = __( 'Edit This' );
1431 $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
1434 * Filters the comment edit link anchor tag.
1438 * @param string $link Anchor tag for the edit link.
1439 * @param int $comment_id Comment ID.
1440 * @param string $text Anchor text.
1442 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1446 * Displays the edit bookmark link.
1450 * @param int|stdClass $link Optional. Bookmark ID. Default is the id of the current bookmark.
1451 * @return string|void The edit bookmark link URL.
1453 function get_edit_bookmark_link( $link = 0 ) {
1454 $link = get_bookmark( $link );
1456 if ( !current_user_can('manage_links') )
1459 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1462 * Filters the bookmark edit link.
1466 * @param string $location The edit link.
1467 * @param int $link_id Bookmark ID.
1469 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1473 * Displays the edit bookmark link anchor content.
1477 * @param string $link Optional. Anchor text. Default empty.
1478 * @param string $before Optional. Display before edit link. Default empty.
1479 * @param string $after Optional. Display after edit link. Default empty.
1480 * @param int $bookmark Optional. Bookmark ID. Default is the current bookmark.
1482 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1483 $bookmark = get_bookmark($bookmark);
1485 if ( !current_user_can('manage_links') )
1489 $link = __('Edit This');
1491 $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>';
1494 * Filters the bookmark edit link anchor tag.
1498 * @param string $link Anchor tag for the edit link.
1499 * @param int $link_id Bookmark ID.
1501 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1505 * Retrieves the edit user link.
1509 * @param int $user_id Optional. User ID. Defaults to the current user.
1510 * @return string URL to edit user page or empty string.
1512 function get_edit_user_link( $user_id = null ) {
1514 $user_id = get_current_user_id();
1516 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1519 $user = get_userdata( $user_id );
1524 if ( get_current_user_id() == $user->ID )
1525 $link = get_edit_profile_url( $user->ID );
1527 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1530 * Filters the user edit link.
1534 * @param string $link The edit link.
1535 * @param int $user_id User ID.
1537 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1543 * Retrieves the previous post that is adjacent to the current post.
1547 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
1548 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1549 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1550 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
1551 * corresponding post exists.
1553 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1554 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1558 * Retrieves the next post that is adjacent to the current post.
1562 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
1563 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1564 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1565 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
1566 * corresponding post exists.
1568 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1569 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1573 * Retrieves the adjacent post.
1575 * Can either be next or previous post.
1579 * @global wpdb $wpdb WordPress database abstraction object.
1581 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
1582 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1583 * @param bool $previous Optional. Whether to retrieve previous post. Default true
1584 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1585 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
1586 * corresponding post exists.
1588 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1591 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1594 $current_post_date = $post->post_date;
1598 $adjacent = $previous ? 'previous' : 'next';
1600 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1601 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1602 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1603 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1604 _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1605 $excluded_terms = explode( ' and ', $excluded_terms );
1607 $excluded_terms = explode( ',', $excluded_terms );
1610 $excluded_terms = array_map( 'intval', $excluded_terms );
1613 if ( $in_same_term ) {
1614 $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";
1615 $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1617 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1619 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1621 // Remove any exclusions from the term array to include.
1622 $term_array = array_diff( $term_array, (array) $excluded_terms );
1623 $term_array = array_map( 'intval', $term_array );
1625 if ( ! $term_array || is_wp_error( $term_array ) )
1628 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1632 * Filters the IDs of terms excluded from adjacent post queries.
1634 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1635 * of adjacency, 'next' or 'previous'.
1639 * @param string $excluded_terms Array of excluded term IDs.
1641 $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
1643 if ( ! empty( $excluded_terms ) ) {
1644 $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
1648 // 'post_status' clause depends on the current user.
1649 if ( is_user_logged_in() ) {
1650 $user_id = get_current_user_id();
1652 $post_type_object = get_post_type_object( $post->post_type );
1653 if ( empty( $post_type_object ) ) {
1654 $post_type_cap = $post->post_type;
1655 $read_private_cap = 'read_private_' . $post_type_cap . 's';
1657 $read_private_cap = $post_type_object->cap->read_private_posts;
1661 * Results should include private posts belonging to the current user, or private posts where the
1662 * current user has the 'read_private_posts' cap.
1664 $private_states = get_post_stati( array( 'private' => true ) );
1665 $where .= " AND ( p.post_status = 'publish'";
1666 foreach ( (array) $private_states as $state ) {
1667 if ( current_user_can( $read_private_cap ) ) {
1668 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1670 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1675 $where .= " AND p.post_status = 'publish'";
1678 $op = $previous ? '<' : '>';
1679 $order = $previous ? 'DESC' : 'ASC';
1682 * Filters the JOIN clause in the SQL for an adjacent post query.
1684 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1685 * of adjacency, 'next' or 'previous'.
1688 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1690 * @param string $join The JOIN clause in the SQL.
1691 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1692 * @param array $excluded_terms Array of excluded term IDs.
1693 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1694 * @param WP_Post $post WP_Post object.
1696 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
1699 * Filters the WHERE clause in the SQL for an adjacent post query.
1701 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1702 * of adjacency, 'next' or 'previous'.
1705 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1707 * @param string $where The `WHERE` clause in the SQL.
1708 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1709 * @param array $excluded_terms Array of excluded term IDs.
1710 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1711 * @param WP_Post $post WP_Post object.
1713 $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 );
1716 * Filters the ORDER BY clause in the SQL for an adjacent post query.
1718 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1719 * of adjacency, 'next' or 'previous'.
1722 * @since 4.4.0 Added the `$post` parameter.
1724 * @param string $order_by The `ORDER BY` clause in the SQL.
1725 * @param WP_Post $post WP_Post object.
1727 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post );
1729 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1730 $query_key = 'adjacent_post_' . md5( $query );
1731 $result = wp_cache_get( $query_key, 'counts' );
1732 if ( false !== $result ) {
1734 $result = get_post( $result );
1738 $result = $wpdb->get_var( $query );
1739 if ( null === $result )
1742 wp_cache_set( $query_key, $result, 'counts' );
1745 $result = get_post( $result );
1751 * Retrieves the adjacent post relational link.
1753 * Can either be next or previous post relational link.
1757 * @param string $title Optional. Link title format. Default '%title'.
1758 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1759 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1760 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1761 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1762 * @return string|void The adjacent post relational link URL.
1764 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1765 if ( $previous && is_attachment() && $post = get_post() )
1766 $post = get_post( $post->post_parent );
1768 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1770 if ( empty( $post ) )
1773 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1775 if ( empty( $post_title ) )
1776 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1778 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1780 $title = str_replace( '%title', $post_title, $title );
1781 $title = str_replace( '%date', $date, $title );
1783 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1784 $link .= esc_attr( $title );
1785 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1787 $adjacent = $previous ? 'previous' : 'next';
1790 * Filters the adjacent post relational link.
1792 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1793 * of adjacency, 'next' or 'previous'.
1797 * @param string $link The relational link.
1799 return apply_filters( "{$adjacent}_post_rel_link", $link );
1803 * Displays the relational links for the posts adjacent to the current post.
1807 * @param string $title Optional. Link title format. Default '%title'.
1808 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1809 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1810 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1812 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1813 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1814 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1818 * Displays relational links for the posts adjacent to the current post for single post pages.
1820 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins
1821 * or theme templates.
1825 * @see adjacent_posts_rel_link()
1827 function adjacent_posts_rel_link_wp_head() {
1828 if ( ! is_single() || is_attachment() ) {
1831 adjacent_posts_rel_link();
1835 * Displays the relational link for the next post adjacent to the current post.
1839 * @see get_adjacent_post_rel_link()
1841 * @param string $title Optional. Link title format. Default '%title'.
1842 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1843 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1844 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1846 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1847 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1851 * Displays the relational link for the previous post adjacent to the current post.
1855 * @see get_adjacent_post_rel_link()
1857 * @param string $title Optional. Link title format. Default '%title'.
1858 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1859 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1860 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1862 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1863 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1867 * Retrieves the boundary post.
1869 * Boundary being either the first or last post by publish date within the constraints specified
1870 * by $in_same_term or $excluded_terms.
1874 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1876 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1878 * @param bool $start Optional. Whether to retrieve first or last post. Default true
1879 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1880 * @return null|array Array containing the boundary post object if successful, null otherwise.
1882 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1884 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1887 $query_args = array(
1888 'posts_per_page' => 1,
1889 'order' => $start ? 'ASC' : 'DESC',
1890 'update_post_term_cache' => false,
1891 'update_post_meta_cache' => false
1894 $term_array = array();
1896 if ( ! is_array( $excluded_terms ) ) {
1897 if ( ! empty( $excluded_terms ) )
1898 $excluded_terms = explode( ',', $excluded_terms );
1900 $excluded_terms = array();
1903 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1904 if ( $in_same_term )
1905 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1907 if ( ! empty( $excluded_terms ) ) {
1908 $excluded_terms = array_map( 'intval', $excluded_terms );
1909 $excluded_terms = array_diff( $excluded_terms, $term_array );
1911 $inverse_terms = array();
1912 foreach ( $excluded_terms as $excluded_term )
1913 $inverse_terms[] = $excluded_term * -1;
1914 $excluded_terms = $inverse_terms;
1917 $query_args[ 'tax_query' ] = array( array(
1918 'taxonomy' => $taxonomy,
1919 'terms' => array_merge( $term_array, $excluded_terms )
1923 return get_posts( $query_args );
1927 * Retrieves the previous post link that is adjacent to the current post.
1931 * @param string $format Optional. Link anchor format. Default '« %link'.
1932 * @param string $link Optional. Link permalink format. Default '%title%'.
1933 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1934 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1935 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1936 * @return string The link URL of the previous post in relation to the current post.
1938 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1939 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1943 * Displays the previous post link that is adjacent to the current post.
1947 * @see get_previous_post_link()
1949 * @param string $format Optional. Link anchor format. Default '« %link'.
1950 * @param string $link Optional. Link permalink format. Default '%title'.
1951 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1952 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1953 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1955 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1956 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1960 * Retrieves the next post link that is adjacent to the current post.
1964 * @param string $format Optional. Link anchor format. Default '« %link'.
1965 * @param string $link Optional. Link permalink format. Default '%title'.
1966 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1967 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1968 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1969 * @return string The link URL of the next post in relation to the current post.
1971 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1972 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1976 * Displays the next post link that is adjacent to the current post.
1979 * @see get_next_post_link()
1981 * @param string $format Optional. Link anchor format. Default '« %link'.
1982 * @param string $link Optional. Link permalink format. Default '%title'
1983 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1984 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1985 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1987 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1988 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1992 * Retrieves the adjacent post link.
1994 * Can be either next post link or previous.
1998 * @param string $format Link anchor format.
1999 * @param string $link Link permalink format.
2000 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
2001 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. Default empty.
2002 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
2003 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
2004 * @return string The link URL of the previous or next post in relation to the current post.
2006 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
2007 if ( $previous && is_attachment() )
2008 $post = get_post( get_post()->post_parent );
2010 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
2015 $title = $post->post_title;
2017 if ( empty( $post->post_title ) )
2018 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
2020 /** This filter is documented in wp-includes/post-template.php */
2021 $title = apply_filters( 'the_title', $title, $post->ID );
2023 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
2024 $rel = $previous ? 'prev' : 'next';
2026 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
2027 $inlink = str_replace( '%title', $title, $link );
2028 $inlink = str_replace( '%date', $date, $inlink );
2029 $inlink = $string . $inlink . '</a>';
2031 $output = str_replace( '%link', $inlink, $format );
2034 $adjacent = $previous ? 'previous' : 'next';
2037 * Filters the adjacent post link.
2039 * The dynamic portion of the hook name, `$adjacent`, refers to the type
2040 * of adjacency, 'next' or 'previous'.
2043 * @since 4.2.0 Added the `$adjacent` parameter.
2045 * @param string $output The adjacent post link.
2046 * @param string $format Link anchor format.
2047 * @param string $link Link permalink format.
2048 * @param WP_Post $post The adjacent post.
2049 * @param string $adjacent Whether the post is previous or next.
2051 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
2055 * Displays the adjacent post link.
2057 * Can be either next post link or previous.
2061 * @param string $format Link anchor format.
2062 * @param string $link Link permalink format.
2063 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
2064 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs. Default empty.
2065 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
2066 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
2068 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
2069 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
2073 * Retrieves the link for a page number.
2077 * @global WP_Rewrite $wp_rewrite
2079 * @param int $pagenum Optional. Page ID. Default 1.
2080 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
2081 * Otherwise, prepares the URL with esc_url_raw().
2082 * @return string The link URL for the given page number.
2084 function get_pagenum_link($pagenum = 1, $escape = true ) {
2087 $pagenum = (int) $pagenum;
2089 $request = remove_query_arg( 'paged' );
2091 $home_root = parse_url(home_url());
2092 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
2093 $home_root = preg_quote( $home_root, '|' );
2095 $request = preg_replace('|^'. $home_root . '|i', '', $request);
2096 $request = preg_replace('|^/+|', '', $request);
2098 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
2099 $base = trailingslashit( get_bloginfo( 'url' ) );
2101 if ( $pagenum > 1 ) {
2102 $result = add_query_arg( 'paged', $pagenum, $base . $request );
2104 $result = $base . $request;
2107 $qs_regex = '|\?.*?$|';
2108 preg_match( $qs_regex, $request, $qs_match );
2110 if ( !empty( $qs_match[0] ) ) {
2111 $query_string = $qs_match[0];
2112 $request = preg_replace( $qs_regex, '', $request );
2117 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
2118 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
2119 $request = ltrim($request, '/');
2121 $base = trailingslashit( get_bloginfo( 'url' ) );
2123 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
2124 $base .= $wp_rewrite->index . '/';
2126 if ( $pagenum > 1 ) {
2127 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
2130 $result = $base . $request . $query_string;
2134 * Filters the page number link for the current request.
2138 * @param string $result The page number link.
2140 $result = apply_filters( 'get_pagenum_link', $result );
2143 return esc_url( $result );
2145 return esc_url_raw( $result );
2149 * Retrieves the next posts page link.
2151 * Backported from 2.1.3 to 2.0.10.
2155 * @global int $paged
2157 * @param int $max_page Optional. Max pages. Default 0.
2158 * @return string|void The link URL for next posts page.
2160 function get_next_posts_page_link($max_page = 0) {
2163 if ( !is_single() ) {
2166 $nextpage = intval($paged) + 1;
2167 if ( !$max_page || $max_page >= $nextpage )
2168 return get_pagenum_link($nextpage);
2173 * Displays or retrieves the next posts page link.
2177 * @param int $max_page Optional. Max pages. Default 0.
2178 * @param bool $echo Optional. Whether to echo the link. Default true.
2179 * @return string|void The link URL for next posts page if `$echo = false`.
2181 function next_posts( $max_page = 0, $echo = true ) {
2182 $output = esc_url( get_next_posts_page_link( $max_page ) );
2191 * Retrieves the next posts page link.
2195 * @global int $paged
2196 * @global WP_Query $wp_query
2198 * @param string $label Content for link text.
2199 * @param int $max_page Optional. Max pages. Default 0.
2200 * @return string|void HTML-formatted next posts page link.
2202 function get_next_posts_link( $label = null, $max_page = 0 ) {
2203 global $paged, $wp_query;
2206 $max_page = $wp_query->max_num_pages;
2211 $nextpage = intval($paged) + 1;
2213 if ( null === $label )
2214 $label = __( 'Next Page »' );
2216 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2218 * Filters the anchor tag attributes for the next posts page link.
2222 * @param string $attributes Attributes for the anchor tag.
2224 $attr = apply_filters( 'next_posts_link_attributes', '' );
2226 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2231 * Displays the next posts page link.
2235 * @param string $label Content for link text.
2236 * @param int $max_page Optional. Max pages. Default 0.
2238 function next_posts_link( $label = null, $max_page = 0 ) {
2239 echo get_next_posts_link( $label, $max_page );
2243 * Retrieves the previous posts page link.
2245 * Will only return string, if not on a single page or post.
2247 * Backported to 2.0.10 from 2.1.3.
2251 * @global int $paged
2253 * @return string|void The link for the previous posts page.
2255 function get_previous_posts_page_link() {
2258 if ( !is_single() ) {
2259 $nextpage = intval($paged) - 1;
2260 if ( $nextpage < 1 )
2262 return get_pagenum_link($nextpage);
2267 * Displays or retrieves the previous posts page link.
2271 * @param bool $echo Optional. Whether to echo the link. Default true.
2272 * @return string|void The previous posts page link if `$echo = false`.
2274 function previous_posts( $echo = true ) {
2275 $output = esc_url( get_previous_posts_page_link() );
2284 * Retrieves the previous posts page link.
2288 * @global int $paged
2290 * @param string $label Optional. Previous page link text.
2291 * @return string|void HTML-formatted previous page link.
2293 function get_previous_posts_link( $label = null ) {
2296 if ( null === $label )
2297 $label = __( '« Previous Page' );
2299 if ( !is_single() && $paged > 1 ) {
2301 * Filters the anchor tag attributes for the previous posts page link.
2305 * @param string $attributes Attributes for the anchor tag.
2307 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2308 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2313 * Displays the previous posts page link.
2317 * @param string $label Optional. Previous page link text.
2319 function previous_posts_link( $label = null ) {
2320 echo get_previous_posts_link( $label );
2324 * Retrieves the post pages link navigation for previous and next pages.
2328 * @global WP_Query $wp_query
2330 * @param string|array $args {
2331 * Optional. Arguments to build the post pages link navigation.
2333 * @type string $sep Separator character. Default '—'.
2334 * @type string $prelabel Link text to display for the previous page link.
2335 * Default '« Previous Page'.
2336 * @type string $nxtlabel Link text to display for the next page link.
2337 * Default 'Next Page »'.
2339 * @return string The posts link navigation.
2341 function get_posts_nav_link( $args = array() ) {
2346 if ( !is_singular() ) {
2348 'sep' => ' — ',
2349 'prelabel' => __('« Previous Page'),
2350 'nxtlabel' => __('Next Page »'),
2352 $args = wp_parse_args( $args, $defaults );
2354 $max_num_pages = $wp_query->max_num_pages;
2355 $paged = get_query_var('paged');
2357 //only have sep if there's both prev and next results
2358 if ($paged < 2 || $paged >= $max_num_pages) {
2362 if ( $max_num_pages > 1 ) {
2363 $return = get_previous_posts_link($args['prelabel']);
2364 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2365 $return .= get_next_posts_link($args['nxtlabel']);
2373 * Displays the post pages link navigation for previous and next pages.
2377 * @param string $sep Optional. Separator for posts navigation links. Default empty.
2378 * @param string $prelabel Optional. Label for previous pages. Default empty.
2379 * @param string $nxtlabel Optional Label for next pages. Default empty.
2381 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2382 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2383 echo get_posts_nav_link($args);
2387 * Retrieves the navigation to next/previous post, when applicable.
2390 * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments.
2392 * @param array $args {
2393 * Optional. Default post navigation arguments. Default empty array.
2395 * @type string $prev_text Anchor text to display in the previous post link. Default '%title'.
2396 * @type string $next_text Anchor text to display in the next post link. Default '%title'.
2397 * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false.
2398 * @type array|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty.
2399 * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'.
2400 * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2402 * @return string Markup for post links.
2404 function get_the_post_navigation( $args = array() ) {
2405 $args = wp_parse_args( $args, array(
2406 'prev_text' => '%title',
2407 'next_text' => '%title',
2408 'in_same_term' => false,
2409 'excluded_terms' => '',
2410 'taxonomy' => 'category',
2411 'screen_reader_text' => __( 'Post navigation' ),
2416 $previous = get_previous_post_link(
2417 '<div class="nav-previous">%link</div>',
2419 $args['in_same_term'],
2420 $args['excluded_terms'],
2424 $next = get_next_post_link(
2425 '<div class="nav-next">%link</div>',
2427 $args['in_same_term'],
2428 $args['excluded_terms'],
2432 // Only add markup if there's somewhere to navigate to.
2433 if ( $previous || $next ) {
2434 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2441 * Displays the navigation to next/previous post, when applicable.
2445 * @param array $args Optional. See get_the_post_navigation() for available arguments.
2446 * Default empty array.
2448 function the_post_navigation( $args = array() ) {
2449 echo get_the_post_navigation( $args );
2453 * Returns the navigation to next/previous set of posts, when applicable.
2457 * @global WP_Query $wp_query WordPress Query object.
2459 * @param array $args {
2460 * Optional. Default posts navigation arguments. Default empty array.
2462 * @type string $prev_text Anchor text to display in the previous posts link.
2463 * Default 'Older posts'.
2464 * @type string $next_text Anchor text to display in the next posts link.
2465 * Default 'Newer posts'.
2466 * @type string $screen_reader_text Screen reader text for nav element.
2467 * Default 'Posts navigation'.
2469 * @return string Markup for posts links.
2471 function get_the_posts_navigation( $args = array() ) {
2474 // Don't print empty markup if there's only one page.
2475 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2476 $args = wp_parse_args( $args, array(
2477 'prev_text' => __( 'Older posts' ),
2478 'next_text' => __( 'Newer posts' ),
2479 'screen_reader_text' => __( 'Posts navigation' ),
2482 $next_link = get_previous_posts_link( $args['next_text'] );
2483 $prev_link = get_next_posts_link( $args['prev_text'] );
2486 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2490 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2493 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2500 * Displays the navigation to next/previous set of posts, when applicable.
2504 * @param array $args Optional. See get_the_posts_navigation() for available arguments.
2505 * Default empty array.
2507 function the_posts_navigation( $args = array() ) {
2508 echo get_the_posts_navigation( $args );
2512 * Retrieves a paginated navigation to next/previous set of posts, when applicable.
2516 * @param array $args {
2517 * Optional. Default pagination arguments, see paginate_links().
2519 * @type string $screen_reader_text Screen reader text for navigation element.
2520 * Default 'Posts navigation'.
2522 * @return string Markup for pagination links.
2524 function get_the_posts_pagination( $args = array() ) {
2527 // Don't print empty markup if there's only one page.
2528 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2529 $args = wp_parse_args( $args, array(
2531 'prev_text' => _x( 'Previous', 'previous post' ),
2532 'next_text' => _x( 'Next', 'next post' ),
2533 'screen_reader_text' => __( 'Posts navigation' ),
2536 // Make sure we get a string back. Plain is the next best thing.
2537 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2538 $args['type'] = 'plain';
2541 // Set up paginated links.
2542 $links = paginate_links( $args );
2545 $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2553 * Displays a paginated navigation to next/previous set of posts, when applicable.
2557 * @param array $args Optional. See get_the_posts_pagination() for available arguments.
2558 * Default empty array.
2560 function the_posts_pagination( $args = array() ) {
2561 echo get_the_posts_pagination( $args );
2565 * Wraps passed links in navigational markup.
2570 * @param string $links Navigational links.
2571 * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'.
2572 * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2573 * @return string Navigation template tag.
2575 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2576 if ( empty( $screen_reader_text ) ) {
2577 $screen_reader_text = __( 'Posts navigation' );
2581 <nav class="navigation %1$s" role="navigation">
2582 <h2 class="screen-reader-text">%2$s</h2>
2583 <div class="nav-links">%3$s</div>
2587 * Filters the navigation markup template.
2589 * Note: The filtered template HTML must contain specifiers for the navigation
2590 * class (%1$s), the screen-reader-text value (%2$s), and placement of the
2591 * navigation links (%3$s):
2593 * <nav class="navigation %1$s" role="navigation">
2594 * <h2 class="screen-reader-text">%2$s</h2>
2595 * <div class="nav-links">%3$s</div>
2600 * @param string $template The default template.
2601 * @param string $class The class passed by the calling function.
2602 * @return string Navigation template.
2604 $template = apply_filters( 'navigation_markup_template', $template, $class );
2606 return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2610 * Retrieves the comments page number link.
2614 * @global WP_Rewrite $wp_rewrite
2616 * @param int $pagenum Optional. Page number. Default 1.
2617 * @param int $max_page Optional. The maximum number of comment pages. Default 0.
2618 * @return string The comments page number link URL.
2620 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2623 $pagenum = (int) $pagenum;
2625 $result = get_permalink();
2627 if ( 'newest' == get_option('default_comments_page') ) {
2628 if ( $pagenum != $max_page ) {
2629 if ( $wp_rewrite->using_permalinks() )
2630 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2632 $result = add_query_arg( 'cpage', $pagenum, $result );
2634 } elseif ( $pagenum > 1 ) {
2635 if ( $wp_rewrite->using_permalinks() )
2636 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2638 $result = add_query_arg( 'cpage', $pagenum, $result );
2641 $result .= '#comments';
2644 * Filters the comments page number link for the current request.
2648 * @param string $result The comments page number link.
2650 return apply_filters( 'get_comments_pagenum_link', $result );
2654 * Retrieves the link to the next comments page.
2658 * @global WP_Query $wp_query
2660 * @param string $label Optional. Label for link text. Default empty.
2661 * @param int $max_page Optional. Max page. Default 0.
2662 * @return string|void HTML-formatted link for the next page of comments.
2664 function get_next_comments_link( $label = '', $max_page = 0 ) {
2667 if ( ! is_singular() )
2670 $page = get_query_var('cpage');
2676 $nextpage = intval($page) + 1;
2678 if ( empty($max_page) )
2679 $max_page = $wp_query->max_num_comment_pages;
2681 if ( empty($max_page) )
2682 $max_page = get_comment_pages_count();
2684 if ( $nextpage > $max_page )
2687 if ( empty($label) )
2688 $label = __('Newer Comments »');
2691 * Filters the anchor tag attributes for the next comments page link.
2695 * @param string $attributes Attributes for the anchor tag.
2697 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>';
2701 * Displays the link to the next comments page.
2705 * @param string $label Optional. Label for link text. Default empty.
2706 * @param int $max_page Optional. Max page. Default 0.
2708 function next_comments_link( $label = '', $max_page = 0 ) {
2709 echo get_next_comments_link( $label, $max_page );
2713 * Retrieves the link to the previous comments page.
2717 * @param string $label Optional. Label for comments link text. Default empty.
2718 * @return string|void HTML-formatted link for the previous page of comments.
2720 function get_previous_comments_link( $label = '' ) {
2721 if ( ! is_singular() )
2724 $page = get_query_var('cpage');
2726 if ( intval($page) <= 1 )
2729 $prevpage = intval($page) - 1;
2731 if ( empty($label) )
2732 $label = __('« Older Comments');
2735 * Filters the anchor tag attributes for the previous comments page link.
2739 * @param string $attributes Attributes for the anchor tag.
2741 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>';
2745 * Displays the link to the previous comments page.
2749 * @param string $label Optional. Label for comments link text. Default empty.
2751 function previous_comments_link( $label = '' ) {
2752 echo get_previous_comments_link( $label );
2756 * Displays or retrieves pagination links for the comments on the current post.
2758 * @see paginate_links()
2761 * @global WP_Rewrite $wp_rewrite
2763 * @param string|array $args Optional args. See paginate_links(). Default empty array.
2764 * @return string|void Markup for pagination links.
2766 function paginate_comments_links( $args = array() ) {
2769 if ( ! is_singular() )
2772 $page = get_query_var('cpage');
2775 $max_page = get_comment_pages_count();
2777 'base' => add_query_arg( 'cpage', '%#%' ),
2779 'total' => $max_page,
2782 'add_fragment' => '#comments'
2784 if ( $wp_rewrite->using_permalinks() )
2785 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2787 $args = wp_parse_args( $args, $defaults );
2788 $page_links = paginate_links( $args );
2790 if ( $args['echo'] )
2797 * Retrieves navigation to next/previous set of comments, when applicable.
2801 * @param array $args {
2802 * Optional. Default comments navigation arguments.
2804 * @type string $prev_text Anchor text to display in the previous comments link.
2805 * Default 'Older comments'.
2806 * @type string $next_text Anchor text to display in the next comments link.
2807 * Default 'Newer comments'.
2808 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2810 * @return string Markup for comments links.
2812 function get_the_comments_navigation( $args = array() ) {
2815 // Are there comments to navigate through?
2816 if ( get_comment_pages_count() > 1 ) {
2817 $args = wp_parse_args( $args, array(
2818 'prev_text' => __( 'Older comments' ),
2819 'next_text' => __( 'Newer comments' ),
2820 'screen_reader_text' => __( 'Comments navigation' ),
2823 $prev_link = get_previous_comments_link( $args['prev_text'] );
2824 $next_link = get_next_comments_link( $args['next_text'] );
2827 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2831 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2834 $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] );
2841 * Displays navigation to next/previous set of comments, when applicable.
2845 * @param array $args See get_the_comments_navigation() for available arguments. Default empty array.
2847 function the_comments_navigation( $args = array() ) {
2848 echo get_the_comments_navigation( $args );
2852 * Retrieves a paginated navigation to next/previous set of comments, when applicable.
2856 * @see paginate_comments_links()
2858 * @param array $args {
2859 * Optional. Default pagination arguments.
2861 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2863 * @return string Markup for pagination links.
2865 function get_the_comments_pagination( $args = array() ) {
2867 $args = wp_parse_args( $args, array(
2868 'screen_reader_text' => __( 'Comments navigation' ),
2870 $args['echo'] = false;
2872 // Make sure we get plain links, so we get a string we can work with.
2873 $args['type'] = 'plain';
2875 $links = paginate_comments_links( $args );
2878 $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
2885 * Displays a paginated navigation to next/previous set of comments, when applicable.
2889 * @param array $args See get_the_comments_pagination() for available arguments. Default empty array.
2891 function the_comments_pagination( $args = array() ) {
2892 echo get_the_comments_pagination( $args );
2896 * Retrieves the Press This bookmarklet link.
2900 * @global bool $is_IE Whether the browser matches an Internet Explorer user agent.
2901 * @global string $wp_version WP version.
2903 * @global bool $is_IE
2904 * @global string $wp_version
2905 * @global WP_Press_This $wp_press_this
2907 * @return string The Press This bookmarklet link URL.
2909 function get_shortcut_link() {
2910 global $is_IE, $wp_version;
2912 include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2913 $bookmarklet_version = $GLOBALS['wp_press_this']->version;
2918 * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2919 * since they only support a max length of ~2000 characters for
2920 * bookmark[let] URLs, which is way to small for our smarter one.
2921 * Do update the version number so users do not get the "upgrade your
2922 * bookmarklet" notice when using PT in those browsers.
2924 $ua = $_SERVER['HTTP_USER_AGENT'];
2926 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2927 $url = wp_json_encode( admin_url( 'press-this.php' ) );
2929 $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2930 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2931 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' .
2932 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2933 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2937 if ( empty( $link ) ) {
2938 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2941 $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version );
2942 $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2946 $link = str_replace( array( "\r", "\n", "\t" ), '', $link );
2949 * Filters the Press This bookmarklet link.
2953 * @param string $link The Press This bookmarklet link.
2955 return apply_filters( 'shortcut_link', $link );
2959 * Retrieves the URL for the current site where the front end is accessible.
2961 * Returns the 'home' option with the appropriate protocol, 'https' if
2962 * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',
2963 * `is_ssl()` is overridden.
2967 * @param string $path Optional. Path relative to the home URL. Default empty.
2968 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
2969 * 'http', 'https', 'relative', 'rest', or null. Default null.
2970 * @return string Home URL link with optional path appended.
2972 function home_url( $path = '', $scheme = null ) {
2973 return get_home_url( null, $path, $scheme );
2977 * Retrieves the URL for a given site where the front end is accessible.
2979 * Returns the 'home' option with the appropriate protocol, 'https' if
2980 * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',
2981 * `is_ssl()` is overridden.
2985 * @global string $pagenow
2987 * @param int $blog_id Optional. Site ID. Default null (current site).
2988 * @param string $path Optional. Path relative to the home URL. Default empty.
2989 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
2990 * 'http', 'https', 'relative', 'rest', or null. Default null.
2991 * @return string Home URL link with optional path appended.
2993 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2996 $orig_scheme = $scheme;
2998 if ( empty( $blog_id ) || !is_multisite() ) {
2999 $url = get_option( 'home' );
3001 switch_to_blog( $blog_id );
3002 $url = get_option( 'home' );
3003 restore_current_blog();
3006 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
3007 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow )
3010 $scheme = parse_url( $url, PHP_URL_SCHEME );
3013 $url = set_url_scheme( $url, $scheme );
3015 if ( $path && is_string( $path ) )
3016 $url .= '/' . ltrim( $path, '/' );
3019 * Filters the home URL.
3023 * @param string $url The complete home URL including scheme and path.
3024 * @param string $path Path relative to the home URL. Blank string if no path is specified.
3025 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
3026 * 'relative', 'rest', or null.
3027 * @param int|null $blog_id Site ID, or null for the current site.
3029 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
3033 * Retrieves the URL for the current site where WordPress application files
3034 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3036 * Returns the 'site_url' option with the appropriate protocol, 'https' if
3037 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3042 * @param string $path Optional. Path relative to the site URL. Default empty.
3043 * @param string $scheme Optional. Scheme to give the site URL context. See set_url_scheme().
3044 * @return string Site URL link with optional path appended.
3046 function site_url( $path = '', $scheme = null ) {
3047 return get_site_url( null, $path, $scheme );
3051 * Retrieves the URL for a given site where WordPress application files
3052 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3054 * Returns the 'site_url' option with the appropriate protocol, 'https' if
3055 * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',
3056 * `is_ssl()` is overridden.
3060 * @param int $blog_id Optional. Site ID. Default null (current site).
3061 * @param string $path Optional. Path relative to the site URL. Default empty.
3062 * @param string $scheme Optional. Scheme to give the site URL context. Accepts
3063 * 'http', 'https', 'login', 'login_post', 'admin', or
3064 * 'relative'. Default null.
3065 * @return string Site URL link with optional path appended.
3067 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
3068 if ( empty( $blog_id ) || !is_multisite() ) {
3069 $url = get_option( 'siteurl' );
3071 switch_to_blog( $blog_id );
3072 $url = get_option( 'siteurl' );
3073 restore_current_blog();
3076 $url = set_url_scheme( $url, $scheme );
3078 if ( $path && is_string( $path ) )
3079 $url .= '/' . ltrim( $path, '/' );
3082 * Filters the site URL.
3086 * @param string $url The complete site URL including scheme and path.
3087 * @param string $path Path relative to the site URL. Blank string if no path is specified.
3088 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
3089 * 'login_post', 'admin', 'relative' or null.
3090 * @param int|null $blog_id Site ID, or null for the current site.
3092 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
3096 * Retrieves the URL to the admin area for the current site.
3100 * @param string $path Optional path relative to the admin URL.
3101 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3102 * 'http' or 'https' can be passed to force those schemes.
3103 * @return string Admin URL link with optional path appended.
3105 function admin_url( $path = '', $scheme = 'admin' ) {
3106 return get_admin_url( null, $path, $scheme );
3110 * Retrieves the URL to the admin area for a given site.
3114 * @param int $blog_id Optional. Site ID. Default null (current site).
3115 * @param string $path Optional. Path relative to the admin URL. Default empty.
3116 * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
3117 * to force those schemes. Default 'admin', which obeys
3118 * force_ssl_admin() and is_ssl().
3119 * @return string Admin URL link with optional path appended.
3121 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
3122 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
3124 if ( $path && is_string( $path ) )
3125 $url .= ltrim( $path, '/' );
3128 * Filters the admin area URL.
3132 * @param string $url The complete admin area URL including scheme and path.
3133 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
3134 * @param int|null $blog_id Site ID, or null for the current site.
3136 return apply_filters( 'admin_url', $url, $path, $blog_id );
3140 * Retrieves the URL to the includes directory.
3144 * @param string $path Optional. Path relative to the includes URL. Default empty.
3145 * @param string $scheme Optional. Scheme to give the includes URL context. Accepts
3146 * 'http', 'https', or 'relative'. Default null.
3147 * @return string Includes URL link with optional path appended.
3149 function includes_url( $path = '', $scheme = null ) {
3150 $url = site_url( '/' . WPINC . '/', $scheme );
3152 if ( $path && is_string( $path ) )
3153 $url .= ltrim($path, '/');
3156 * Filters the URL to the includes directory.
3160 * @param string $url The complete URL to the includes directory including scheme and path.
3161 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
3162 * if no path is specified.
3164 return apply_filters( 'includes_url', $url, $path );
3168 * Retrieves the URL to the content directory.
3172 * @param string $path Optional. Path relative to the content URL. Default empty.
3173 * @return string Content URL link with optional path appended.
3175 function content_url( $path = '' ) {
3176 $url = set_url_scheme( WP_CONTENT_URL );
3178 if ( $path && is_string( $path ) )
3179 $url .= '/' . ltrim($path, '/');
3182 * Filters the URL to the content directory.
3186 * @param string $url The complete URL to the content directory including scheme and path.
3187 * @param string $path Path relative to the URL to the content directory. Blank string
3188 * if no path is specified.
3190 return apply_filters( 'content_url', $url, $path);
3194 * Retrieves a URL within the plugins or mu-plugins directory.
3196 * Defaults to the plugins directory URL if no arguments are supplied.
3200 * @param string $path Optional. Extra path appended to the end of the URL, including
3201 * the relative directory if $plugin is supplied. Default empty.
3202 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
3203 * The URL will be relative to its directory. Default empty.
3204 * Typically this is done by passing `__FILE__` as the argument.
3205 * @return string Plugins URL link with optional paths appended.
3207 function plugins_url( $path = '', $plugin = '' ) {
3209 $path = wp_normalize_path( $path );
3210 $plugin = wp_normalize_path( $plugin );
3211 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
3213 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
3214 $url = WPMU_PLUGIN_URL;
3216 $url = WP_PLUGIN_URL;
3219 $url = set_url_scheme( $url );
3221 if ( !empty($plugin) && is_string($plugin) ) {
3222 $folder = dirname(plugin_basename($plugin));
3223 if ( '.' != $folder )
3224 $url .= '/' . ltrim($folder, '/');
3227 if ( $path && is_string( $path ) )
3228 $url .= '/' . ltrim($path, '/');
3231 * Filters the URL to the plugins directory.
3235 * @param string $url The complete URL to the plugins directory including scheme and path.
3236 * @param string $path Path relative to the URL to the plugins directory. Blank string
3237 * if no path is specified.
3238 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
3241 return apply_filters( 'plugins_url', $url, $path, $plugin );
3245 * Retrieves the site URL for the current network.
3247 * Returns the site URL with the appropriate protocol, 'https' if
3248 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3253 * @see set_url_scheme()
3255 * @param string $path Optional. Path relative to the site URL. Default empty.
3256 * @param string $scheme Optional. Scheme to give the site URL context. Accepts
3257 * 'http', 'https', or 'relative'. Default null.
3258 * @return string Site URL link with optional path appended.
3260 function network_site_url( $path = '', $scheme = null ) {
3261 if ( ! is_multisite() )
3262 return site_url($path, $scheme);
3264 $current_site = get_current_site();
3266 if ( 'relative' == $scheme )
3267 $url = $current_site->path;
3269 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3271 if ( $path && is_string( $path ) )
3272 $url .= ltrim( $path, '/' );
3275 * Filters the network site URL.
3279 * @param string $url The complete network site URL including scheme and path.
3280 * @param string $path Path relative to the network site URL. Blank string if
3281 * no path is specified.
3282 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3283 * 'relative' or null.
3285 return apply_filters( 'network_site_url', $url, $path, $scheme );
3289 * Retrieves the home URL for the current network.
3291 * Returns the home URL with the appropriate protocol, 'https' is_ssl()
3292 * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3297 * @param string $path Optional. Path relative to the home URL. Default empty.
3298 * @param string $scheme Optional. Scheme to give the home URL context. Accepts
3299 * 'http', 'https', or 'relative'. Default null.
3300 * @return string Home URL link with optional path appended.
3302 function network_home_url( $path = '', $scheme = null ) {
3303 if ( ! is_multisite() )
3304 return home_url($path, $scheme);
3306 $current_site = get_current_site();
3307 $orig_scheme = $scheme;
3309 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
3310 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3312 if ( 'relative' == $scheme )
3313 $url = $current_site->path;
3315 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3317 if ( $path && is_string( $path ) )
3318 $url .= ltrim( $path, '/' );
3321 * Filters the network home URL.
3325 * @param string $url The complete network home URL including scheme and path.
3326 * @param string $path Path relative to the network home URL. Blank string
3327 * if no path is specified.
3328 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3329 * 'relative' or null.
3331 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3335 * Retrieves the URL to the admin area for the network.
3339 * @param string $path Optional path relative to the admin URL. Default empty.
3340 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3341 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3342 * @return string Admin URL link with optional path appended.
3344 function network_admin_url( $path = '', $scheme = 'admin' ) {
3345 if ( ! is_multisite() )
3346 return admin_url( $path, $scheme );
3348 $url = network_site_url('wp-admin/network/', $scheme);
3350 if ( $path && is_string( $path ) )
3351 $url .= ltrim($path, '/');
3354 * Filters the network admin URL.
3358 * @param string $url The complete network admin URL including scheme and path.
3359 * @param string $path Path relative to the network admin URL. Blank string if
3360 * no path is specified.
3362 return apply_filters( 'network_admin_url', $url, $path );
3366 * Retrieves the URL to the admin area for the current user.
3370 * @param string $path Optional. Path relative to the admin URL. Default empty.
3371 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3372 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3373 * @return string Admin URL link with optional path appended.
3375 function user_admin_url( $path = '', $scheme = 'admin' ) {
3376 $url = network_site_url('wp-admin/user/', $scheme);
3378 if ( $path && is_string( $path ) )
3379 $url .= ltrim($path, '/');
3382 * Filters the user admin URL for the current user.
3386 * @param string $url The complete URL including scheme and path.
3387 * @param string $path Path relative to the URL. Blank string if
3388 * no path is specified.
3390 return apply_filters( 'user_admin_url', $url, $path );
3394 * Retrieves the URL to the admin area for either the current site or the network depending on context.
3398 * @param string $path Optional. Path relative to the admin URL. Default empty.
3399 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3400 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3401 * @return string Admin URL link with optional path appended.
3403 function self_admin_url( $path = '', $scheme = 'admin' ) {
3404 if ( is_network_admin() )
3405 return network_admin_url($path, $scheme);
3406 elseif ( is_user_admin() )
3407 return user_admin_url($path, $scheme);
3409 return admin_url($path, $scheme);
3413 * Sets the scheme for a URL.
3416 * @since 4.4.0 The 'rest' scheme was added.
3418 * @param string $url Absolute URL that includes a scheme
3419 * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
3420 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
3421 * @return string $url URL with chosen scheme.
3423 function set_url_scheme( $url, $scheme = null ) {
3424 $orig_scheme = $scheme;
3427 $scheme = is_ssl() ? 'https' : 'http';
3428 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3429 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3430 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3431 $scheme = is_ssl() ? 'https' : 'http';
3434 $url = trim( $url );
3435 if ( substr( $url, 0, 2 ) === '//' )
3436 $url = 'http:' . $url;
3438 if ( 'relative' == $scheme ) {
3439 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3440 if ( $url !== '' && $url[0] === '/' )
3441 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3443 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3447 * Filters the resulting URL after setting the scheme.
3451 * @param string $url The complete URL including scheme and path.
3452 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3453 * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3454 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
3456 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3460 * Retrieves the URL to the user's dashboard.
3462 * If a user does not belong to any site, the global user dashboard is used. If the user
3463 * belongs to the current site, the dashboard for the current site is returned. If the user
3464 * cannot edit the current site, the dashboard to the user's primary site is returned.
3468 * @param int $user_id Optional. User ID. Defaults to current user.
3469 * @param string $path Optional path relative to the dashboard. Use only paths known to
3470 * both site and user admins. Default empty.
3471 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3472 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3473 * @return string Dashboard URL link with optional path appended.
3475 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3476 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3478 $blogs = get_blogs_of_user( $user_id );
3479 if ( ! is_super_admin() && empty($blogs) ) {
3480 $url = user_admin_url( $path, $scheme );
3481 } elseif ( ! is_multisite() ) {
3482 $url = admin_url( $path, $scheme );
3484 $current_blog = get_current_blog_id();
3485 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3486 $url = admin_url( $path, $scheme );
3488 $active = get_active_blog_for_user( $user_id );
3490 $url = get_admin_url( $active->blog_id, $path, $scheme );
3492 $url = user_admin_url( $path, $scheme );
3497 * Filters the dashboard URL for a user.
3501 * @param string $url The complete URL including scheme and path.
3502 * @param int $user_id The user ID.
3503 * @param string $path Path relative to the URL. Blank string if no path is specified.
3504 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3505 * 'login_post', 'admin', 'relative' or null.
3507 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3511 * Retrieves the URL to the user's profile editor.
3515 * @param int $user_id Optional. User ID. Defaults to current user.
3516 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3517 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3518 * @return string Dashboard URL link with optional path appended.
3520 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3521 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3523 if ( is_user_admin() )
3524 $url = user_admin_url( 'profile.php', $scheme );
3525 elseif ( is_network_admin() )
3526 $url = network_admin_url( 'profile.php', $scheme );
3528 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3531 * Filters the URL for a user's profile editor.
3535 * @param string $url The complete URL including scheme and path.
3536 * @param int $user_id The user ID.
3537 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3538 * 'login_post', 'admin', 'relative' or null.
3540 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3544 * Returns the canonical URL for a post.
3546 * When the post is the same as the current requested page the function will handle the
3547 * pagination arguments too.
3551 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
3552 * @return string|false The canonical URL, or false if the post does not exist or has not
3553 * been published yet.
3555 function wp_get_canonical_url( $post = null ) {
3556 $post = get_post( $post );
3562 if ( 'publish' !== $post->post_status ) {
3566 $canonical_url = get_permalink( $post );
3568 // If a canonical is being generated for the current page, make sure it has pagination if needed.
3569 if ( $post->ID === get_queried_object_id() ) {
3570 $page = get_query_var( 'page', 0 );
3572 if ( '' == get_option( 'permalink_structure' ) ) {
3573 $canonical_url = add_query_arg( 'page', $page, $canonical_url );
3575 $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
3579 $cpage = get_query_var( 'cpage', 0 );
3581 $canonical_url = get_comments_pagenum_link( $cpage );
3586 * Filters the canonical URL for a post.
3590 * @param string $string The post's canonical URL.
3591 * @param WP_Post $post Post object.
3593 return apply_filters( 'get_canonical_url', $canonical_url, $post );
3597 * Outputs rel=canonical for singular queries.
3600 * @since 4.6.0 Adjusted to use wp_get_canonical_url().
3602 function rel_canonical() {
3603 if ( ! is_singular() ) {
3607 $id = get_queried_object_id();
3613 $url = wp_get_canonical_url( $id );
3615 if ( ! empty( $url ) ) {
3616 echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
3621 * Returns a shortlink for a post, page, attachment, or site.
3623 * This function exists to provide a shortlink tag that all themes and plugins can target.
3624 * A plugin must hook in to provide the actual shortlinks. Default shortlink support is
3625 * limited to providing ?p= style links for posts. Plugins can short-circuit this function
3626 * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'}
3631 * @param int $id Optional. A post or site id. Default is 0, which means the current post or site.
3632 * @param string $context Optional. Whether the id is a 'site' id, 'post' id, or 'media' id. If 'post',
3633 * the post_type of the post is consulted. If 'query', the current query is consulted
3634 * to determine the id and context. Default 'post'.
3635 * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how
3636 * and whether to honor this. Default true.
3637 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks
3640 function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
3642 * Filters whether to preempt generating a shortlink for the given post.
3644 * Passing a truthy value to the filter will effectively short-circuit the
3645 * shortlink-generation process, returning that value instead.
3649 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3650 * @param int $id Post ID, or 0 for the current post.
3651 * @param string $context The context for the link. One of 'post' or 'query',
3652 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3654 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3656 if ( false !== $shortlink ) {
3661 if ( 'query' == $context && is_singular() ) {
3662 $post_id = get_queried_object_id();
3663 $post = get_post( $post_id );
3664 } elseif ( 'post' == $context ) {
3665 $post = get_post( $id );
3666 if ( ! empty( $post->ID ) )
3667 $post_id = $post->ID;
3672 // Return p= link for all public post types.
3673 if ( ! empty( $post_id ) ) {
3674 $post_type = get_post_type_object( $post->post_type );
3676 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3677 $shortlink = home_url( '/' );
3678 } elseif ( $post_type->public ) {
3679 $shortlink = home_url( '?p=' . $post_id );
3684 * Filters the shortlink for a post.
3688 * @param string $shortlink Shortlink URL.
3689 * @param int $id Post ID, or 0 for the current post.
3690 * @param string $context The context for the link. One of 'post' or 'query',
3691 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3693 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3697 * Injects rel=shortlink into the head if a shortlink is defined for the current page.
3699 * Attached to the {@see 'wp_head'} action.
3703 function wp_shortlink_wp_head() {
3704 $shortlink = wp_get_shortlink( 0, 'query' );
3706 if ( empty( $shortlink ) )
3709 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3713 * Sends a Link: rel=shortlink header if a shortlink is defined for the current page.
3715 * Attached to the {@see 'wp'} action.
3719 function wp_shortlink_header() {
3720 if ( headers_sent() )
3723 $shortlink = wp_get_shortlink(0, 'query');
3725 if ( empty($shortlink) )
3728 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3732 * Displays the shortlink for a post.
3734 * Must be called from inside "The Loop"
3736 * Call like the_shortlink( __( 'Shortlinkage FTW' ) )
3740 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3741 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3742 * @param string $before Optional HTML to display before the link. Default empty.
3743 * @param string $after Optional HTML to display after the link. Default empty.
3745 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3748 if ( empty( $text ) )
3749 $text = __('This is the short link.');
3751 if ( empty( $title ) )
3752 $title = the_title_attribute( array( 'echo' => false ) );
3754 $shortlink = wp_get_shortlink( $post->ID );
3756 if ( !empty( $shortlink ) ) {
3757 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3760 * Filters the short link anchor tag for a post.
3764 * @param string $link Shortlink anchor tag.
3765 * @param string $shortlink Shortlink URL.
3766 * @param string $text Shortlink's text.
3767 * @param string $title Shortlink's title attribute.
3769 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3770 echo $before, $link, $after;
3776 * Retrieves the avatar URL.
3780 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3781 * user email, WP_User object, WP_Post object, or WP_Comment object.
3782 * @param array $args {
3783 * Optional. Arguments to return instead of the default arguments.
3785 * @type int $size Height and width of the avatar in pixels. Default 96.
3786 * @type string $default URL for the default image or a default type. Accepts '404' (return
3787 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3788 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3789 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3790 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3791 * 'avatar_default' option, with a fallback of 'mystery'.
3792 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3793 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3794 * judged in that order. Default is the value of the 'avatar_rating' option.
3795 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3797 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3798 * plus a "found_avatar" guess. Pass as a reference. Default null.
3800 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3802 function get_avatar_url( $id_or_email, $args = null ) {
3803 $args = get_avatar_data( $id_or_email, $args );
3804 return $args['url'];
3808 * Retrieves default data about the avatar.
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.
3814 * @param array $args {
3815 * Optional. Arguments to return instead of the default arguments.
3817 * @type int $size Height and width of the avatar image file in pixels. Default 96.
3818 * @type int $height Display height of the avatar in pixels. Defaults to $size.
3819 * @type int $width Display width of the avatar in pixels. Defaults to $size.
3820 * @type string $default URL for the default image or a default type. Accepts '404' (return
3821 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3822 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3823 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3824 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3825 * 'avatar_default' option, with a fallback of 'mystery'.
3826 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3827 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3828 * judged in that order. Default is the value of the 'avatar_rating' option.
3829 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3831 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3832 * plus a "found_avatar" guess. Pass as a reference. Default null.
3833 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3835 * @return array $processed_args {
3836 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3838 * @type bool $found_avatar True if we were able to find an avatar for this user,
3839 * false or not set if we couldn't.
3840 * @type string $url The URL of the avatar we found.
3843 function get_avatar_data( $id_or_email, $args = null ) {
3844 $args = wp_parse_args( $args, array(
3848 'default' => get_option( 'avatar_default', 'mystery' ),
3849 'force_default' => false,
3850 'rating' => get_option( 'avatar_rating' ),
3852 'processed_args' => null, // if used, should be a reference
3856 if ( is_numeric( $args['size'] ) ) {
3857 $args['size'] = absint( $args['size'] );
3858 if ( ! $args['size'] ) {
3865 if ( is_numeric( $args['height'] ) ) {
3866 $args['height'] = absint( $args['height'] );
3867 if ( ! $args['height'] ) {
3868 $args['height'] = $args['size'];
3871 $args['height'] = $args['size'];
3874 if ( is_numeric( $args['width'] ) ) {
3875 $args['width'] = absint( $args['width'] );
3876 if ( ! $args['width'] ) {
3877 $args['width'] = $args['size'];
3880 $args['width'] = $args['size'];
3883 if ( empty( $args['default'] ) ) {
3884 $args['default'] = get_option( 'avatar_default', 'mystery' );
3887 switch ( $args['default'] ) {
3891 $args['default'] = 'mm';
3893 case 'gravatar_default' :
3894 $args['default'] = false;
3898 $args['force_default'] = (bool) $args['force_default'];
3900 $args['rating'] = strtolower( $args['rating'] );
3902 $args['found_avatar'] = false;
3905 * Filters whether to retrieve the avatar URL early.
3907 * Passing a non-null value in the 'url' member of the return array will
3908 * effectively short circuit get_avatar_data(), passing the value through
3909 * the {@see 'get_avatar_data'} filter and returning early.
3913 * @param array $args Arguments passed to get_avatar_data(), after processing.
3914 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3915 * user email, WP_User object, WP_Post object, or WP_Comment object.
3917 $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3919 if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3920 /** This filter is documented in wp-includes/link-template.php */
3921 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3925 $user = $email = false;
3927 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3928 $id_or_email = get_comment( $id_or_email );
3931 // Process the user identifier.
3932 if ( is_numeric( $id_or_email ) ) {
3933 $user = get_user_by( 'id', absint( $id_or_email ) );
3934 } elseif ( is_string( $id_or_email ) ) {
3935 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3937 list( $email_hash ) = explode( '@', $id_or_email );
3940 $email = $id_or_email;
3942 } elseif ( $id_or_email instanceof WP_User ) {
3944 $user = $id_or_email;
3945 } elseif ( $id_or_email instanceof WP_Post ) {
3947 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3948 } elseif ( $id_or_email instanceof WP_Comment ) {
3950 * Filters the list of allowed comment types for retrieving avatars.
3954 * @param array $types An array of content types. Default only contains 'comment'.
3956 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3957 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3958 $args['url'] = false;
3959 /** This filter is documented in wp-includes/link-template.php */
3960 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3963 if ( ! empty( $id_or_email->user_id ) ) {
3964 $user = get_user_by( 'id', (int) $id_or_email->user_id );
3966 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3967 $email = $id_or_email->comment_author_email;
3971 if ( ! $email_hash ) {
3973 $email = $user->user_email;
3977 $email_hash = md5( strtolower( trim( $email ) ) );
3981 if ( $email_hash ) {
3982 $args['found_avatar'] = true;
3983 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3985 $gravatar_server = rand( 0, 2 );
3989 's' => $args['size'],
3990 'd' => $args['default'],
3991 'f' => $args['force_default'] ? 'y' : false,
3992 'r' => $args['rating'],
3996 $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
3998 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
4001 $url = add_query_arg(
4002 rawurlencode_deep( array_filter( $url_args ) ),
4003 set_url_scheme( $url, $args['scheme'] )
4007 * Filters the avatar URL.
4011 * @param string $url The URL of the avatar.
4012 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4013 * user email, WP_User object, WP_Post object, or WP_Comment object.
4014 * @param array $args Arguments passed to get_avatar_data(), after processing.
4016 $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
4019 * Filters the avatar data.
4023 * @param array $args Arguments passed to get_avatar_data(), after processing.
4024 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4025 * user email, WP_User object, WP_Post object, or WP_Comment object.
4027 return apply_filters( 'get_avatar_data', $args, $id_or_email );