3 * WordPress Link Template Functions
10 * Display the permalink for the current post.
14 function the_permalink() {
16 * Filter the display of the permalink for the current post.
20 * @param string $permalink The permalink for the current post.
22 echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
26 * Retrieve trailing slash string, if blog set for adding trailing slashes.
28 * Conditionally adds a trailing slash if the permalink structure has a trailing
29 * slash, strips the trailing slash if not. The string is passed through the
30 * 'user_trailingslashit' filter. Will remove trailing slash from string, if
31 * blog is not set to have them.
34 * @global WP_Rewrite $wp_rewrite
36 * @param string $string URL with or without a trailing slash.
37 * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
38 * @return string The URL with the trailing slash appended or stripped.
40 function user_trailingslashit($string, $type_of_url = '') {
42 if ( $wp_rewrite->use_trailing_slashes )
43 $string = trailingslashit($string);
45 $string = untrailingslashit($string);
48 * Filter the trailing slashed string, depending on whether the site is set
49 * to use training slashes.
53 * @param string $string URL with or without a trailing slash.
54 * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
55 * 'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',
56 * 'month', 'day', 'paged', 'post_type_archive'.
58 return apply_filters( 'user_trailingslashit', $string, $type_of_url );
62 * Display permalink anchor for current post.
64 * The permalink mode title will use the post title for the 'a' element 'id'
65 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
69 * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
71 function permalink_anchor( $mode = 'id' ) {
73 switch ( strtolower( $mode ) ) {
75 $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
76 echo '<a id="'.$title.'"></a>';
80 echo '<a id="post-' . $post->ID . '"></a>';
86 * Retrieve full permalink for current post or post ID.
88 * This function is an alias for get_permalink().
92 * @see get_permalink()
94 * @param int|WP_Post $id Optional. Post ID or post object. Default is the current post.
95 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
96 * @return string|false The permalink URL or false if post does not exist.
98 function get_the_permalink( $id = 0, $leavename = false ) {
99 return get_permalink( $id, $leavename );
103 * Retrieve full permalink for current post or post ID.
107 * @param int|WP_Post $id Optional. Post ID or post object. Default current post.
108 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
109 * @return string|false The permalink URL or false if post does not exist.
111 function get_permalink( $id = 0, $leavename = false ) {
112 $rewritecode = array(
119 $leavename? '' : '%postname%',
123 $leavename? '' : '%pagename%',
126 if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
130 $post = get_post($id);
134 if ( empty($post->ID) )
137 if ( $post->post_type == 'page' )
138 return get_page_link($post, $leavename, $sample);
139 elseif ( $post->post_type == 'attachment' )
140 return get_attachment_link( $post, $leavename );
141 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
142 return get_post_permalink($post, $leavename, $sample);
144 $permalink = get_option('permalink_structure');
147 * Filter the permalink structure for a post before token replacement occurs.
149 * Only applies to posts with post_type of 'post'.
153 * @param string $permalink The site's permalink structure.
154 * @param WP_Post $post The post in question.
155 * @param bool $leavename Whether to keep the post name.
157 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
159 if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
160 $unixtime = strtotime($post->post_date);
163 if ( strpos($permalink, '%category%') !== false ) {
164 $cats = get_the_category($post->ID);
166 usort($cats, '_usort_terms_by_ID'); // order by ID
169 * Filter the category that gets used in the %category% permalink token.
173 * @param stdClass $cat The category to use in the permalink.
174 * @param array $cats Array of all categories associated with the post.
175 * @param WP_Post $post The post in question.
177 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
179 $category_object = get_term( $category_object, 'category' );
180 $category = $category_object->slug;
181 if ( $parent = $category_object->parent )
182 $category = get_category_parents($parent, false, '/', true) . $category;
184 // show default category in permalinks, without
185 // having to assign it explicitly
186 if ( empty($category) ) {
187 $default_category = get_term( get_option( 'default_category' ), 'category' );
188 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
193 if ( strpos($permalink, '%author%') !== false ) {
194 $authordata = get_userdata($post->post_author);
195 $author = $authordata->user_nicename;
198 $date = explode(" ",date('Y m d H i s', $unixtime));
213 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
214 $permalink = user_trailingslashit($permalink, 'single');
215 } else { // if they're not using the fancy permalink option
216 $permalink = home_url('?p=' . $post->ID);
220 * Filter the permalink for a post.
222 * Only applies to posts with post_type of 'post'.
226 * @param string $permalink The post's permalink.
227 * @param WP_Post $post The post in question.
228 * @param bool $leavename Whether to keep the post name.
230 return apply_filters( 'post_link', $permalink, $post, $leavename );
234 * Retrieve the permalink for a post with a custom post type.
238 * @global WP_Rewrite $wp_rewrite
240 * @param int $id Optional. Post ID.
241 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
242 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
243 * @return string|WP_Error The post permalink.
245 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
248 $post = get_post($id);
250 if ( is_wp_error( $post ) )
253 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
255 $slug = $post->post_name;
257 $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) );
259 $post_type = get_post_type_object($post->post_type);
261 if ( $post_type->hierarchical ) {
262 $slug = get_page_uri( $id );
265 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
266 if ( ! $leavename ) {
267 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
269 $post_link = home_url( user_trailingslashit($post_link) );
271 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
272 $post_link = add_query_arg($post_type->query_var, $slug, '');
274 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
275 $post_link = home_url($post_link);
279 * Filter the permalink for a post with a custom post type.
283 * @param string $post_link The post's permalink.
284 * @param WP_Post $post The post in question.
285 * @param bool $leavename Whether to keep the post name.
286 * @param bool $sample Is it a sample permalink.
288 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
292 * Retrieve permalink from post ID.
296 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
297 * @param mixed $deprecated Not used.
298 * @return string|false
300 function post_permalink( $post_id = 0, $deprecated = '' ) {
301 if ( !empty( $deprecated ) )
302 _deprecated_argument( __FUNCTION__, '1.3' );
304 return get_permalink($post_id);
308 * Retrieve the permalink for current page or page ID.
310 * Respects page_on_front. Use this one.
314 * @param int|object $post Optional. Post ID or object.
315 * @param bool $leavename Optional, defaults to false. Whether to keep page name.
316 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
317 * @return string The page permalink.
319 function get_page_link( $post = false, $leavename = false, $sample = false ) {
320 $post = get_post( $post );
322 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
323 $link = home_url('/');
325 $link = _get_page_link( $post, $leavename, $sample );
328 * Filter the permalink for a page.
332 * @param string $link The page's permalink.
333 * @param int $post_id The ID of the page.
334 * @param bool $sample Is it a sample permalink.
336 return apply_filters( 'page_link', $link, $post->ID, $sample );
340 * Retrieve the page permalink.
342 * Ignores page_on_front. Internal use only.
347 * @global WP_Rewrite $wp_rewrite
349 * @param int|object $post Optional. Post ID or object.
350 * @param bool $leavename Optional. Leave name.
351 * @param bool $sample Optional. Sample permalink.
352 * @return string The page permalink.
354 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
357 $post = get_post( $post );
359 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
361 $link = $wp_rewrite->get_page_permastruct();
363 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
364 if ( ! $leavename ) {
365 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
368 $link = home_url($link);
369 $link = user_trailingslashit($link, 'page');
371 $link = home_url( '?page_id=' . $post->ID );
375 * Filter the permalink for a non-page_on_front page.
379 * @param string $link The page's permalink.
380 * @param int $post_id The ID of the page.
382 return apply_filters( '_get_page_link', $link, $post->ID );
386 * Retrieve permalink for attachment.
388 * This can be used in the WordPress Loop or outside of it.
392 * @global WP_Rewrite $wp_rewrite
394 * @param int|object $post Optional. Post ID or object.
395 * @param bool $leavename Optional. Leave name.
396 * @return string The attachment permalink.
398 function get_attachment_link( $post = null, $leavename = false ) {
403 $post = get_post( $post );
404 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
406 if ( $wp_rewrite->using_permalinks() && $parent ) {
407 if ( 'page' == $parent->post_type )
408 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
410 $parentlink = get_permalink( $post->post_parent );
412 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
413 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
415 $name = $post->post_name;
417 if ( strpos($parentlink, '?') === false )
418 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
421 $link = str_replace( '%postname%', $name, $link );
425 $link = home_url( '/?attachment_id=' . $post->ID );
428 * Filter the permalink for an attachment.
432 * @param string $link The attachment's permalink.
433 * @param int $post_id Attachment ID.
435 return apply_filters( 'attachment_link', $link, $post->ID );
439 * Retrieve the permalink for the year archives.
443 * @global WP_Rewrite $wp_rewrite
445 * @param int|bool $year False for current year or year for permalink.
446 * @return string The permalink for the specified year archive.
448 function get_year_link($year) {
451 $year = gmdate('Y', current_time('timestamp'));
452 $yearlink = $wp_rewrite->get_year_permastruct();
453 if ( !empty($yearlink) ) {
454 $yearlink = str_replace('%year%', $year, $yearlink);
455 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
457 $yearlink = home_url( '?m=' . $year );
461 * Filter the year archive permalink.
465 * @param string $yearlink Permalink for the year archive.
466 * @param int $year Year for the archive.
468 return apply_filters( 'year_link', $yearlink, $year );
472 * Retrieve the permalink for the month archives with year.
476 * @global WP_Rewrite $wp_rewrite
478 * @param bool|int $year False for current year. Integer of year.
479 * @param bool|int $month False for current month. Integer of month.
480 * @return string The permalink for the specified month and year archive.
482 function get_month_link($year, $month) {
485 $year = gmdate('Y', current_time('timestamp'));
487 $month = gmdate('m', current_time('timestamp'));
488 $monthlink = $wp_rewrite->get_month_permastruct();
489 if ( !empty($monthlink) ) {
490 $monthlink = str_replace('%year%', $year, $monthlink);
491 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
492 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
494 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
498 * Filter the month archive permalink.
502 * @param string $monthlink Permalink for the month archive.
503 * @param int $year Year for the archive.
504 * @param int $month The month for the archive.
506 return apply_filters( 'month_link', $monthlink, $year, $month );
510 * Retrieve the permalink for the day archives with year and month.
514 * @global WP_Rewrite $wp_rewrite
516 * @param bool|int $year False for current year. Integer of year.
517 * @param bool|int $month False for current month. Integer of month.
518 * @param bool|int $day False for current day. Integer of day.
519 * @return string The permalink for the specified day, month, and year archive.
521 function get_day_link($year, $month, $day) {
524 $year = gmdate('Y', current_time('timestamp'));
526 $month = gmdate('m', current_time('timestamp'));
528 $day = gmdate('j', current_time('timestamp'));
530 $daylink = $wp_rewrite->get_day_permastruct();
531 if ( !empty($daylink) ) {
532 $daylink = str_replace('%year%', $year, $daylink);
533 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
534 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
535 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
537 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
541 * Filter the day archive permalink.
545 * @param string $daylink Permalink for the day archive.
546 * @param int $year Year for the archive.
547 * @param int $month Month for the archive.
548 * @param int $day The day for the archive.
550 return apply_filters( 'day_link', $daylink, $year, $month, $day );
554 * Display the permalink for the feed type.
558 * @param string $anchor The link's anchor text.
559 * @param string $feed Optional, defaults to default feed. Feed type.
561 function the_feed_link( $anchor, $feed = '' ) {
562 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
565 * Filter the feed link anchor tag.
569 * @param string $link The complete anchor tag for a feed link.
570 * @param string $feed The feed type, or an empty string for the
573 echo apply_filters( 'the_feed_link', $link, $feed );
577 * Retrieve the permalink for the feed type.
581 * @global WP_Rewrite $wp_rewrite
583 * @param string $feed Optional, defaults to default feed. Feed type.
584 * @return string The feed permalink.
586 function get_feed_link($feed = '') {
589 $permalink = $wp_rewrite->get_feed_permastruct();
590 if ( '' != $permalink ) {
591 if ( false !== strpos($feed, 'comments_') ) {
592 $feed = str_replace('comments_', '', $feed);
593 $permalink = $wp_rewrite->get_comment_feed_permastruct();
596 if ( get_default_feed() == $feed )
599 $permalink = str_replace('%feed%', $feed, $permalink);
600 $permalink = preg_replace('#/+#', '/', "/$permalink");
601 $output = home_url( user_trailingslashit($permalink, 'feed') );
604 $feed = get_default_feed();
606 if ( false !== strpos($feed, 'comments_') )
607 $feed = str_replace('comments_', 'comments-', $feed);
609 $output = home_url("?feed={$feed}");
613 * Filter the feed type permalink.
617 * @param string $output The feed permalink.
618 * @param string $feed Feed type.
620 return apply_filters( 'feed_link', $output, $feed );
624 * Retrieve the permalink for the post comments feed.
628 * @param int $post_id Optional. Post ID.
629 * @param string $feed Optional. Feed type.
630 * @return string The permalink for the comments feed for the given post.
632 function get_post_comments_feed_link($post_id = 0, $feed = '') {
633 $post_id = absint( $post_id );
636 $post_id = get_the_ID();
638 if ( empty( $feed ) )
639 $feed = get_default_feed();
641 if ( '' != get_option('permalink_structure') ) {
642 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
643 $url = _get_page_link( $post_id );
645 $url = get_permalink($post_id);
647 $url = trailingslashit($url) . 'feed';
648 if ( $feed != get_default_feed() )
650 $url = user_trailingslashit($url, 'single_feed');
652 $type = get_post_field('post_type', $post_id);
653 if ( 'page' == $type )
654 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
656 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
660 * Filter the post comments feed permalink.
664 * @param string $url Post comments feed permalink.
666 return apply_filters( 'post_comments_feed_link', $url );
670 * Display the comment feed link for a post.
672 * Prints out the comment feed link for a post. Link text is placed in the
673 * anchor. If no link text is specified, default text is used. If no post ID is
674 * specified, the current post is used.
678 * @param string $link_text Descriptive text.
679 * @param int $post_id Optional post ID. Default to current post.
680 * @param string $feed Optional. Feed format.
682 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
683 $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
684 if ( empty($link_text) )
685 $link_text = __('Comments Feed');
688 * Filter the post comment feed link anchor tag.
692 * @param string $link The complete anchor tag for the comment feed link.
693 * @param int $post_id Post ID.
694 * @param string $feed The feed type, or an empty string for the default feed type.
696 echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
700 * Retrieve the feed link for a given author.
702 * Returns a link to the feed for all posts by a given author. A specific feed
703 * can be requested or left blank to get the default feed.
707 * @param int $author_id ID of an author.
708 * @param string $feed Optional. Feed type.
709 * @return string Link to the feed for the author specified by $author_id.
711 function get_author_feed_link( $author_id, $feed = '' ) {
712 $author_id = (int) $author_id;
713 $permalink_structure = get_option('permalink_structure');
716 $feed = get_default_feed();
718 if ( '' == $permalink_structure ) {
719 $link = home_url("?feed=$feed&author=" . $author_id);
721 $link = get_author_posts_url($author_id);
722 if ( $feed == get_default_feed() )
725 $feed_link = "feed/$feed";
727 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
731 * Filter the feed link for a given author.
735 * @param string $link The author feed link.
736 * @param string $feed Feed type.
738 $link = apply_filters( 'author_feed_link', $link, $feed );
744 * Retrieve the feed link for a category.
746 * Returns a link to the feed for all posts in a given category. A specific feed
747 * can be requested or left blank to get the default feed.
751 * @param int $cat_id ID of a category.
752 * @param string $feed Optional. Feed type.
753 * @return string Link to the feed for the category specified by $cat_id.
755 function get_category_feed_link( $cat_id, $feed = '' ) {
756 return get_term_feed_link( $cat_id, 'category', $feed );
760 * Retrieve the feed link for a term.
762 * Returns a link to the feed for all posts in a given term. A specific feed
763 * can be requested or left blank to get the default feed.
767 * @param int $term_id ID of a category.
768 * @param string $taxonomy Optional. Taxonomy of $term_id
769 * @param string $feed Optional. Feed type.
770 * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
772 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
773 $term_id = ( int ) $term_id;
775 $term = get_term( $term_id, $taxonomy );
777 if ( empty( $term ) || is_wp_error( $term ) )
780 if ( empty( $feed ) )
781 $feed = get_default_feed();
783 $permalink_structure = get_option( 'permalink_structure' );
785 if ( '' == $permalink_structure ) {
786 if ( 'category' == $taxonomy ) {
787 $link = home_url("?feed=$feed&cat=$term_id");
789 elseif ( 'post_tag' == $taxonomy ) {
790 $link = home_url("?feed=$feed&tag=$term->slug");
792 $t = get_taxonomy( $taxonomy );
793 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
796 $link = get_term_link( $term_id, $term->taxonomy );
797 if ( $feed == get_default_feed() )
800 $feed_link = "feed/$feed";
802 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
805 if ( 'category' == $taxonomy ) {
807 * Filter the category feed link.
811 * @param string $link The category feed link.
812 * @param string $feed Feed type.
814 $link = apply_filters( 'category_feed_link', $link, $feed );
815 } elseif ( 'post_tag' == $taxonomy ) {
817 * Filter the post tag feed link.
821 * @param string $link The tag feed link.
822 * @param string $feed Feed type.
824 $link = apply_filters( 'tag_feed_link', $link, $feed );
827 * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
831 * @param string $link The taxonomy feed link.
832 * @param string $feed Feed type.
833 * @param string $feed The taxonomy name.
835 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
842 * Retrieve permalink for feed of tag.
846 * @param int $tag_id Tag ID.
847 * @param string $feed Optional. Feed type.
848 * @return string The feed permalink for the given tag.
850 function get_tag_feed_link( $tag_id, $feed = '' ) {
851 return get_term_feed_link( $tag_id, 'post_tag', $feed );
855 * Retrieve edit tag link.
859 * @param int $tag_id Tag ID
860 * @param string $taxonomy Taxonomy
861 * @return string The edit tag link URL for the given tag.
863 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
865 * Filter the edit link for a tag (or term in another taxonomy).
869 * @param string $link The term edit link.
871 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
875 * Display or retrieve edit tag link with formatting.
879 * @param string $link Optional. Anchor text.
880 * @param string $before Optional. Display before edit link.
881 * @param string $after Optional. Display after edit link.
882 * @param object $tag Tag object.
884 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
885 $link = edit_term_link( $link, '', '', $tag, false );
888 * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
892 * @param string $link The anchor tag for the edit link.
894 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
898 * Retrieve edit term url.
902 * @param int $term_id Term ID.
903 * @param string $taxonomy Taxonomy.
904 * @param string $object_type The object type. Used to highlight the proper post type menu on the linked page.
905 * Defaults to the first object_type associated with the taxonomy.
906 * @return string|null The edit term link URL for the given term, or null on failure.
908 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
909 $tax = get_taxonomy( $taxonomy );
910 if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
914 $term = get_term( $term_id, $taxonomy );
915 if ( ! $term || is_wp_error( $term ) ) {
921 'taxonomy' => $taxonomy,
922 'tag_ID' => $term->term_id,
925 if ( $object_type ) {
926 $args['post_type'] = $object_type;
927 } elseif ( ! empty( $tax->object_type ) ) {
928 $args['post_type'] = reset( $tax->object_type );
931 $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
934 * Filter the edit link for a term.
938 * @param string $location The edit link.
939 * @param int $term_id Term ID.
940 * @param string $taxonomy Taxonomy name.
941 * @param string $object_type The object type (eg. the post type).
943 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
947 * Display or retrieve edit term link with formatting.
951 * @param string $link Optional. Anchor text. Default empty.
952 * @param string $before Optional. Display before edit link. Default empty.
953 * @param string $after Optional. Display after edit link. Default empty.
954 * @param object $term Optional. Term object. If null, the queried object will be inspected. Default null.
955 * @param bool $echo Optional. Whether or not to echo the return. Default true.
956 * @return string|void HTML content.
958 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
959 if ( is_null( $term ) )
960 $term = get_queried_object();
965 $tax = get_taxonomy( $term->taxonomy );
966 if ( ! current_user_can( $tax->cap->edit_terms ) )
969 if ( empty( $link ) )
970 $link = __('Edit This');
972 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
975 * Filter the anchor tag for the edit link of a term.
979 * @param string $link The anchor tag for the edit link.
980 * @param int $term_id Term ID.
982 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
991 * Retrieve permalink for search.
995 * @global WP_Rewrite $wp_rewrite
997 * @param string $query Optional. The query string to use. If empty the current query is used.
998 * @return string The search permalink.
1000 function get_search_link( $query = '' ) {
1003 if ( empty($query) )
1004 $search = get_search_query( false );
1006 $search = stripslashes($query);
1008 $permastruct = $wp_rewrite->get_search_permastruct();
1010 if ( empty( $permastruct ) ) {
1011 $link = home_url('?s=' . urlencode($search) );
1013 $search = urlencode($search);
1014 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
1015 $link = str_replace( '%search%', $search, $permastruct );
1016 $link = home_url( user_trailingslashit( $link, 'search' ) );
1020 * Filter the search permalink.
1024 * @param string $link Search permalink.
1025 * @param string $search The URL-encoded search term.
1027 return apply_filters( 'search_link', $link, $search );
1031 * Retrieve the permalink for the feed of the search results.
1035 * @global WP_Rewrite $wp_rewrite
1037 * @param string $search_query Optional. Search query.
1038 * @param string $feed Optional. Feed type.
1039 * @return string The search results feed permalink.
1041 function get_search_feed_link($search_query = '', $feed = '') {
1043 $link = get_search_link($search_query);
1046 $feed = get_default_feed();
1048 $permastruct = $wp_rewrite->get_search_permastruct();
1050 if ( empty($permastruct) ) {
1051 $link = add_query_arg('feed', $feed, $link);
1053 $link = trailingslashit($link);
1054 $link .= "feed/$feed/";
1058 * Filter the search feed link.
1062 * @param string $link Search feed link.
1063 * @param string $feed Feed type.
1064 * @param string $type The search type. One of 'posts' or 'comments'.
1066 return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1070 * Retrieve the permalink for the comments feed of the search results.
1074 * @global WP_Rewrite $wp_rewrite
1076 * @param string $search_query Optional. Search query.
1077 * @param string $feed Optional. Feed type.
1078 * @return string The comments feed search results permalink.
1080 function get_search_comments_feed_link($search_query = '', $feed = '') {
1084 $feed = get_default_feed();
1086 $link = get_search_feed_link($search_query, $feed);
1088 $permastruct = $wp_rewrite->get_search_permastruct();
1090 if ( empty($permastruct) )
1091 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1093 $link = add_query_arg('withcomments', 1, $link);
1095 /** This filter is documented in wp-includes/link-template.php */
1096 return apply_filters( 'search_feed_link', $link, $feed, 'comments' );
1100 * Retrieve the permalink for a post type archive.
1104 * @global WP_Rewrite $wp_rewrite
1106 * @param string $post_type Post type
1107 * @return string|false The post type archive permalink.
1109 function get_post_type_archive_link( $post_type ) {
1111 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1114 if ( ! $post_type_obj->has_archive )
1117 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1118 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1119 if ( $post_type_obj->rewrite['with_front'] )
1120 $struct = $wp_rewrite->front . $struct;
1122 $struct = $wp_rewrite->root . $struct;
1123 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1125 $link = home_url( '?post_type=' . $post_type );
1129 * Filter the post type archive permalink.
1133 * @param string $link The post type archive permalink.
1134 * @param string $post_type Post type name.
1136 return apply_filters( 'post_type_archive_link', $link, $post_type );
1140 * Retrieve the permalink for a post type archive feed.
1144 * @param string $post_type Post type
1145 * @param string $feed Optional. Feed type
1146 * @return string|false The post type feed permalink.
1148 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1149 $default_feed = get_default_feed();
1150 if ( empty( $feed ) )
1151 $feed = $default_feed;
1153 if ( ! $link = get_post_type_archive_link( $post_type ) )
1156 $post_type_obj = get_post_type_object( $post_type );
1157 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1158 $link = trailingslashit( $link );
1160 if ( $feed != $default_feed )
1163 $link = add_query_arg( 'feed', $feed, $link );
1167 * Filter the post type archive feed link.
1171 * @param string $link The post type archive feed link.
1172 * @param string $feed Feed type.
1174 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1178 * Retrieve edit posts link for post.
1180 * Can be used within the WordPress loop or outside of it. Can be used with
1181 * pages, posts, attachments, and revisions.
1185 * @param int $id Optional. Post ID.
1186 * @param string $context Optional, defaults to display. How to write the '&', defaults to '&'.
1187 * @return string|void The edit post link for the given post.
1189 function get_edit_post_link( $id = 0, $context = 'display' ) {
1190 if ( ! $post = get_post( $id ) )
1193 if ( 'revision' === $post->post_type )
1195 elseif ( 'display' == $context )
1196 $action = '&action=edit';
1198 $action = '&action=edit';
1200 $post_type_object = get_post_type_object( $post->post_type );
1201 if ( !$post_type_object )
1204 if ( !current_user_can( 'edit_post', $post->ID ) )
1208 * Filter the post edit link.
1212 * @param string $link The edit link.
1213 * @param int $post_id Post ID.
1214 * @param string $context The link context. If set to 'display' then ampersands
1217 return apply_filters( 'get_edit_post_link', admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ), $post->ID, $context );
1221 * Display edit post link for post.
1225 * @param string $text Optional. Anchor text.
1226 * @param string $before Optional. Display before edit link.
1227 * @param string $after Optional. Display after edit link.
1228 * @param int $id Optional. Post ID.
1230 function edit_post_link( $text = null, $before = '', $after = '', $id = 0 ) {
1231 if ( ! $post = get_post( $id ) ) {
1235 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1239 if ( null === $text ) {
1240 $text = __( 'Edit This' );
1243 $link = '<a class="post-edit-link" href="' . $url . '">' . $text . '</a>';
1246 * Filter the post edit link anchor tag.
1250 * @param string $link Anchor tag for the edit link.
1251 * @param int $post_id Post ID.
1252 * @param string $text Anchor text.
1254 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1258 * Retrieve delete posts link for post.
1260 * Can be used within the WordPress loop or outside of it, with any post type.
1264 * @param int $id Optional. Post ID.
1265 * @param string $deprecated Not used.
1266 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1267 * @return string|void The delete post link URL for the given post.
1269 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1270 if ( ! empty( $deprecated ) )
1271 _deprecated_argument( __FUNCTION__, '3.0' );
1273 if ( !$post = get_post( $id ) )
1276 $post_type_object = get_post_type_object( $post->post_type );
1277 if ( !$post_type_object )
1280 if ( !current_user_can( 'delete_post', $post->ID ) )
1283 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1285 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1288 * Filter the post delete link.
1292 * @param string $link The delete link.
1293 * @param int $post_id Post ID.
1294 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1296 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1300 * Retrieve edit comment link.
1304 * @param int $comment_id Optional. Comment ID.
1305 * @return string|void The edit comment link URL for the given comment.
1307 function get_edit_comment_link( $comment_id = 0 ) {
1308 $comment = get_comment( $comment_id );
1310 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1313 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1316 * Filter the comment edit link.
1320 * @param string $location The edit link.
1322 return apply_filters( 'get_edit_comment_link', $location );
1326 * Display edit comment link with formatting.
1330 * @global object $comment
1332 * @param string $text Optional. Anchor text.
1333 * @param string $before Optional. Display before edit link.
1334 * @param string $after Optional. Display after edit link.
1336 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1339 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1343 if ( null === $text ) {
1344 $text = __( 'Edit This' );
1347 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $text . '</a>';
1350 * Filter the comment edit link anchor tag.
1354 * @param string $link Anchor tag for the edit link.
1355 * @param int $comment_id Comment ID.
1356 * @param string $text Anchor text.
1358 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1362 * Display edit bookmark (literally a URL external to blog) link.
1366 * @param int|stdClass $link Optional. Bookmark ID.
1367 * @return string|void The edit bookmark link URL.
1369 function get_edit_bookmark_link( $link = 0 ) {
1370 $link = get_bookmark( $link );
1372 if ( !current_user_can('manage_links') )
1375 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1378 * Filter the bookmark (link) edit link.
1382 * @param string $location The edit link.
1383 * @param int $link_id Bookmark ID.
1385 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1389 * Display edit bookmark (literally a URL external to blog) link anchor content.
1393 * @param string $link Optional. Anchor text.
1394 * @param string $before Optional. Display before edit link.
1395 * @param string $after Optional. Display after edit link.
1396 * @param int $bookmark Optional. Bookmark ID.
1398 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1399 $bookmark = get_bookmark($bookmark);
1401 if ( !current_user_can('manage_links') )
1405 $link = __('Edit This');
1407 $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
1410 * Filter the bookmark edit link anchor tag.
1414 * @param string $link Anchor tag for the edit link.
1415 * @param int $link_id Bookmark ID.
1417 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1421 * Retrieve edit user link
1425 * @param int $user_id Optional. User ID. Defaults to the current user.
1426 * @return string URL to edit user page or empty string.
1428 function get_edit_user_link( $user_id = null ) {
1430 $user_id = get_current_user_id();
1432 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1435 $user = get_userdata( $user_id );
1440 if ( get_current_user_id() == $user->ID )
1441 $link = get_edit_profile_url( $user->ID );
1443 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1446 * Filter the user edit link.
1450 * @param string $link The edit link.
1451 * @param int $user_id User ID.
1453 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1459 * Retrieve previous post that is adjacent to current post.
1463 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1464 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1465 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1466 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1468 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1469 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1473 * Retrieve next post that is adjacent to current post.
1477 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1478 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1479 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1480 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1482 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1483 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1487 * Retrieve adjacent post.
1489 * Can either be next or previous post.
1493 * @global wpdb $wpdb
1495 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1496 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1497 * @param bool $previous Optional. Whether to retrieve previous post.
1498 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1499 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1501 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1504 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1507 $current_post_date = $post->post_date;
1512 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1513 $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";
1514 $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1516 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1517 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1518 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1519 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1520 $excluded_terms = explode( ' and ', $excluded_terms );
1522 $excluded_terms = explode( ',', $excluded_terms );
1525 $excluded_terms = array_map( 'intval', $excluded_terms );
1528 if ( $in_same_term ) {
1529 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1531 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1533 // Remove any exclusions from the term array to include.
1534 $term_array = array_diff( $term_array, (array) $excluded_terms );
1535 $term_array = array_map( 'intval', $term_array );
1537 if ( ! $term_array || is_wp_error( $term_array ) )
1540 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1543 if ( ! empty( $excluded_terms ) ) {
1544 $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( $excluded_terms, ',' ) . ') )';
1548 // 'post_status' clause depends on the current user.
1549 if ( is_user_logged_in() ) {
1550 $user_id = get_current_user_id();
1552 $post_type_object = get_post_type_object( $post->post_type );
1553 if ( empty( $post_type_object ) ) {
1554 $post_type_cap = $post->post_type;
1555 $read_private_cap = 'read_private_' . $post_type_cap . 's';
1557 $read_private_cap = $post_type_object->cap->read_private_posts;
1561 * Results should include private posts belonging to the current user, or private posts where the
1562 * current user has the 'read_private_posts' cap.
1564 $private_states = get_post_stati( array( 'private' => true ) );
1565 $where .= " AND ( p.post_status = 'publish'";
1566 foreach ( (array) $private_states as $state ) {
1567 if ( current_user_can( $read_private_cap ) ) {
1568 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1570 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1575 $where .= " AND p.post_status = 'publish'";
1578 $adjacent = $previous ? 'previous' : 'next';
1579 $op = $previous ? '<' : '>';
1580 $order = $previous ? 'DESC' : 'ASC';
1583 * Filter the JOIN clause in the SQL for an adjacent post query.
1585 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1586 * of adjacency, 'next' or 'previous'.
1590 * @param string $join The JOIN clause in the SQL.
1591 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1592 * @param array $excluded_terms Array of excluded term IDs.
1594 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
1597 * Filter the WHERE clause in the SQL for an adjacent post query.
1599 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1600 * of adjacency, 'next' or 'previous'.
1604 * @param string $where The `WHERE` clause in the SQL.
1605 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1606 * @param array $excluded_terms Array of excluded term IDs.
1608 $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 );
1611 * Filter the ORDER BY clause in the SQL for an adjacent post query.
1613 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1614 * of adjacency, 'next' or 'previous'.
1618 * @param string $order_by The `ORDER BY` clause in the SQL.
1620 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1622 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1623 $query_key = 'adjacent_post_' . md5( $query );
1624 $result = wp_cache_get( $query_key, 'counts' );
1625 if ( false !== $result ) {
1627 $result = get_post( $result );
1631 $result = $wpdb->get_var( $query );
1632 if ( null === $result )
1635 wp_cache_set( $query_key, $result, 'counts' );
1638 $result = get_post( $result );
1644 * Get adjacent post relational link.
1646 * Can either be next or previous post relational link.
1650 * @param string $title Optional. Link title format.
1651 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1652 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1653 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1654 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1655 * @return string|void The adjacent post relational link URL.
1657 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1658 if ( $previous && is_attachment() && $post = get_post() )
1659 $post = get_post( $post->post_parent );
1661 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1663 if ( empty( $post ) )
1666 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1668 if ( empty( $post_title ) )
1669 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1671 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1673 $title = str_replace( '%title', $post_title, $title );
1674 $title = str_replace( '%date', $date, $title );
1676 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1677 $link .= esc_attr( $title );
1678 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1680 $adjacent = $previous ? 'previous' : 'next';
1683 * Filter the adjacent post relational link.
1685 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1686 * of adjacency, 'next' or 'previous'.
1690 * @param string $link The relational link.
1692 return apply_filters( "{$adjacent}_post_rel_link", $link );
1696 * Display relational links for the posts adjacent to the current post.
1700 * @param string $title Optional. Link title format.
1701 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1702 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1703 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1705 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1706 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1707 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1711 * Display relational links for the posts adjacent to the current post for single post pages.
1713 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1717 function adjacent_posts_rel_link_wp_head() {
1718 if ( ! is_single() || is_attachment() ) {
1721 adjacent_posts_rel_link();
1725 * Display relational link for the next post adjacent to the current post.
1729 * @param string $title Optional. Link title format.
1730 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1731 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1732 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1734 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1735 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1739 * Display relational link for the previous post adjacent to the current post.
1743 * @param string $title Optional. Link title format.
1744 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1745 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1746 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1748 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1749 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1753 * Retrieve boundary post.
1755 * Boundary being either the first or last post by publish date within the constraints specified
1756 * by $in_same_term or $excluded_terms.
1760 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1761 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1762 * @param bool $start Optional. Whether to retrieve first or last post.
1763 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1764 * @return null|array Array containing the boundary post object if successful, null otherwise.
1766 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1768 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1771 $query_args = array(
1772 'posts_per_page' => 1,
1773 'order' => $start ? 'ASC' : 'DESC',
1774 'update_post_term_cache' => false,
1775 'update_post_meta_cache' => false
1778 $term_array = array();
1780 if ( ! is_array( $excluded_terms ) ) {
1781 if ( ! empty( $excluded_terms ) )
1782 $excluded_terms = explode( ',', $excluded_terms );
1784 $excluded_terms = array();
1787 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1788 if ( $in_same_term )
1789 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1791 if ( ! empty( $excluded_terms ) ) {
1792 $excluded_terms = array_map( 'intval', $excluded_terms );
1793 $excluded_terms = array_diff( $excluded_terms, $term_array );
1795 $inverse_terms = array();
1796 foreach ( $excluded_terms as $excluded_term )
1797 $inverse_terms[] = $excluded_term * -1;
1798 $excluded_terms = $inverse_terms;
1801 $query_args[ 'tax_query' ] = array( array(
1802 'taxonomy' => $taxonomy,
1803 'terms' => array_merge( $term_array, $excluded_terms )
1807 return get_posts( $query_args );
1811 * Get previous post link that is adjacent to the current post.
1815 * @param string $format Optional. Link anchor format.
1816 * @param string $link Optional. Link permalink format.
1817 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1818 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1819 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1820 * @return string The link URL of the previous post in relation to the current post.
1822 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1823 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1827 * Display previous post link that is adjacent to the current post.
1830 * @see get_previous_post_link()
1832 * @param string $format Optional. Link anchor format.
1833 * @param string $link Optional. Link permalink format.
1834 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1835 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1836 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1838 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1839 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1843 * Get next post link that is adjacent to the current post.
1847 * @param string $format Optional. Link anchor format.
1848 * @param string $link Optional. Link permalink format.
1849 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1850 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1851 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1852 * @return string The link URL of the next post in relation to the current post.
1854 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1855 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1859 * Display next post link that is adjacent to the current post.
1862 * @see get_next_post_link()
1864 * @param string $format Optional. Link anchor format.
1865 * @param string $link Optional. Link permalink format.
1866 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1867 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1868 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1870 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1871 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1875 * Get adjacent post link.
1877 * Can be either next post link or previous.
1881 * @param string $format Link anchor format.
1882 * @param string $link Link permalink format.
1883 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1884 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1885 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1886 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1887 * @return string The link URL of the previous or next post in relation to the current post.
1889 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1890 if ( $previous && is_attachment() )
1891 $post = get_post( get_post()->post_parent );
1893 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1898 $title = $post->post_title;
1900 if ( empty( $post->post_title ) )
1901 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1903 /** This filter is documented in wp-includes/post-template.php */
1904 $title = apply_filters( 'the_title', $title, $post->ID );
1906 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1907 $rel = $previous ? 'prev' : 'next';
1909 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1910 $inlink = str_replace( '%title', $title, $link );
1911 $inlink = str_replace( '%date', $date, $inlink );
1912 $inlink = $string . $inlink . '</a>';
1914 $output = str_replace( '%link', $inlink, $format );
1917 $adjacent = $previous ? 'previous' : 'next';
1920 * Filter the adjacent post link.
1922 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1923 * of adjacency, 'next' or 'previous'.
1926 * @since 4.2.0 Added the `$adjacent` parameter.
1928 * @param string $output The adjacent post link.
1929 * @param string $format Link anchor format.
1930 * @param string $link Link permalink format.
1931 * @param WP_Post $post The adjacent post.
1932 * @param string $adjacent Whether the post is previous or next.
1934 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
1938 * Display adjacent post link.
1940 * Can be either next post link or previous.
1944 * @param string $format Link anchor format.
1945 * @param string $link Link permalink format.
1946 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1947 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
1948 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1949 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1951 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1952 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
1956 * Retrieve links for page numbers.
1960 * @global WP_Rewrite $wp_rewrite
1962 * @param int $pagenum Optional. Page ID.
1963 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
1964 * Otherwise, prepares the URL with esc_url_raw().
1965 * @return string The link URL for the given page number.
1967 function get_pagenum_link($pagenum = 1, $escape = true ) {
1970 $pagenum = (int) $pagenum;
1972 $request = remove_query_arg( 'paged' );
1974 $home_root = parse_url(home_url());
1975 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1976 $home_root = preg_quote( $home_root, '|' );
1978 $request = preg_replace('|^'. $home_root . '|i', '', $request);
1979 $request = preg_replace('|^/+|', '', $request);
1981 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1982 $base = trailingslashit( get_bloginfo( 'url' ) );
1984 if ( $pagenum > 1 ) {
1985 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1987 $result = $base . $request;
1990 $qs_regex = '|\?.*?$|';
1991 preg_match( $qs_regex, $request, $qs_match );
1993 if ( !empty( $qs_match[0] ) ) {
1994 $query_string = $qs_match[0];
1995 $request = preg_replace( $qs_regex, '', $request );
2000 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
2001 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
2002 $request = ltrim($request, '/');
2004 $base = trailingslashit( get_bloginfo( 'url' ) );
2006 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
2007 $base .= $wp_rewrite->index . '/';
2009 if ( $pagenum > 1 ) {
2010 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
2013 $result = $base . $request . $query_string;
2017 * Filter the page number link for the current request.
2021 * @param string $result The page number link.
2023 $result = apply_filters( 'get_pagenum_link', $result );
2026 return esc_url( $result );
2028 return esc_url_raw( $result );
2032 * Retrieve next posts page link.
2034 * Backported from 2.1.3 to 2.0.10.
2038 * @global int $paged
2040 * @param int $max_page Optional. Max pages.
2041 * @return string|void The link URL for next posts page.
2043 function get_next_posts_page_link($max_page = 0) {
2046 if ( !is_single() ) {
2049 $nextpage = intval($paged) + 1;
2050 if ( !$max_page || $max_page >= $nextpage )
2051 return get_pagenum_link($nextpage);
2056 * Display or return the next posts page link.
2060 * @param int $max_page Optional. Max pages.
2061 * @param bool $echo Optional. Echo or return;
2062 * @return string|void The link URL for next posts page if `$echo = false`.
2064 function next_posts( $max_page = 0, $echo = true ) {
2065 $output = esc_url( get_next_posts_page_link( $max_page ) );
2074 * Return the next posts page link.
2078 * @global int $paged
2079 * @global WP_Query $wp_query
2081 * @param string $label Content for link text.
2082 * @param int $max_page Optional. Max pages.
2083 * @return string|void HTML-formatted next posts page link.
2085 function get_next_posts_link( $label = null, $max_page = 0 ) {
2086 global $paged, $wp_query;
2089 $max_page = $wp_query->max_num_pages;
2094 $nextpage = intval($paged) + 1;
2096 if ( null === $label )
2097 $label = __( 'Next Page »' );
2099 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2101 * Filter the anchor tag attributes for the next posts page link.
2105 * @param string $attributes Attributes for the anchor tag.
2107 $attr = apply_filters( 'next_posts_link_attributes', '' );
2109 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2114 * Display the next posts page link.
2118 * @param string $label Content for link text.
2119 * @param int $max_page Optional. Max pages.
2121 function next_posts_link( $label = null, $max_page = 0 ) {
2122 echo get_next_posts_link( $label, $max_page );
2126 * Retrieve previous posts page link.
2128 * Will only return string, if not on a single page or post.
2130 * Backported to 2.0.10 from 2.1.3.
2134 * @global int $paged
2136 * @return string|void The link for the previous posts page.
2138 function get_previous_posts_page_link() {
2141 if ( !is_single() ) {
2142 $nextpage = intval($paged) - 1;
2143 if ( $nextpage < 1 )
2145 return get_pagenum_link($nextpage);
2150 * Display or return the previous posts page link.
2154 * @param bool $echo Optional. Echo or return;
2155 * @return string|void The previous posts page link if `$echo = false`.
2157 function previous_posts( $echo = true ) {
2158 $output = esc_url( get_previous_posts_page_link() );
2167 * Return the previous posts page link.
2171 * @global int $paged
2173 * @param string $label Optional. Previous page link text.
2174 * @return string|void HTML-formatted previous page link.
2176 function get_previous_posts_link( $label = null ) {
2179 if ( null === $label )
2180 $label = __( '« Previous Page' );
2182 if ( !is_single() && $paged > 1 ) {
2184 * Filter the anchor tag attributes for the previous posts page link.
2188 * @param string $attributes Attributes for the anchor tag.
2190 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2191 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2196 * Display the previous posts page link.
2200 * @param string $label Optional. Previous page link text.
2202 function previous_posts_link( $label = null ) {
2203 echo get_previous_posts_link( $label );
2207 * Return post pages link navigation for previous and next pages.
2211 * @global WP_Query $wp_query
2213 * @param string|array $args Optional args.
2214 * @return string The posts link navigation.
2216 function get_posts_nav_link( $args = array() ) {
2221 if ( !is_singular() ) {
2223 'sep' => ' — ',
2224 'prelabel' => __('« Previous Page'),
2225 'nxtlabel' => __('Next Page »'),
2227 $args = wp_parse_args( $args, $defaults );
2229 $max_num_pages = $wp_query->max_num_pages;
2230 $paged = get_query_var('paged');
2232 //only have sep if there's both prev and next results
2233 if ($paged < 2 || $paged >= $max_num_pages) {
2237 if ( $max_num_pages > 1 ) {
2238 $return = get_previous_posts_link($args['prelabel']);
2239 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2240 $return .= get_next_posts_link($args['nxtlabel']);
2248 * Display post pages link navigation for previous and next pages.
2252 * @param string $sep Optional. Separator for posts navigation links.
2253 * @param string $prelabel Optional. Label for previous pages.
2254 * @param string $nxtlabel Optional Label for next pages.
2256 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2257 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2258 echo get_posts_nav_link($args);
2262 * Return navigation to next/previous post when applicable.
2266 * @param array $args {
2267 * Optional. Default post navigation arguments. Default empty array.
2269 * @type string $prev_text Anchor text to display in the previous post link. Default `%title`.
2270 * @type string $next_text Anchor text to display in the next post link. Default `%title`.
2271 * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2273 * @return string Markup for post links.
2275 function get_the_post_navigation( $args = array() ) {
2276 $args = wp_parse_args( $args, array(
2277 'prev_text' => '%title',
2278 'next_text' => '%title',
2279 'screen_reader_text' => __( 'Post navigation' ),
2283 $previous = get_previous_post_link( '<div class="nav-previous">%link</div>', $args['prev_text'] );
2284 $next = get_next_post_link( '<div class="nav-next">%link</div>', $args['next_text'] );
2286 // Only add markup if there's somewhere to navigate to.
2287 if ( $previous || $next ) {
2288 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2295 * Display navigation to next/previous post when applicable.
2299 * @param array $args Optional. See {@see get_the_post_navigation()} for available
2300 * arguments. Default empty array.
2302 function the_post_navigation( $args = array() ) {
2303 echo get_the_post_navigation( $args );
2307 * Return navigation to next/previous set of posts when applicable.
2311 * @global WP_Query $wp_query WordPress Query object.
2313 * @param array $args {
2314 * Optional. Default posts navigation arguments. Default empty array.
2316 * @type string $prev_text Anchor text to display in the previous posts link.
2317 * Default 'Older posts'.
2318 * @type string $next_text Anchor text to display in the next posts link.
2319 * Default 'Newer posts'.
2320 * @type string $screen_reader_text Screen reader text for nav element.
2321 * Default 'Posts navigation'.
2323 * @return string Markup for posts links.
2325 function get_the_posts_navigation( $args = array() ) {
2328 // Don't print empty markup if there's only one page.
2329 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2330 $args = wp_parse_args( $args, array(
2331 'prev_text' => __( 'Older posts' ),
2332 'next_text' => __( 'Newer posts' ),
2333 'screen_reader_text' => __( 'Posts navigation' ),
2336 $next_link = get_previous_posts_link( $args['next_text'] );
2337 $prev_link = get_next_posts_link( $args['prev_text'] );
2340 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2344 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2347 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2354 * Display navigation to next/previous set of posts when applicable.
2358 * @param array $args Optional. See {@see get_the_posts_navigation()} for available
2359 * arguments. Default empty array.
2361 function the_posts_navigation( $args = array() ) {
2362 echo get_the_posts_navigation( $args );
2366 * Return a paginated navigation to next/previous set of posts,
2371 * @param array $args {
2372 * Optional. Default pagination arguments, {@see paginate_links()}.
2374 * @type string $screen_reader_text Screen reader text for navigation element.
2375 * Default 'Posts navigation'.
2377 * @return string Markup for pagination links.
2379 function get_the_posts_pagination( $args = array() ) {
2382 // Don't print empty markup if there's only one page.
2383 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2384 $args = wp_parse_args( $args, array(
2386 'prev_text' => _x( 'Previous', 'previous post' ),
2387 'next_text' => _x( 'Next', 'next post' ),
2388 'screen_reader_text' => __( 'Posts navigation' ),
2391 // Make sure we get a string back. Plain is the next best thing.
2392 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2393 $args['type'] = 'plain';
2396 // Set up paginated links.
2397 $links = paginate_links( $args );
2400 $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2408 * Display a paginated navigation to next/previous set of posts,
2413 * @param array $args Optional. See {@see get_the_posts_pagination()} for available arguments.
2414 * Default empty array.
2416 function the_posts_pagination( $args = array() ) {
2417 echo get_the_posts_pagination( $args );
2421 * Wraps passed links in navigational markup.
2426 * @param string $links Navigational links.
2427 * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'.
2428 * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2429 * @return string Navigation template tag.
2431 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2432 if ( empty( $screen_reader_text ) ) {
2433 $screen_reader_text = __( 'Posts navigation' );
2437 <nav class="navigation %1$s" role="navigation">
2438 <h2 class="screen-reader-text">%2$s</h2>
2439 <div class="nav-links">%3$s</div>
2442 return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2446 * Retrieve comments page number link.
2450 * @global WP_Rewrite $wp_rewrite
2452 * @param int $pagenum Optional. Page number.
2453 * @param int $max_page Optional. The maximum number of comment pages.
2454 * @return string The comments page number link URL.
2456 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2459 $pagenum = (int) $pagenum;
2461 $result = get_permalink();
2463 if ( 'newest' == get_option('default_comments_page') ) {
2464 if ( $pagenum != $max_page ) {
2465 if ( $wp_rewrite->using_permalinks() )
2466 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2468 $result = add_query_arg( 'cpage', $pagenum, $result );
2470 } elseif ( $pagenum > 1 ) {
2471 if ( $wp_rewrite->using_permalinks() )
2472 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2474 $result = add_query_arg( 'cpage', $pagenum, $result );
2477 $result .= '#comments';
2480 * Filter the comments page number link for the current request.
2484 * @param string $result The comments page number link.
2486 return apply_filters( 'get_comments_pagenum_link', $result );
2490 * Return the link to next comments page.
2494 * @global WP_Query $wp_query
2496 * @param string $label Optional. Label for link text.
2497 * @param int $max_page Optional. Max page.
2498 * @return string|void HTML-formatted link for the next page of comments.
2500 function get_next_comments_link( $label = '', $max_page = 0 ) {
2503 if ( !is_singular() || !get_option('page_comments') )
2506 $page = get_query_var('cpage');
2512 $nextpage = intval($page) + 1;
2514 if ( empty($max_page) )
2515 $max_page = $wp_query->max_num_comment_pages;
2517 if ( empty($max_page) )
2518 $max_page = get_comment_pages_count();
2520 if ( $nextpage > $max_page )
2523 if ( empty($label) )
2524 $label = __('Newer Comments »');
2527 * Filter the anchor tag attributes for the next comments page link.
2531 * @param string $attributes Attributes for the anchor tag.
2533 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>';
2537 * Display the link to next comments page.
2541 * @param string $label Optional. Label for link text.
2542 * @param int $max_page Optional. Max page.
2544 function next_comments_link( $label = '', $max_page = 0 ) {
2545 echo get_next_comments_link( $label, $max_page );
2549 * Return the previous comments page link.
2553 * @param string $label Optional. Label for comments link text.
2554 * @return string|void HTML-formatted link for the previous page of comments.
2556 function get_previous_comments_link( $label = '' ) {
2557 if ( !is_singular() || !get_option('page_comments') )
2560 $page = get_query_var('cpage');
2562 if ( intval($page) <= 1 )
2565 $prevpage = intval($page) - 1;
2567 if ( empty($label) )
2568 $label = __('« Older Comments');
2571 * Filter the anchor tag attributes for the previous comments page link.
2575 * @param string $attributes Attributes for the anchor tag.
2577 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>';
2581 * Display the previous comments page link.
2585 * @param string $label Optional. Label for comments link text.
2587 function previous_comments_link( $label = '' ) {
2588 echo get_previous_comments_link( $label );
2592 * Create pagination links for the comments on the current post.
2594 * @see paginate_links()
2597 * @global WP_Rewrite $wp_rewrite
2599 * @param string|array $args Optional args. See paginate_links().
2600 * @return string|void Markup for pagination links.
2602 function paginate_comments_links($args = array()) {
2605 if ( !is_singular() || !get_option('page_comments') )
2608 $page = get_query_var('cpage');
2611 $max_page = get_comment_pages_count();
2613 'base' => add_query_arg( 'cpage', '%#%' ),
2615 'total' => $max_page,
2618 'add_fragment' => '#comments'
2620 if ( $wp_rewrite->using_permalinks() )
2621 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2623 $args = wp_parse_args( $args, $defaults );
2624 $page_links = paginate_links( $args );
2626 if ( $args['echo'] )
2633 * Retrieve the Press This bookmarklet link.
2635 * Use this in 'a' element 'href' attribute.
2639 * @global bool $is_IE
2640 * @global string $wp_version
2641 * @global WP_Press_This $wp_press_this
2643 * @return string The Press This bookmarklet link URL.
2645 function get_shortcut_link() {
2646 global $is_IE, $wp_version;
2648 include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2649 $bookmarklet_version = $GLOBALS['wp_press_this']->version;
2654 * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2655 * since they only support a max length of ~2000 characters for
2656 * bookmark[let] URLs, which is way to small for our smarter one.
2657 * Do update the version number so users do not get the "upgrade your
2658 * bookmarklet" notice when using PT in those browsers.
2660 $ua = $_SERVER['HTTP_USER_AGENT'];
2662 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2663 $url = wp_json_encode( admin_url( 'press-this.php' ) );
2665 $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2666 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2667 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' .
2668 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2669 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2673 if ( empty( $link ) ) {
2674 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2677 $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version );
2678 $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2682 $link = str_replace( array( "\r", "\n", "\t" ), '', $link );
2685 * Filter the Press This bookmarklet link.
2689 * @param string $link The Press This bookmarklet link.
2691 return apply_filters( 'shortcut_link', $link );
2695 * Retrieve the home url for the current site.
2697 * Returns the 'home' option with the appropriate protocol, 'https' if
2698 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2699 * `is_ssl()` is overridden.
2703 * @param string $path Optional. Path relative to the home url. Default empty.
2704 * @param string $scheme Optional. Scheme to give the home url context. Accepts
2705 * 'http', 'https', or 'relative'. Default null.
2706 * @return string Home url link with optional path appended.
2708 function home_url( $path = '', $scheme = null ) {
2709 return get_home_url( null, $path, $scheme );
2713 * Retrieve the home url for a given site.
2715 * Returns the 'home' option with the appropriate protocol, 'https' if
2716 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2722 * @global string $pagenow
2724 * @param int $blog_id Optional. Blog ID. Default null (current blog).
2725 * @param string $path Optional. Path relative to the home URL. Default empty.
2726 * @param string|null $orig_scheme Optional. Scheme to give the home URL context. Accepts
2727 * 'http', 'https', 'relative', or null. Default null.
2728 * @return string Home URL link with optional path appended.
2730 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2731 $orig_scheme = $scheme;
2733 if ( empty( $blog_id ) || !is_multisite() ) {
2734 $url = get_option( 'home' );
2736 switch_to_blog( $blog_id );
2737 $url = get_option( 'home' );
2738 restore_current_blog();
2741 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2742 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
2745 $scheme = parse_url( $url, PHP_URL_SCHEME );
2748 $url = set_url_scheme( $url, $scheme );
2750 if ( $path && is_string( $path ) )
2751 $url .= '/' . ltrim( $path, '/' );
2754 * Filter the home URL.
2758 * @param string $url The complete home URL including scheme and path.
2759 * @param string $path Path relative to the home URL. Blank string if no path is specified.
2760 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
2761 * @param int|null $blog_id Blog ID, or null for the current blog.
2763 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
2767 * Retrieve the site url for the current site.
2769 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2770 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2775 * @param string $path Optional. Path relative to the site url.
2776 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2777 * @return string Site url link with optional path appended.
2779 function site_url( $path = '', $scheme = null ) {
2780 return get_site_url( null, $path, $scheme );
2784 * Retrieve the site url for a given site.
2786 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2787 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2788 * `is_ssl()` is overridden.
2792 * @param int $blog_id Optional. Blog ID. Default null (current site).
2793 * @param string $path Optional. Path relative to the site url. Default empty.
2794 * @param string $scheme Optional. Scheme to give the site url context. Accepts
2795 * 'http', 'https', 'login', 'login_post', 'admin', or
2796 * 'relative'. Default null.
2797 * @return string Site url link with optional path appended.
2799 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2800 if ( empty( $blog_id ) || !is_multisite() ) {
2801 $url = get_option( 'siteurl' );
2803 switch_to_blog( $blog_id );
2804 $url = get_option( 'siteurl' );
2805 restore_current_blog();
2808 $url = set_url_scheme( $url, $scheme );
2810 if ( $path && is_string( $path ) )
2811 $url .= '/' . ltrim( $path, '/' );
2814 * Filter the site URL.
2818 * @param string $url The complete site URL including scheme and path.
2819 * @param string $path Path relative to the site URL. Blank string if no path is specified.
2820 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
2821 * 'login_post', 'admin', 'relative' or null.
2822 * @param int|null $blog_id Blog ID, or null for the current blog.
2824 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
2828 * Retrieve the url to the admin area for the current site.
2832 * @param string $path Optional path relative to the admin url.
2833 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
2834 * @return string Admin url link with optional path appended.
2836 function admin_url( $path = '', $scheme = 'admin' ) {
2837 return get_admin_url( null, $path, $scheme );
2841 * Retrieves the url to the admin area for a given site.
2845 * @param int $blog_id Optional. Blog ID. Default null (current site).
2846 * @param string $path Optional. Path relative to the admin url. Default empty.
2847 * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
2848 * to force those schemes. Default 'admin', which obeys
2849 * {@see force_ssl_admin()} and {@see is_ssl()}.
2850 * @return string Admin url link with optional path appended.
2852 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2853 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2855 if ( $path && is_string( $path ) )
2856 $url .= ltrim( $path, '/' );
2859 * Filter the admin area URL.
2863 * @param string $url The complete admin area URL including scheme and path.
2864 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
2865 * @param int|null $blog_id Blog ID, or null for the current blog.
2867 return apply_filters( 'admin_url', $url, $path, $blog_id );
2871 * Retrieve the url to the includes directory.
2875 * @param string $path Optional. Path relative to the includes url.
2876 * @param string $scheme Optional. Scheme to give the includes url context.
2877 * @return string Includes url link with optional path appended.
2879 function includes_url( $path = '', $scheme = null ) {
2880 $url = site_url( '/' . WPINC . '/', $scheme );
2882 if ( $path && is_string( $path ) )
2883 $url .= ltrim($path, '/');
2886 * Filter the URL to the includes directory.
2890 * @param string $url The complete URL to the includes directory including scheme and path.
2891 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
2892 * if no path is specified.
2894 return apply_filters( 'includes_url', $url, $path );
2898 * Retrieve the url to the content directory.
2902 * @param string $path Optional. Path relative to the content url.
2903 * @return string Content url link with optional path appended.
2905 function content_url($path = '') {
2906 $url = set_url_scheme( WP_CONTENT_URL );
2908 if ( $path && is_string( $path ) )
2909 $url .= '/' . ltrim($path, '/');
2912 * Filter the URL to the content directory.
2916 * @param string $url The complete URL to the content directory including scheme and path.
2917 * @param string $path Path relative to the URL to the content directory. Blank string
2918 * if no path is specified.
2920 return apply_filters( 'content_url', $url, $path);
2924 * Retrieve a URL within the plugins or mu-plugins directory.
2926 * Defaults to the plugins directory URL if no arguments are supplied.
2930 * @param string $path Optional. Extra path appended to the end of the URL, including
2931 * the relative directory if $plugin is supplied. Default empty.
2932 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
2933 * The URL will be relative to its directory. Default empty.
2934 * Typically this is done by passing `__FILE__` as the argument.
2935 * @return string Plugins URL link with optional paths appended.
2937 function plugins_url( $path = '', $plugin = '' ) {
2939 $path = wp_normalize_path( $path );
2940 $plugin = wp_normalize_path( $plugin );
2941 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
2943 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2944 $url = WPMU_PLUGIN_URL;
2946 $url = WP_PLUGIN_URL;
2949 $url = set_url_scheme( $url );
2951 if ( !empty($plugin) && is_string($plugin) ) {
2952 $folder = dirname(plugin_basename($plugin));
2953 if ( '.' != $folder )
2954 $url .= '/' . ltrim($folder, '/');
2957 if ( $path && is_string( $path ) )
2958 $url .= '/' . ltrim($path, '/');
2961 * Filter the URL to the plugins directory.
2965 * @param string $url The complete URL to the plugins directory including scheme and path.
2966 * @param string $path Path relative to the URL to the plugins directory. Blank string
2967 * if no path is specified.
2968 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
2971 return apply_filters( 'plugins_url', $url, $path, $plugin );
2975 * Retrieve the site url for the current network.
2977 * Returns the site url with the appropriate protocol, 'https' if
2978 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2983 * @param string $path Optional. Path relative to the site url.
2984 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2985 * @return string Site url link with optional path appended.
2987 function network_site_url( $path = '', $scheme = null ) {
2988 if ( ! is_multisite() )
2989 return site_url($path, $scheme);
2991 $current_site = get_current_site();
2993 if ( 'relative' == $scheme )
2994 $url = $current_site->path;
2996 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2998 if ( $path && is_string( $path ) )
2999 $url .= ltrim( $path, '/' );
3002 * Filter the network site URL.
3006 * @param string $url The complete network site URL including scheme and path.
3007 * @param string $path Path relative to the network site URL. Blank string if
3008 * no path is specified.
3009 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3010 * 'relative' or null.
3012 return apply_filters( 'network_site_url', $url, $path, $scheme );
3016 * Retrieves the home url for the current network.
3018 * Returns the home url with the appropriate protocol, 'https' {@see is_ssl()}
3019 * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3024 * @param string $path Optional. Path relative to the home url. Default empty.
3025 * @param string $scheme Optional. Scheme to give the home url context. Accepts
3026 * 'http', 'https', or 'relative'. Default null.
3027 * @return string Home url link with optional path appended.
3029 function network_home_url( $path = '', $scheme = null ) {
3030 if ( ! is_multisite() )
3031 return home_url($path, $scheme);
3033 $current_site = get_current_site();
3034 $orig_scheme = $scheme;
3036 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
3037 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3039 if ( 'relative' == $scheme )
3040 $url = $current_site->path;
3042 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3044 if ( $path && is_string( $path ) )
3045 $url .= ltrim( $path, '/' );
3048 * Filter the network home URL.
3052 * @param string $url The complete network home URL including scheme and path.
3053 * @param string $path Path relative to the network home URL. Blank string
3054 * if no path is specified.
3055 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3056 * 'relative' or null.
3058 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3062 * Retrieve the url to the admin area for the network.
3066 * @param string $path Optional path relative to the admin url.
3067 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3068 * @return string Admin url link with optional path appended.
3070 function network_admin_url( $path = '', $scheme = 'admin' ) {
3071 if ( ! is_multisite() )
3072 return admin_url( $path, $scheme );
3074 $url = network_site_url('wp-admin/network/', $scheme);
3076 if ( $path && is_string( $path ) )
3077 $url .= ltrim($path, '/');
3080 * Filter the network admin URL.
3084 * @param string $url The complete network admin URL including scheme and path.
3085 * @param string $path Path relative to the network admin URL. Blank string if
3086 * no path is specified.
3088 return apply_filters( 'network_admin_url', $url, $path );
3092 * Retrieve the url to the admin area for the current user.
3096 * @param string $path Optional path relative to the admin url.
3097 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3098 * @return string Admin url link with optional path appended.
3100 function user_admin_url( $path = '', $scheme = 'admin' ) {
3101 $url = network_site_url('wp-admin/user/', $scheme);
3103 if ( $path && is_string( $path ) )
3104 $url .= ltrim($path, '/');
3107 * Filter the user admin URL for the current user.
3111 * @param string $url The complete URL including scheme and path.
3112 * @param string $path Path relative to the URL. Blank string if
3113 * no path is specified.
3115 return apply_filters( 'user_admin_url', $url, $path );
3119 * Retrieve the url to the admin area for either the current blog or the network depending on context.
3123 * @param string $path Optional path relative to the admin url.
3124 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3125 * @return string Admin url link with optional path appended.
3127 function self_admin_url($path = '', $scheme = 'admin') {
3128 if ( is_network_admin() )
3129 return network_admin_url($path, $scheme);
3130 elseif ( is_user_admin() )
3131 return user_admin_url($path, $scheme);
3133 return admin_url($path, $scheme);
3137 * Set the scheme for a URL
3141 * @param string $url Absolute url that includes a scheme
3142 * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
3143 * @return string $url URL with chosen scheme.
3145 function set_url_scheme( $url, $scheme = null ) {
3146 $orig_scheme = $scheme;
3149 $scheme = is_ssl() ? 'https' : 'http';
3150 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3151 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3152 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3153 $scheme = is_ssl() ? 'https' : 'http';
3156 $url = trim( $url );
3157 if ( substr( $url, 0, 2 ) === '//' )
3158 $url = 'http:' . $url;
3160 if ( 'relative' == $scheme ) {
3161 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3162 if ( $url !== '' && $url[0] === '/' )
3163 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3165 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3169 * Filter the resulting URL after setting the scheme.
3173 * @param string $url The complete URL including scheme and path.
3174 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3175 * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3176 * 'login_post', 'admin', 'rpc', or 'relative'.
3178 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3182 * Get the URL to the user's dashboard.
3184 * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
3185 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
3186 * primary blog is returned.
3190 * @param int $user_id Optional. User ID. Defaults to current user.
3191 * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
3192 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3193 * @return string Dashboard url link with optional path appended.
3195 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3196 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3198 $blogs = get_blogs_of_user( $user_id );
3199 if ( ! is_super_admin() && empty($blogs) ) {
3200 $url = user_admin_url( $path, $scheme );
3201 } elseif ( ! is_multisite() ) {
3202 $url = admin_url( $path, $scheme );
3204 $current_blog = get_current_blog_id();
3205 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3206 $url = admin_url( $path, $scheme );
3208 $active = get_active_blog_for_user( $user_id );
3210 $url = get_admin_url( $active->blog_id, $path, $scheme );
3212 $url = user_admin_url( $path, $scheme );
3217 * Filter the dashboard URL for a user.
3221 * @param string $url The complete URL including scheme and path.
3222 * @param int $user_id The user ID.
3223 * @param string $path Path relative to the URL. Blank string if no path is specified.
3224 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3225 * 'login_post', 'admin', 'relative' or null.
3227 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3231 * Get the URL to the user's profile editor.
3235 * @param int $user_id Optional. User ID. Defaults to current user.
3236 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3237 * 'http' or 'https' can be passed to force those schemes.
3238 * @return string Dashboard url link with optional path appended.
3240 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3241 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3243 if ( is_user_admin() )
3244 $url = user_admin_url( 'profile.php', $scheme );
3245 elseif ( is_network_admin() )
3246 $url = network_admin_url( 'profile.php', $scheme );
3248 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3251 * Filter the URL for a user's profile editor.
3255 * @param string $url The complete URL including scheme and path.
3256 * @param int $user_id The user ID.
3257 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3258 * 'login_post', 'admin', 'relative' or null.
3260 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3264 * Output rel=canonical for singular queries.
3268 * @global WP_Query $wp_the_query
3270 function rel_canonical() {
3271 if ( !is_singular() )
3274 global $wp_the_query;
3275 if ( !$id = $wp_the_query->get_queried_object_id() )
3278 $link = get_permalink( $id );
3280 if ( $page = get_query_var('cpage') )
3281 $link = get_comments_pagenum_link( $page );
3283 echo "<link rel='canonical' href='$link' />\n";
3287 * Return a shortlink for a post, page, attachment, or blog.
3289 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
3290 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
3291 * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
3292 * via the get_shortlink filter.
3296 * @global WP_Query $wp_query
3298 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
3299 * @param string $context Whether the id is a 'blog' id, 'post' id, or 'media' id.
3300 * If 'post', the post_type of the post is consulted.
3301 * If 'query', the current query is consulted to determine the id and context.
3302 * Default is 'post'.
3303 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3304 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3306 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3308 * Filter whether to preempt generating a shortlink for the given post.
3310 * Passing a truthy value to the filter will effectively short-circuit the
3311 * shortlink-generation process, returning that value instead.
3315 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3316 * @param int $id Post ID, or 0 for the current post.
3317 * @param string $context The context for the link. One of 'post' or 'query',
3318 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3320 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3322 if ( false !== $shortlink )
3327 if ( 'query' == $context && is_singular() ) {
3328 $post_id = $wp_query->get_queried_object_id();
3329 $post = get_post( $post_id );
3330 } elseif ( 'post' == $context ) {
3331 $post = get_post( $id );
3332 if ( ! empty( $post->ID ) )
3333 $post_id = $post->ID;
3338 // Return p= link for all public post types.
3339 if ( ! empty( $post_id ) ) {
3340 $post_type = get_post_type_object( $post->post_type );
3342 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3343 $shortlink = home_url( '/' );
3344 } elseif ( $post_type->public ) {
3345 $shortlink = home_url( '?p=' . $post_id );
3350 * Filter the shortlink for a post.
3354 * @param string $shortlink Shortlink URL.
3355 * @param int $id Post ID, or 0 for the current post.
3356 * @param string $context The context for the link. One of 'post' or 'query',
3357 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3359 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3363 * Inject rel=shortlink into head if a shortlink is defined for the current page.
3365 * Attached to the wp_head action.
3369 function wp_shortlink_wp_head() {
3370 $shortlink = wp_get_shortlink( 0, 'query' );
3372 if ( empty( $shortlink ) )
3375 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3379 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3381 * Attached to the wp action.
3385 function wp_shortlink_header() {
3386 if ( headers_sent() )
3389 $shortlink = wp_get_shortlink(0, 'query');
3391 if ( empty($shortlink) )
3394 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3398 * Display the Short Link for a Post
3400 * Must be called from inside "The Loop"
3402 * Call like the_shortlink(__('Shortlinkage FTW'))
3406 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3407 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3408 * @param string $before Optional HTML to display before the link.
3409 * @param string $after Optional HTML to display after the link.
3411 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3414 if ( empty( $text ) )
3415 $text = __('This is the short link.');
3417 if ( empty( $title ) )
3418 $title = the_title_attribute( array( 'echo' => false ) );
3420 $shortlink = wp_get_shortlink( $post->ID );
3422 if ( !empty( $shortlink ) ) {
3423 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3426 * Filter the shortlink anchor tag for a post.
3430 * @param string $link Shortlink anchor tag.
3431 * @param string $shortlink Shortlink URL.
3432 * @param string $text Shortlink's text.
3433 * @param string $title Shortlink's title attribute.
3435 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3436 echo $before, $link, $after;
3442 * Retrieve the avatar URL.
3446 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3447 * user email, WP_User object, WP_Post object, or comment object.
3448 * @param array $args {
3449 * Optional. Arguments to return instead of the default arguments.
3451 * @type int $size Height and width of the avatar in pixels. Default 96.
3452 * @type string $default URL for the default image or a default type. Accepts '404' (return
3453 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3454 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3455 * or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
3456 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3457 * 'avatar_default' option, with a fallback of 'mystery'.
3458 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3459 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3460 * judged in that order. Default is the value of the 'avatar_rating' option.
3461 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3463 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3464 * plus a "found_avatar" guess. Pass as a reference. Default null.
3466 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3468 function get_avatar_url( $id_or_email, $args = null ) {
3469 $args = get_avatar_data( $id_or_email, $args );
3470 return $args['url'];
3474 * Retrieve default data about the avatar.
3478 * @param mixed $id_or_email The Gravatar to check the data against. Accepts a user_id, gravatar md5 hash,
3479 * user email, WP_User object, WP_Post object, or comment object.
3480 * @param array $args {
3481 * Optional. Arguments to return instead of the default arguments.
3483 * @type int $size Height and width of the avatar image file in pixels. Default 96.
3484 * @type int $height Display height of the avatar in pixels. Defaults to $size.
3485 * @type int $width Display width of the avatar in pixels. Defaults to $size.
3486 * @type string $default URL for the default image or a default type. Accepts '404' (return
3487 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3488 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3489 * or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
3490 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3491 * 'avatar_default' option, with a fallback of 'mystery'.
3492 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3493 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3494 * judged in that order. Default is the value of the 'avatar_rating' option.
3495 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3497 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3498 * plus a "found_avatar" guess. Pass as a reference. Default null.
3499 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3501 * @return array $processed_args {
3502 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3504 * @type bool $found_avatar True if we were able to find an avatar for this user,
3505 * false or not set if we couldn't.
3506 * @type string $url The URL of the avatar we found.
3509 function get_avatar_data( $id_or_email, $args = null ) {
3510 $args = wp_parse_args( $args, array(
3514 'default' => get_option( 'avatar_default', 'mystery' ),
3515 'force_default' => false,
3516 'rating' => get_option( 'avatar_rating' ),
3518 'processed_args' => null, // if used, should be a reference
3522 if ( is_numeric( $args['size'] ) ) {
3523 $args['size'] = absint( $args['size'] );
3524 if ( ! $args['size'] ) {
3531 if ( is_numeric( $args['height'] ) ) {
3532 $args['height'] = absint( $args['height'] );
3533 if ( ! $args['height'] ) {
3534 $args['height'] = $args['size'];
3537 $args['height'] = $args['size'];
3540 if ( is_numeric( $args['width'] ) ) {
3541 $args['width'] = absint( $args['width'] );
3542 if ( ! $args['width'] ) {
3543 $args['width'] = $args['size'];
3546 $args['width'] = $args['size'];
3549 if ( empty( $args['default'] ) ) {
3550 $args['default'] = get_option( 'avatar_default', 'mystery' );
3553 switch ( $args['default'] ) {
3557 $args['default'] = 'mm';
3559 case 'gravatar_default' :
3560 $args['default'] = false;
3564 $args['force_default'] = (bool) $args['force_default'];
3566 $args['rating'] = strtolower( $args['rating'] );
3568 $args['found_avatar'] = false;
3571 * Filter whether to retrieve the avatar URL early.
3573 * Passing a non-null value in the 'url' member of the return array will
3574 * effectively short circuit get_avatar_data(), passing the value through
3575 * the {@see 'get_avatar_data'} filter and returning early.
3579 * @param array $args Arguments passed to get_avatar_data(), after processing.
3580 * @param int|object|string $id_or_email A user ID, email address, or comment object.
3582 $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3584 if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3585 /** This filter is documented in wp-includes/link-template.php */
3586 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3590 $user = $email = false;
3592 // Process the user identifier.
3593 if ( is_numeric( $id_or_email ) ) {
3594 $user = get_user_by( 'id', absint( $id_or_email ) );
3595 } elseif ( is_string( $id_or_email ) ) {
3596 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3598 list( $email_hash ) = explode( '@', $id_or_email );
3601 $email = $id_or_email;
3603 } elseif ( $id_or_email instanceof WP_User ) {
3605 $user = $id_or_email;
3606 } elseif ( $id_or_email instanceof WP_Post ) {
3608 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3609 } elseif ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3613 * Filter the list of allowed comment types for retrieving avatars.
3617 * @param array $types An array of content types. Default only contains 'comment'.
3619 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3620 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3621 $args['url'] = false;
3622 /** This filter is documented in wp-includes/link-template.php */
3623 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3626 if ( ! empty( $id_or_email->user_id ) ) {
3627 $user = get_user_by( 'id', (int) $id_or_email->user_id );
3629 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3630 $email = $id_or_email->comment_author_email;
3634 if ( ! $email_hash ) {
3636 $email = $user->user_email;
3640 $email_hash = md5( strtolower( trim( $email ) ) );
3644 if ( $email_hash ) {
3645 $args['found_avatar'] = true;
3646 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3648 $gravatar_server = rand( 0, 2 );
3652 's' => $args['size'],
3653 'd' => $args['default'],
3654 'f' => $args['force_default'] ? 'y' : false,
3655 'r' => $args['rating'],
3659 $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
3661 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
3664 $url = add_query_arg(
3665 rawurlencode_deep( array_filter( $url_args ) ),
3666 set_url_scheme( $url, $args['scheme'] )
3670 * Filter the avatar URL.
3674 * @param string $url The URL of the avatar.
3675 * @param int|object|string $id_or_email A user ID, email address, or comment object.
3676 * @param array $args Arguments passed to get_avatar_data(), after processing.
3678 $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
3681 * Filter the avatar data.
3685 * @param array $args Arguments passed to get_avatar_data(), after processing.
3686 * @param int|object|string $id_or_email A user ID, email address, or comment object.
3688 return apply_filters( 'get_avatar_data', $args, $id_or_email );