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.
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.
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 $string = apply_filters( 'user_trailingslashit', $string, $type_of_url );
63 * Display permalink anchor for current post.
65 * The permalink mode title will use the post title for the 'a' element 'id'
66 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
70 * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
72 function permalink_anchor( $mode = 'id' ) {
74 switch ( strtolower( $mode ) ) {
76 $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
77 echo '<a id="'.$title.'"></a>';
81 echo '<a id="post-' . $post->ID . '"></a>';
87 * Retrieve full permalink for current post or post ID.
89 * This function is an alias for get_permalink().
93 * @see get_permalink()
95 * @param int|WP_Post $id Optional. Post ID or post object. Default is the current post.
96 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
97 * @return string|bool The permalink URL or false if post does not exist.
99 function get_the_permalink( $id = 0, $leavename = false ) {
100 return get_permalink( $id, $leavename );
104 * Retrieve full permalink for current post or post ID.
108 * @param int|WP_Post $id Optional. Post ID or post object. Default current post.
109 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
110 * @return string|bool The permalink URL or false if post does not exist.
112 function get_permalink( $id = 0, $leavename = false ) {
113 $rewritecode = array(
120 $leavename? '' : '%postname%',
124 $leavename? '' : '%pagename%',
127 if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
131 $post = get_post($id);
135 if ( empty($post->ID) )
138 if ( $post->post_type == 'page' )
139 return get_page_link($post, $leavename, $sample);
140 elseif ( $post->post_type == 'attachment' )
141 return get_attachment_link( $post, $leavename );
142 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
143 return get_post_permalink($post, $leavename, $sample);
145 $permalink = get_option('permalink_structure');
148 * Filter the permalink structure for a post before token replacement occurs.
150 * Only applies to posts with post_type of 'post'.
154 * @param string $permalink The site's permalink structure.
155 * @param WP_Post $post The post in question.
156 * @param bool $leavename Whether to keep the post name.
158 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
160 if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
161 $unixtime = strtotime($post->post_date);
164 if ( strpos($permalink, '%category%') !== false ) {
165 $cats = get_the_category($post->ID);
167 usort($cats, '_usort_terms_by_ID'); // order by ID
170 * Filter the category that gets used in the %category% permalink token.
174 * @param stdClass $cat The category to use in the permalink.
175 * @param array $cats Array of all categories associated with the post.
176 * @param WP_Post $post The post in question.
178 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
180 $category_object = get_term( $category_object, 'category' );
181 $category = $category_object->slug;
182 if ( $parent = $category_object->parent )
183 $category = get_category_parents($parent, false, '/', true) . $category;
185 // show default category in permalinks, without
186 // having to assign it explicitly
187 if ( empty($category) ) {
188 $default_category = get_term( get_option( 'default_category' ), 'category' );
189 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
194 if ( strpos($permalink, '%author%') !== false ) {
195 $authordata = get_userdata($post->post_author);
196 $author = $authordata->user_nicename;
199 $date = explode(" ",date('Y m d H i s', $unixtime));
214 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
215 $permalink = user_trailingslashit($permalink, 'single');
216 } else { // if they're not using the fancy permalink option
217 $permalink = home_url('?p=' . $post->ID);
221 * Filter the permalink for a post.
223 * Only applies to posts with post_type of 'post'.
227 * @param string $permalink The post's permalink.
228 * @param WP_Post $post The post in question.
229 * @param bool $leavename Whether to keep the post name.
231 return apply_filters( 'post_link', $permalink, $post, $leavename );
235 * Retrieve the permalink for a post with a custom post type.
239 * @param int $id Optional. Post ID.
240 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
241 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
244 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
247 $post = get_post($id);
249 if ( is_wp_error( $post ) )
252 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
254 $slug = $post->post_name;
256 $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
258 $post_type = get_post_type_object($post->post_type);
260 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
261 if ( ! $leavename ) {
262 if ( $post_type->hierarchical )
263 $slug = get_page_uri($id);
264 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
266 $post_link = home_url( user_trailingslashit($post_link) );
268 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
269 $post_link = add_query_arg($post_type->query_var, $slug, '');
271 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
272 $post_link = home_url($post_link);
276 * Filter the permalink for a post with a custom post type.
280 * @param string $post_link The post's permalink.
281 * @param WP_Post $post The post in question.
282 * @param bool $leavename Whether to keep the post name.
283 * @param bool $sample Is it a sample permalink.
285 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
289 * Retrieve permalink from post ID.
293 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
294 * @param mixed $deprecated Not used.
297 function post_permalink( $post_id = 0, $deprecated = '' ) {
298 if ( !empty( $deprecated ) )
299 _deprecated_argument( __FUNCTION__, '1.3' );
301 return get_permalink($post_id);
305 * Retrieve the permalink for current page or page ID.
307 * Respects page_on_front. Use this one.
311 * @param int|object $post Optional. Post ID or object.
312 * @param bool $leavename Optional, defaults to false. Whether to keep page name.
313 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
316 function get_page_link( $post = false, $leavename = false, $sample = false ) {
317 $post = get_post( $post );
319 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
320 $link = home_url('/');
322 $link = _get_page_link( $post, $leavename, $sample );
325 * Filter the permalink for a page.
329 * @param string $link The page's permalink.
330 * @param int $post_id The ID of the page.
331 * @param bool $sample Is it a sample permalink.
333 return apply_filters( 'page_link', $link, $post->ID, $sample );
337 * Retrieve the page permalink.
339 * Ignores page_on_front. Internal use only.
344 * @param int|object $post Optional. Post ID or object.
345 * @param bool $leavename Optional. Leave name.
346 * @param bool $sample Optional. Sample permalink.
349 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
352 $post = get_post( $post );
354 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
356 $link = $wp_rewrite->get_page_permastruct();
358 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
359 if ( ! $leavename ) {
360 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
363 $link = home_url($link);
364 $link = user_trailingslashit($link, 'page');
366 $link = home_url( '?page_id=' . $post->ID );
370 * Filter the permalink for a non-page_on_front page.
374 * @param string $link The page's permalink.
375 * @param int $post_id The ID of the page.
377 return apply_filters( '_get_page_link', $link, $post->ID );
381 * Retrieve permalink for attachment.
383 * This can be used in the WordPress Loop or outside of it.
387 * @param int|object $post Optional. Post ID or object.
388 * @param bool $leavename Optional. Leave name.
391 function get_attachment_link( $post = null, $leavename = false ) {
396 $post = get_post( $post );
397 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
399 if ( $wp_rewrite->using_permalinks() && $parent ) {
400 if ( 'page' == $parent->post_type )
401 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
403 $parentlink = get_permalink( $post->post_parent );
405 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
406 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
408 $name = $post->post_name;
410 if ( strpos($parentlink, '?') === false )
411 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
414 $link = str_replace( '%postname%', $name, $link );
418 $link = home_url( '/?attachment_id=' . $post->ID );
421 * Filter the permalink for an attachment.
425 * @param string $link The attachment's permalink.
426 * @param int $post_id Attachment ID.
428 return apply_filters( 'attachment_link', $link, $post->ID );
432 * Retrieve the permalink for the year archives.
436 * @param int|bool $year False for current year or year for permalink.
439 function get_year_link($year) {
442 $year = gmdate('Y', current_time('timestamp'));
443 $yearlink = $wp_rewrite->get_year_permastruct();
444 if ( !empty($yearlink) ) {
445 $yearlink = str_replace('%year%', $year, $yearlink);
446 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
448 $yearlink = home_url( '?m=' . $year );
452 * Filter the year archive permalink.
456 * @param string $yearlink Permalink for the year archive.
457 * @param int $year Year for the archive.
459 return apply_filters( 'year_link', $yearlink, $year );
463 * Retrieve the permalink for the month archives with year.
467 * @param bool|int $year False for current year. Integer of year.
468 * @param bool|int $month False for current month. Integer of month.
471 function get_month_link($year, $month) {
474 $year = gmdate('Y', current_time('timestamp'));
476 $month = gmdate('m', current_time('timestamp'));
477 $monthlink = $wp_rewrite->get_month_permastruct();
478 if ( !empty($monthlink) ) {
479 $monthlink = str_replace('%year%', $year, $monthlink);
480 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
481 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
483 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
487 * Filter the month archive permalink.
491 * @param string $monthlink Permalink for the month archive.
492 * @param int $year Year for the archive.
493 * @param int $month The month for the archive.
495 return apply_filters( 'month_link', $monthlink, $year, $month );
499 * Retrieve the permalink for the day archives with year and month.
503 * @param bool|int $year False for current year. Integer of year.
504 * @param bool|int $month False for current month. Integer of month.
505 * @param bool|int $day False for current day. Integer of day.
508 function get_day_link($year, $month, $day) {
511 $year = gmdate('Y', current_time('timestamp'));
513 $month = gmdate('m', current_time('timestamp'));
515 $day = gmdate('j', current_time('timestamp'));
517 $daylink = $wp_rewrite->get_day_permastruct();
518 if ( !empty($daylink) ) {
519 $daylink = str_replace('%year%', $year, $daylink);
520 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
521 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
522 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
524 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
528 * Filter the day archive permalink.
532 * @param string $daylink Permalink for the day archive.
533 * @param int $year Year for the archive.
534 * @param int $month Month for the archive.
535 * @param int $day The day for the archive.
537 return apply_filters( 'day_link', $daylink, $year, $month, $day );
541 * Display the permalink for the feed type.
545 * @param string $anchor The link's anchor text.
546 * @param string $feed Optional, defaults to default feed. Feed type.
548 function the_feed_link( $anchor, $feed = '' ) {
549 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
552 * Filter the feed link anchor tag.
556 * @param string $link The complete anchor tag for a feed link.
557 * @param string $feed The feed type, or an empty string for the
560 echo apply_filters( 'the_feed_link', $link, $feed );
564 * Retrieve the permalink for the feed type.
568 * @param string $feed Optional, defaults to default feed. Feed type.
571 function get_feed_link($feed = '') {
574 $permalink = $wp_rewrite->get_feed_permastruct();
575 if ( '' != $permalink ) {
576 if ( false !== strpos($feed, 'comments_') ) {
577 $feed = str_replace('comments_', '', $feed);
578 $permalink = $wp_rewrite->get_comment_feed_permastruct();
581 if ( get_default_feed() == $feed )
584 $permalink = str_replace('%feed%', $feed, $permalink);
585 $permalink = preg_replace('#/+#', '/', "/$permalink");
586 $output = home_url( user_trailingslashit($permalink, 'feed') );
589 $feed = get_default_feed();
591 if ( false !== strpos($feed, 'comments_') )
592 $feed = str_replace('comments_', 'comments-', $feed);
594 $output = home_url("?feed={$feed}");
598 * Filter the feed type permalink.
602 * @param string $output The feed permalink.
603 * @param string $feed Feed type.
605 return apply_filters( 'feed_link', $output, $feed );
609 * Retrieve the permalink for the post comments feed.
613 * @param int $post_id Optional. Post ID.
614 * @param string $feed Optional. Feed type.
617 function get_post_comments_feed_link($post_id = 0, $feed = '') {
618 $post_id = absint( $post_id );
621 $post_id = get_the_ID();
623 if ( empty( $feed ) )
624 $feed = get_default_feed();
626 if ( '' != get_option('permalink_structure') ) {
627 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
628 $url = _get_page_link( $post_id );
630 $url = get_permalink($post_id);
632 $url = trailingslashit($url) . 'feed';
633 if ( $feed != get_default_feed() )
635 $url = user_trailingslashit($url, 'single_feed');
637 $type = get_post_field('post_type', $post_id);
638 if ( 'page' == $type )
639 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
641 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
645 * Filter the post comments feed permalink.
649 * @param string $url Post comments feed permalink.
651 return apply_filters( 'post_comments_feed_link', $url );
655 * Display the comment feed link for a post.
657 * Prints out the comment feed link for a post. Link text is placed in the
658 * anchor. If no link text is specified, default text is used. If no post ID is
659 * specified, the current post is used.
663 * @param string $link_text Descriptive text.
664 * @param int $post_id Optional post ID. Default to current post.
665 * @param string $feed Optional. Feed format.
666 * @return string Link to the comment feed for the current post.
668 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
669 $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
670 if ( empty($link_text) )
671 $link_text = __('Comments Feed');
674 * Filter the post comment feed link anchor tag.
678 * @param string $link The complete anchor tag for the comment feed link.
679 * @param int $post_id Post ID.
680 * @param string $feed The feed type, or an empty string for the default feed type.
682 echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
686 * Retrieve the feed link for a given author.
688 * Returns a link to the feed for all posts by a given author. A specific feed
689 * can be requested or left blank to get the default feed.
693 * @param int $author_id ID of an author.
694 * @param string $feed Optional. Feed type.
695 * @return string Link to the feed for the author specified by $author_id.
697 function get_author_feed_link( $author_id, $feed = '' ) {
698 $author_id = (int) $author_id;
699 $permalink_structure = get_option('permalink_structure');
702 $feed = get_default_feed();
704 if ( '' == $permalink_structure ) {
705 $link = home_url("?feed=$feed&author=" . $author_id);
707 $link = get_author_posts_url($author_id);
708 if ( $feed == get_default_feed() )
711 $feed_link = "feed/$feed";
713 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
717 * Filter the feed link for a given author.
721 * @param string $link The author feed link.
722 * @param string $feed Feed type.
724 $link = apply_filters( 'author_feed_link', $link, $feed );
730 * Retrieve the feed link for a category.
732 * Returns a link to the feed for all posts in a given category. A specific feed
733 * can be requested or left blank to get the default feed.
737 * @param int $cat_id ID of a category.
738 * @param string $feed Optional. Feed type.
739 * @return string Link to the feed for the category specified by $cat_id.
741 function get_category_feed_link($cat_id, $feed = '') {
742 return get_term_feed_link($cat_id, 'category', $feed);
746 * Retrieve the feed link for a term.
748 * Returns a link to the feed for all posts in a given term. A specific feed
749 * can be requested or left blank to get the default feed.
753 * @param int $term_id ID of a category.
754 * @param string $taxonomy Optional. Taxonomy of $term_id
755 * @param string $feed Optional. Feed type.
756 * @return string Link to the feed for the term specified by $term_id and $taxonomy.
758 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
759 $term_id = ( int ) $term_id;
761 $term = get_term( $term_id, $taxonomy );
763 if ( empty( $term ) || is_wp_error( $term ) )
766 if ( empty( $feed ) )
767 $feed = get_default_feed();
769 $permalink_structure = get_option( 'permalink_structure' );
771 if ( '' == $permalink_structure ) {
772 if ( 'category' == $taxonomy ) {
773 $link = home_url("?feed=$feed&cat=$term_id");
775 elseif ( 'post_tag' == $taxonomy ) {
776 $link = home_url("?feed=$feed&tag=$term->slug");
778 $t = get_taxonomy( $taxonomy );
779 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
782 $link = get_term_link( $term_id, $term->taxonomy );
783 if ( $feed == get_default_feed() )
786 $feed_link = "feed/$feed";
788 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
791 if ( 'category' == $taxonomy ) {
793 * Filter the category feed link.
797 * @param string $link The category feed link.
798 * @param string $feed Feed type.
800 $link = apply_filters( 'category_feed_link', $link, $feed );
801 } elseif ( 'post_tag' == $taxonomy ) {
803 * Filter the post tag feed link.
807 * @param string $link The tag feed link.
808 * @param string $feed Feed type.
810 $link = apply_filters( 'tag_feed_link', $link, $feed );
813 * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
817 * @param string $link The taxonomy feed link.
818 * @param string $feed Feed type.
819 * @param string $feed The taxonomy name.
821 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
828 * Retrieve permalink for feed of tag.
832 * @param int $tag_id Tag ID.
833 * @param string $feed Optional. Feed type.
836 function get_tag_feed_link($tag_id, $feed = '') {
837 return get_term_feed_link($tag_id, 'post_tag', $feed);
841 * Retrieve edit tag link.
845 * @param int $tag_id Tag ID
846 * @param string $taxonomy Taxonomy
849 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
851 * Filter the edit link for a tag (or term in another taxonomy).
855 * @param string $link The term edit link.
857 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
861 * Display or retrieve edit tag link with formatting.
865 * @param string $link Optional. Anchor text.
866 * @param string $before Optional. Display before edit link.
867 * @param string $after Optional. Display after edit link.
868 * @param object $tag Tag object.
869 * @return string HTML content.
871 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
872 $link = edit_term_link( $link, '', '', $tag, false );
875 * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
879 * @param string $link The anchor tag for the edit link.
881 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
885 * Retrieve edit term url.
889 * @param int $term_id Term ID
890 * @param string $taxonomy Taxonomy
891 * @param string $object_type The object type
894 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
895 $tax = get_taxonomy( $taxonomy );
896 if ( !current_user_can( $tax->cap->edit_terms ) )
899 $term = get_term( $term_id, $taxonomy );
903 'taxonomy' => $taxonomy,
904 'tag_ID' => $term->term_id,
908 $args['post_type'] = $object_type;
910 $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
913 * Filter the edit link for a term.
917 * @param string $location The edit link.
918 * @param int $term_id Term ID.
919 * @param string $taxonomy Taxonomy name.
920 * @param string $object_type The object type (eg. the post type).
922 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
926 * Display or retrieve edit term link with formatting.
930 * @param string $link Optional. Anchor text.
931 * @param string $before Optional. Display before edit link.
932 * @param string $after Optional. Display after edit link.
933 * @param object $term Term object.
934 * @return string HTML content.
936 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
937 if ( is_null( $term ) )
938 $term = get_queried_object();
943 $tax = get_taxonomy( $term->taxonomy );
944 if ( ! current_user_can( $tax->cap->edit_terms ) )
947 if ( empty( $link ) )
948 $link = __('Edit This');
950 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
953 * Filter the anchor tag for the edit link of a term.
957 * @param string $link The anchor tag for the edit link.
958 * @param int $term_id Term ID.
960 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
969 * Retrieve permalink for search.
973 * @param string $query Optional. The query string to use. If empty the current query is used.
976 function get_search_link( $query = '' ) {
980 $search = get_search_query( false );
982 $search = stripslashes($query);
984 $permastruct = $wp_rewrite->get_search_permastruct();
986 if ( empty( $permastruct ) ) {
987 $link = home_url('?s=' . urlencode($search) );
989 $search = urlencode($search);
990 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
991 $link = str_replace( '%search%', $search, $permastruct );
992 $link = home_url( user_trailingslashit( $link, 'search' ) );
996 * Filter the search permalink.
1000 * @param string $link Search permalink.
1001 * @param string $search The URL-encoded search term.
1003 return apply_filters( 'search_link', $link, $search );
1007 * Retrieve the permalink for the feed of the search results.
1011 * @param string $search_query Optional. Search query.
1012 * @param string $feed Optional. Feed type.
1015 function get_search_feed_link($search_query = '', $feed = '') {
1017 $link = get_search_link($search_query);
1020 $feed = get_default_feed();
1022 $permastruct = $wp_rewrite->get_search_permastruct();
1024 if ( empty($permastruct) ) {
1025 $link = add_query_arg('feed', $feed, $link);
1027 $link = trailingslashit($link);
1028 $link .= "feed/$feed/";
1032 * Filter the search feed link.
1036 * @param string $link Search feed link.
1037 * @param string $feed Feed type.
1038 * @param string $type The search type. One of 'posts' or 'comments'.
1040 $link = apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1046 * Retrieve the permalink for the comments feed of the search results.
1050 * @param string $search_query Optional. Search query.
1051 * @param string $feed Optional. Feed type.
1054 function get_search_comments_feed_link($search_query = '', $feed = '') {
1058 $feed = get_default_feed();
1060 $link = get_search_feed_link($search_query, $feed);
1062 $permastruct = $wp_rewrite->get_search_permastruct();
1064 if ( empty($permastruct) )
1065 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1067 $link = add_query_arg('withcomments', 1, $link);
1069 /** This filter is documented in wp-includes/link-template.php */
1070 $link = apply_filters('search_feed_link', $link, $feed, 'comments');
1076 * Retrieve the permalink for a post type archive.
1080 * @param string $post_type Post type
1083 function get_post_type_archive_link( $post_type ) {
1085 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1088 if ( ! $post_type_obj->has_archive )
1091 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1092 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1093 if ( $post_type_obj->rewrite['with_front'] )
1094 $struct = $wp_rewrite->front . $struct;
1096 $struct = $wp_rewrite->root . $struct;
1097 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1099 $link = home_url( '?post_type=' . $post_type );
1103 * Filter the post type archive permalink.
1107 * @param string $link The post type archive permalink.
1108 * @param string $post_type Post type name.
1110 return apply_filters( 'post_type_archive_link', $link, $post_type );
1114 * Retrieve the permalink for a post type archive feed.
1118 * @param string $post_type Post type
1119 * @param string $feed Optional. Feed type
1122 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1123 $default_feed = get_default_feed();
1124 if ( empty( $feed ) )
1125 $feed = $default_feed;
1127 if ( ! $link = get_post_type_archive_link( $post_type ) )
1130 $post_type_obj = get_post_type_object( $post_type );
1131 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1132 $link = trailingslashit( $link );
1134 if ( $feed != $default_feed )
1137 $link = add_query_arg( 'feed', $feed, $link );
1141 * Filter the post type archive feed link.
1145 * @param string $link The post type archive feed link.
1146 * @param string $feed Feed type.
1148 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1152 * Retrieve edit posts link for post.
1154 * Can be used within the WordPress loop or outside of it. Can be used with
1155 * pages, posts, attachments, and revisions.
1159 * @param int $id Optional. Post ID.
1160 * @param string $context Optional, defaults to display. How to write the '&', defaults to '&'.
1163 function get_edit_post_link( $id = 0, $context = 'display' ) {
1164 if ( ! $post = get_post( $id ) )
1167 if ( 'revision' === $post->post_type )
1169 elseif ( 'display' == $context )
1170 $action = '&action=edit';
1172 $action = '&action=edit';
1174 $post_type_object = get_post_type_object( $post->post_type );
1175 if ( !$post_type_object )
1178 if ( !current_user_can( 'edit_post', $post->ID ) )
1182 * Filter the post edit link.
1186 * @param string $link The edit link.
1187 * @param int $post_id Post ID.
1188 * @param string $context The link context. If set to 'display' then ampersands
1191 return apply_filters( 'get_edit_post_link', admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ), $post->ID, $context );
1195 * Display edit post link for post.
1199 * @param string $text Optional. Anchor text.
1200 * @param string $before Optional. Display before edit link.
1201 * @param string $after Optional. Display after edit link.
1202 * @param int $id Optional. Post ID.
1204 function edit_post_link( $text = null, $before = '', $after = '', $id = 0 ) {
1205 if ( ! $post = get_post( $id ) ) {
1209 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1213 if ( null === $text ) {
1214 $text = __( 'Edit This' );
1217 $link = '<a class="post-edit-link" href="' . $url . '">' . $text . '</a>';
1220 * Filter the post edit link anchor tag.
1224 * @param string $link Anchor tag for the edit link.
1225 * @param int $post_id Post ID.
1226 * @param string $text Anchor text.
1228 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1232 * Retrieve delete posts link for post.
1234 * Can be used within the WordPress loop or outside of it, with any post type.
1238 * @param int $id Optional. Post ID.
1239 * @param string $deprecated Not used.
1240 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1243 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1244 if ( ! empty( $deprecated ) )
1245 _deprecated_argument( __FUNCTION__, '3.0' );
1247 if ( !$post = get_post( $id ) )
1250 $post_type_object = get_post_type_object( $post->post_type );
1251 if ( !$post_type_object )
1254 if ( !current_user_can( 'delete_post', $post->ID ) )
1257 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1259 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1262 * Filter the post delete link.
1266 * @param string $link The delete link.
1267 * @param int $post_id Post ID.
1268 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1270 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1274 * Retrieve edit comment link.
1278 * @param int $comment_id Optional. Comment ID.
1281 function get_edit_comment_link( $comment_id = 0 ) {
1282 $comment = get_comment( $comment_id );
1284 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1287 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1290 * Filter the comment edit link.
1294 * @param string $location The edit link.
1296 return apply_filters( 'get_edit_comment_link', $location );
1300 * Display edit comment link with formatting.
1304 * @param string $text Optional. Anchor text.
1305 * @param string $before Optional. Display before edit link.
1306 * @param string $after Optional. Display after edit link.
1308 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1311 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1315 if ( null === $text ) {
1316 $text = __( 'Edit This' );
1319 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $text . '</a>';
1322 * Filter the comment edit link anchor tag.
1326 * @param string $link Anchor tag for the edit link.
1327 * @param int $comment_id Comment ID.
1328 * @param string $text Anchor text.
1330 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1334 * Display edit bookmark (literally a URL external to blog) link.
1338 * @param int $link Optional. Bookmark ID.
1341 function get_edit_bookmark_link( $link = 0 ) {
1342 $link = get_bookmark( $link );
1344 if ( !current_user_can('manage_links') )
1347 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1350 * Filter the bookmark (link) edit link.
1354 * @param string $location The edit link.
1355 * @param int $link_id Bookmark ID.
1357 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1361 * Display edit bookmark (literally a URL external to blog) link anchor content.
1365 * @param string $link Optional. Anchor text.
1366 * @param string $before Optional. Display before edit link.
1367 * @param string $after Optional. Display after edit link.
1368 * @param int $bookmark Optional. Bookmark ID.
1370 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1371 $bookmark = get_bookmark($bookmark);
1373 if ( !current_user_can('manage_links') )
1377 $link = __('Edit This');
1379 $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
1382 * Filter the bookmark edit link anchor tag.
1386 * @param string $link Anchor tag for the edit link.
1387 * @param int $link_id Bookmark ID.
1389 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1393 * Retrieve edit user link
1397 * @param int $user_id Optional. User ID. Defaults to the current user.
1398 * @return string URL to edit user page or empty string.
1400 function get_edit_user_link( $user_id = null ) {
1402 $user_id = get_current_user_id();
1404 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1407 $user = get_userdata( $user_id );
1412 if ( get_current_user_id() == $user->ID )
1413 $link = get_edit_profile_url( $user->ID );
1415 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1418 * Filter the user edit link.
1422 * @param string $link The edit link.
1423 * @param int $user_id User ID.
1425 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1431 * Retrieve previous post that is adjacent to current post.
1435 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1436 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1437 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1438 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1440 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1441 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1445 * Retrieve next post that is adjacent to current post.
1449 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1450 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1451 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1452 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1454 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1455 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1459 * Retrieve adjacent post.
1461 * Can either be next or previous post.
1465 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1466 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1467 * @param bool $previous Optional. Whether to retrieve previous post.
1468 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1469 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1471 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1474 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1477 $current_post_date = $post->post_date;
1482 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1483 $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";
1484 $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1486 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1487 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1488 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1489 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1490 $excluded_terms = explode( ' and ', $excluded_terms );
1492 $excluded_terms = explode( ',', $excluded_terms );
1495 $excluded_terms = array_map( 'intval', $excluded_terms );
1498 if ( $in_same_term ) {
1499 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1501 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1503 // Remove any exclusions from the term array to include.
1504 $term_array = array_diff( $term_array, (array) $excluded_terms );
1505 $term_array = array_map( 'intval', $term_array );
1507 if ( ! $term_array || is_wp_error( $term_array ) )
1510 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1513 if ( ! empty( $excluded_terms ) ) {
1514 $where .= " AND p.ID NOT IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . implode( $excluded_terms, ',' ) . ') )';
1518 $adjacent = $previous ? 'previous' : 'next';
1519 $op = $previous ? '<' : '>';
1520 $order = $previous ? 'DESC' : 'ASC';
1523 * Filter the JOIN clause in the SQL for an adjacent post query.
1525 * The dynamic portion of the hook name, $adjacent, refers to the type
1526 * of adjacency, 'next' or 'previous'.
1530 * @param string $join The JOIN clause in the SQL.
1531 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1532 * @param array $excluded_terms Array of excluded term IDs.
1534 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
1537 * Filter the WHERE clause in the SQL for an adjacent post query.
1539 * The dynamic portion of the hook name, $adjacent, refers to the type
1540 * of adjacency, 'next' or 'previous'.
1544 * @param string $where The WHERE clause in the SQL.
1545 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1546 * @param array $excluded_terms Array of excluded term IDs.
1548 $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
1551 * Filter the ORDER BY clause in the SQL for an adjacent post query.
1553 * The dynamic portion of the hook name, $adjacent, refers to the type
1554 * of adjacency, 'next' or 'previous'.
1558 * @param string $order_by The ORDER BY clause in the SQL.
1560 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1562 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1563 $query_key = 'adjacent_post_' . md5( $query );
1564 $result = wp_cache_get( $query_key, 'counts' );
1565 if ( false !== $result ) {
1567 $result = get_post( $result );
1571 $result = $wpdb->get_var( $query );
1572 if ( null === $result )
1575 wp_cache_set( $query_key, $result, 'counts' );
1578 $result = get_post( $result );
1584 * Get adjacent post relational link.
1586 * Can either be next or previous post relational link.
1590 * @param string $title Optional. Link title format.
1591 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1592 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1593 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1594 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1597 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1598 if ( $previous && is_attachment() && $post = get_post() )
1599 $post = get_post( $post->post_parent );
1601 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1603 if ( empty( $post ) )
1606 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1608 if ( empty( $post_title ) )
1609 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1611 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1613 $title = str_replace( '%title', $post_title, $title );
1614 $title = str_replace( '%date', $date, $title );
1616 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1617 $link .= esc_attr( $title );
1618 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1620 $adjacent = $previous ? 'previous' : 'next';
1623 * Filter the adjacent post relational link.
1625 * The dynamic portion of the hook name, $adjacent, refers to the type
1626 * of adjacency, 'next' or 'previous'.
1630 * @param string $link The relational link.
1632 return apply_filters( "{$adjacent}_post_rel_link", $link );
1636 * Display relational links for the posts adjacent to the current post.
1640 * @param string $title Optional. Link title format.
1641 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1642 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1643 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1645 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1646 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1647 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1651 * Display relational links for the posts adjacent to the current post for single post pages.
1653 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1657 function adjacent_posts_rel_link_wp_head() {
1658 if ( ! is_single() || is_attachment() ) {
1661 adjacent_posts_rel_link();
1665 * Display relational link for the next post adjacent to the current post.
1669 * @param string $title Optional. Link title format.
1670 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1671 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1672 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1674 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1675 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1679 * Display relational link for the previous post adjacent to the current post.
1683 * @param string $title Optional. Link title format.
1684 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1685 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1686 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1688 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1689 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1693 * Retrieve boundary post.
1695 * Boundary being either the first or last post by publish date within the constraints specified
1696 * by $in_same_term or $excluded_terms.
1700 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1701 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1702 * @param bool $start Optional. Whether to retrieve first or last post.
1703 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1704 * @return mixed Array containing the boundary post object if successful, null otherwise.
1706 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1708 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1711 $query_args = array(
1712 'posts_per_page' => 1,
1713 'order' => $start ? 'ASC' : 'DESC',
1714 'update_post_term_cache' => false,
1715 'update_post_meta_cache' => false
1718 $term_array = array();
1720 if ( ! is_array( $excluded_terms ) ) {
1721 if ( ! empty( $excluded_terms ) )
1722 $excluded_terms = explode( ',', $excluded_terms );
1724 $excluded_terms = array();
1727 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1728 if ( $in_same_term )
1729 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1731 if ( ! empty( $excluded_terms ) ) {
1732 $excluded_terms = array_map( 'intval', $excluded_terms );
1733 $excluded_terms = array_diff( $excluded_terms, $term_array );
1735 $inverse_terms = array();
1736 foreach ( $excluded_terms as $excluded_term )
1737 $inverse_terms[] = $excluded_term * -1;
1738 $excluded_terms = $inverse_terms;
1741 $query_args[ 'tax_query' ] = array( array(
1742 'taxonomy' => $taxonomy,
1743 'terms' => array_merge( $term_array, $excluded_terms )
1747 return get_posts( $query_args );
1751 * Get previous post link that is adjacent to the current post.
1755 * @param string $format Optional. Link anchor format.
1756 * @param string $link Optional. Link permalink format.
1757 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1758 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1759 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1762 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1763 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1767 * Display previous post link that is adjacent to the current post.
1770 * @see get_previous_post_link()
1772 * @param string $format Optional. Link anchor format.
1773 * @param string $link Optional. Link permalink format.
1774 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1775 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1776 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1778 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1779 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1783 * Get next post link that is adjacent to the current post.
1787 * @param string $format Optional. Link anchor format.
1788 * @param string $link Optional. Link permalink format.
1789 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1790 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1791 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1794 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1795 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1799 * Display next post link that is adjacent to the current post.
1802 * @see get_next_post_link()
1804 * @param string $format Optional. Link anchor format.
1805 * @param string $link Optional. Link permalink format.
1806 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1807 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1808 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1810 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1811 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1815 * Get adjacent post link.
1817 * Can be either next post link or previous.
1821 * @param string $format Link anchor format.
1822 * @param string $link Link permalink format.
1823 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1824 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1825 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1826 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1829 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1830 if ( $previous && is_attachment() )
1831 $post = get_post( get_post()->post_parent );
1833 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1838 $title = $post->post_title;
1840 if ( empty( $post->post_title ) )
1841 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1843 /** This filter is documented in wp-includes/post-template.php */
1844 $title = apply_filters( 'the_title', $title, $post->ID );
1846 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1847 $rel = $previous ? 'prev' : 'next';
1849 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1850 $inlink = str_replace( '%title', $title, $link );
1851 $inlink = str_replace( '%date', $date, $inlink );
1852 $inlink = $string . $inlink . '</a>';
1854 $output = str_replace( '%link', $inlink, $format );
1857 $adjacent = $previous ? 'previous' : 'next';
1860 * Filter the adjacent post link.
1862 * The dynamic portion of the hook name, $adjacent, refers to the type
1863 * of adjacency, 'next' or 'previous'.
1867 * @param string $output The adjacent post link.
1868 * @param string $format Link anchor format.
1869 * @param string $link Link permalink format.
1870 * @param WP_Post $post The adjacent post.
1872 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
1876 * Display adjacent post link.
1878 * Can be either next post link or previous.
1881 * @uses get_adjacent_post_link()
1883 * @param string $format Link anchor format.
1884 * @param string $link Link permalink format.
1885 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1886 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
1887 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1888 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1891 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1892 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
1896 * Retrieve links for page numbers.
1900 * @param int $pagenum Optional. Page ID.
1901 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
1902 * Otherwise, prepares the URL with esc_url_raw().
1905 function get_pagenum_link($pagenum = 1, $escape = true ) {
1908 $pagenum = (int) $pagenum;
1910 $request = remove_query_arg( 'paged' );
1912 $home_root = parse_url(home_url());
1913 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1914 $home_root = preg_quote( $home_root, '|' );
1916 $request = preg_replace('|^'. $home_root . '|i', '', $request);
1917 $request = preg_replace('|^/+|', '', $request);
1919 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1920 $base = trailingslashit( get_bloginfo( 'url' ) );
1922 if ( $pagenum > 1 ) {
1923 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1925 $result = $base . $request;
1928 $qs_regex = '|\?.*?$|';
1929 preg_match( $qs_regex, $request, $qs_match );
1931 if ( !empty( $qs_match[0] ) ) {
1932 $query_string = $qs_match[0];
1933 $request = preg_replace( $qs_regex, '', $request );
1938 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
1939 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
1940 $request = ltrim($request, '/');
1942 $base = trailingslashit( get_bloginfo( 'url' ) );
1944 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1945 $base .= $wp_rewrite->index . '/';
1947 if ( $pagenum > 1 ) {
1948 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
1951 $result = $base . $request . $query_string;
1955 * Filter the page number link for the current request.
1959 * @param string $result The page number link.
1961 $result = apply_filters( 'get_pagenum_link', $result );
1964 return esc_url( $result );
1966 return esc_url_raw( $result );
1970 * Retrieve next posts page link.
1972 * Backported from 2.1.3 to 2.0.10.
1976 * @param int $max_page Optional. Max pages.
1979 function get_next_posts_page_link($max_page = 0) {
1982 if ( !is_single() ) {
1985 $nextpage = intval($paged) + 1;
1986 if ( !$max_page || $max_page >= $nextpage )
1987 return get_pagenum_link($nextpage);
1992 * Display or return the next posts page link.
1996 * @param int $max_page Optional. Max pages.
1997 * @param boolean $echo Optional. Echo or return;
1999 function next_posts( $max_page = 0, $echo = true ) {
2000 $output = esc_url( get_next_posts_page_link( $max_page ) );
2009 * Return the next posts page link.
2013 * @param string $label Content for link text.
2014 * @param int $max_page Optional. Max pages.
2015 * @return string|null
2017 function get_next_posts_link( $label = null, $max_page = 0 ) {
2018 global $paged, $wp_query;
2021 $max_page = $wp_query->max_num_pages;
2026 $nextpage = intval($paged) + 1;
2028 if ( null === $label )
2029 $label = __( 'Next Page »' );
2031 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2033 * Filter the anchor tag attributes for the next posts page link.
2037 * @param string $attributes Attributes for the anchor tag.
2039 $attr = apply_filters( 'next_posts_link_attributes', '' );
2041 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2046 * Display the next posts page link.
2049 * @uses get_next_posts_link()
2051 * @param string $label Content for link text.
2052 * @param int $max_page Optional. Max pages.
2054 function next_posts_link( $label = null, $max_page = 0 ) {
2055 echo get_next_posts_link( $label, $max_page );
2059 * Retrieve previous posts page link.
2061 * Will only return string, if not on a single page or post.
2063 * Backported to 2.0.10 from 2.1.3.
2067 * @return string|null
2069 function get_previous_posts_page_link() {
2072 if ( !is_single() ) {
2073 $nextpage = intval($paged) - 1;
2074 if ( $nextpage < 1 )
2076 return get_pagenum_link($nextpage);
2081 * Display or return the previous posts page link.
2085 * @param boolean $echo Optional. Echo or return;
2087 function previous_posts( $echo = true ) {
2088 $output = esc_url( get_previous_posts_page_link() );
2097 * Return the previous posts page link.
2101 * @param string $label Optional. Previous page link text.
2102 * @return string|null
2104 function get_previous_posts_link( $label = null ) {
2107 if ( null === $label )
2108 $label = __( '« Previous Page' );
2110 if ( !is_single() && $paged > 1 ) {
2112 * Filter the anchor tag attributes for the previous posts page link.
2116 * @param string $attributes Attributes for the anchor tag.
2118 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2119 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2124 * Display the previous posts page link.
2127 * @uses get_previous_posts_link()
2129 * @param string $label Optional. Previous page link text.
2131 function previous_posts_link( $label = null ) {
2132 echo get_previous_posts_link( $label );
2136 * Return post pages link navigation for previous and next pages.
2140 * @param string|array $args Optional args.
2141 * @return string The posts link navigation.
2143 function get_posts_nav_link( $args = array() ) {
2148 if ( !is_singular() ) {
2150 'sep' => ' — ',
2151 'prelabel' => __('« Previous Page'),
2152 'nxtlabel' => __('Next Page »'),
2154 $args = wp_parse_args( $args, $defaults );
2156 $max_num_pages = $wp_query->max_num_pages;
2157 $paged = get_query_var('paged');
2159 //only have sep if there's both prev and next results
2160 if ($paged < 2 || $paged >= $max_num_pages) {
2164 if ( $max_num_pages > 1 ) {
2165 $return = get_previous_posts_link($args['prelabel']);
2166 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2167 $return .= get_next_posts_link($args['nxtlabel']);
2175 * Display post pages link navigation for previous and next pages.
2179 * @param string $sep Optional. Separator for posts navigation links.
2180 * @param string $prelabel Optional. Label for previous pages.
2181 * @param string $nxtlabel Optional Label for next pages.
2183 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2184 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2185 echo get_posts_nav_link($args);
2189 * Retrieve comments page number link.
2193 * @param int $pagenum Optional. Page number.
2196 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2199 $pagenum = (int) $pagenum;
2201 $result = get_permalink();
2203 if ( 'newest' == get_option('default_comments_page') ) {
2204 if ( $pagenum != $max_page ) {
2205 if ( $wp_rewrite->using_permalinks() )
2206 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
2208 $result = add_query_arg( 'cpage', $pagenum, $result );
2210 } elseif ( $pagenum > 1 ) {
2211 if ( $wp_rewrite->using_permalinks() )
2212 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
2214 $result = add_query_arg( 'cpage', $pagenum, $result );
2217 $result .= '#comments';
2220 * Filter the comments page number link for the current request.
2224 * @param string $result The comments page number link.
2226 $result = apply_filters( 'get_comments_pagenum_link', $result );
2232 * Return the link to next comments page.
2236 * @param string $label Optional. Label for link text.
2237 * @param int $max_page Optional. Max page.
2238 * @return string|null
2240 function get_next_comments_link( $label = '', $max_page = 0 ) {
2243 if ( !is_singular() || !get_option('page_comments') )
2246 $page = get_query_var('cpage');
2248 $nextpage = intval($page) + 1;
2250 if ( empty($max_page) )
2251 $max_page = $wp_query->max_num_comment_pages;
2253 if ( empty($max_page) )
2254 $max_page = get_comment_pages_count();
2256 if ( $nextpage > $max_page )
2259 if ( empty($label) )
2260 $label = __('Newer Comments »');
2263 * Filter the anchor tag attributes for the next comments page link.
2267 * @param string $attributes Attributes for the anchor tag.
2269 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>';
2273 * Display the link to next comments page.
2277 * @param string $label Optional. Label for link text.
2278 * @param int $max_page Optional. Max page.
2280 function next_comments_link( $label = '', $max_page = 0 ) {
2281 echo get_next_comments_link( $label, $max_page );
2285 * Return the previous comments page link.
2289 * @param string $label Optional. Label for comments link text.
2290 * @return string|null
2292 function get_previous_comments_link( $label = '' ) {
2293 if ( !is_singular() || !get_option('page_comments') )
2296 $page = get_query_var('cpage');
2298 if ( intval($page) <= 1 )
2301 $prevpage = intval($page) - 1;
2303 if ( empty($label) )
2304 $label = __('« Older Comments');
2307 * Filter the anchor tag attributes for the previous comments page link.
2311 * @param string $attributes Attributes for the anchor tag.
2313 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>';
2317 * Display the previous comments page link.
2321 * @param string $label Optional. Label for comments link text.
2323 function previous_comments_link( $label = '' ) {
2324 echo get_previous_comments_link( $label );
2328 * Create pagination links for the comments on the current post.
2330 * @see paginate_links()
2333 * @param string|array $args Optional args. See paginate_links().
2334 * @return string Markup for pagination links.
2336 function paginate_comments_links($args = array()) {
2339 if ( !is_singular() || !get_option('page_comments') )
2342 $page = get_query_var('cpage');
2345 $max_page = get_comment_pages_count();
2347 'base' => add_query_arg( 'cpage', '%#%' ),
2349 'total' => $max_page,
2352 'add_fragment' => '#comments'
2354 if ( $wp_rewrite->using_permalinks() )
2355 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
2357 $args = wp_parse_args( $args, $defaults );
2358 $page_links = paginate_links( $args );
2360 if ( $args['echo'] )
2367 * Retrieve the Press This bookmarklet link.
2369 * Use this in 'a' element 'href' attribute.
2375 function get_shortcut_link() {
2376 // In case of breaking changes, version this. #WP20071
2377 $link = "javascript:
2383 s=(e?e():(k)?k():(x?x.createRange().text:0)),
2384 f='" . admin_url('press-this.php') . "',
2386 e=encodeURIComponent,
2387 u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
2388 a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
2389 if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
2392 $link = str_replace(array("\r", "\n", "\t"), '', $link);
2395 * Filter the Press This bookmarklet link.
2399 * @param string $link The Press This bookmarklet link.
2401 return apply_filters( 'shortcut_link', $link );
2405 * Retrieve the home url for the current site.
2407 * Returns the 'home' option with the appropriate protocol, 'https' if
2408 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2413 * @uses get_home_url()
2415 * @param string $path (optional) Path relative to the home url.
2416 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2417 * @return string Home url link with optional path appended.
2419 function home_url( $path = '', $scheme = null ) {
2420 return get_home_url( null, $path, $scheme );
2424 * Retrieve the home url for a given site.
2426 * Returns the 'home' option with the appropriate protocol, 'https' if
2427 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2432 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2433 * @param string $path (optional) Path relative to the home url.
2434 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2435 * @return string Home url link with optional path appended.
2437 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2438 $orig_scheme = $scheme;
2440 if ( empty( $blog_id ) || !is_multisite() ) {
2441 $url = get_option( 'home' );
2443 switch_to_blog( $blog_id );
2444 $url = get_option( 'home' );
2445 restore_current_blog();
2448 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2449 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
2452 $scheme = parse_url( $url, PHP_URL_SCHEME );
2455 $url = set_url_scheme( $url, $scheme );
2457 if ( $path && is_string( $path ) )
2458 $url .= '/' . ltrim( $path, '/' );
2461 * Filter the home URL.
2465 * @param string $url The complete home URL including scheme and path.
2466 * @param string $path Path relative to the home URL. Blank string if no path is specified.
2467 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
2468 * @param int|null $blog_id Blog ID, or null for the current blog.
2470 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
2474 * Retrieve the site url for the current site.
2476 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2477 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2482 * @uses get_site_url()
2484 * @param string $path Optional. Path relative to the site url.
2485 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2486 * @return string Site url link with optional path appended.
2488 function site_url( $path = '', $scheme = null ) {
2489 return get_site_url( null, $path, $scheme );
2493 * Retrieve the site url for a given site.
2495 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2496 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2501 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2502 * @param string $path Optional. Path relative to the site url.
2503 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
2504 * @return string Site url link with optional path appended.
2506 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2507 if ( empty( $blog_id ) || !is_multisite() ) {
2508 $url = get_option( 'siteurl' );
2510 switch_to_blog( $blog_id );
2511 $url = get_option( 'siteurl' );
2512 restore_current_blog();
2515 $url = set_url_scheme( $url, $scheme );
2517 if ( $path && is_string( $path ) )
2518 $url .= '/' . ltrim( $path, '/' );
2521 * Filter the site URL.
2525 * @param string $url The complete site URL including scheme and path.
2526 * @param string $path Path relative to the site URL. Blank string if no path is specified.
2527 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
2528 * 'login_post', 'admin', 'relative' or null.
2529 * @param int|null $blog_id Blog ID, or null for the current blog.
2531 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
2535 * Retrieve the url to the admin area for the current site.
2539 * @param string $path Optional path relative to the admin url.
2540 * @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.
2541 * @return string Admin url link with optional path appended.
2543 function admin_url( $path = '', $scheme = 'admin' ) {
2544 return get_admin_url( null, $path, $scheme );
2548 * Retrieve the url to the admin area for a given site.
2552 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2553 * @param string $path Optional path relative to the admin url.
2554 * @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.
2555 * @return string Admin url link with optional path appended.
2557 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2558 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2560 if ( $path && is_string( $path ) )
2561 $url .= ltrim( $path, '/' );
2564 * Filter the admin area URL.
2568 * @param string $url The complete admin area URL including scheme and path.
2569 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
2570 * @param int|null $blog_id Blog ID, or null for the current blog.
2572 return apply_filters( 'admin_url', $url, $path, $blog_id );
2576 * Retrieve the url to the includes directory.
2580 * @param string $path Optional. Path relative to the includes url.
2581 * @param string $scheme Optional. Scheme to give the includes url context.
2582 * @return string Includes url link with optional path appended.
2584 function includes_url( $path = '', $scheme = null ) {
2585 $url = site_url( '/' . WPINC . '/', $scheme );
2587 if ( $path && is_string( $path ) )
2588 $url .= ltrim($path, '/');
2591 * Filter the URL to the includes directory.
2595 * @param string $url The complete URL to the includes directory including scheme and path.
2596 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
2597 * if no path is specified.
2599 return apply_filters( 'includes_url', $url, $path );
2603 * Retrieve the url to the content directory.
2607 * @param string $path Optional. Path relative to the content url.
2608 * @return string Content url link with optional path appended.
2610 function content_url($path = '') {
2611 $url = set_url_scheme( WP_CONTENT_URL );
2613 if ( $path && is_string( $path ) )
2614 $url .= '/' . ltrim($path, '/');
2617 * Filter the URL to the content directory.
2621 * @param string $url The complete URL to the content directory including scheme and path.
2622 * @param string $path Path relative to the URL to the content directory. Blank string
2623 * if no path is specified.
2625 return apply_filters( 'content_url', $url, $path);
2629 * Retrieve a URL within the plugins or mu-plugins directory.
2631 * Defaults to the plugins directory URL if no arguments are supplied.
2635 * @param string $path Optional. Extra path appended to the end of the URL, including
2636 * the relative directory if $plugin is supplied. Default empty.
2637 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
2638 * The URL will be relative to its directory. Default empty.
2639 * Typically this is done by passing `__FILE__` as the argument.
2640 * @return string Plugins URL link with optional paths appended.
2642 function plugins_url( $path = '', $plugin = '' ) {
2644 $path = wp_normalize_path( $path );
2645 $plugin = wp_normalize_path( $plugin );
2646 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
2648 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2649 $url = WPMU_PLUGIN_URL;
2651 $url = WP_PLUGIN_URL;
2654 $url = set_url_scheme( $url );
2656 if ( !empty($plugin) && is_string($plugin) ) {
2657 $folder = dirname(plugin_basename($plugin));
2658 if ( '.' != $folder )
2659 $url .= '/' . ltrim($folder, '/');
2662 if ( $path && is_string( $path ) )
2663 $url .= '/' . ltrim($path, '/');
2666 * Filter the URL to the plugins directory.
2670 * @param string $url The complete URL to the plugins directory including scheme and path.
2671 * @param string $path Path relative to the URL to the plugins directory. Blank string
2672 * if no path is specified.
2673 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
2676 return apply_filters( 'plugins_url', $url, $path, $plugin );
2680 * Retrieve the site url for the current network.
2682 * Returns the site url with the appropriate protocol, 'https' if
2683 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2688 * @param string $path Optional. Path relative to the site url.
2689 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2690 * @return string Site url link with optional path appended.
2692 function network_site_url( $path = '', $scheme = null ) {
2693 if ( ! is_multisite() )
2694 return site_url($path, $scheme);
2696 $current_site = get_current_site();
2698 if ( 'relative' == $scheme )
2699 $url = $current_site->path;
2701 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2703 if ( $path && is_string( $path ) )
2704 $url .= ltrim( $path, '/' );
2707 * Filter the network site URL.
2711 * @param string $url The complete network site URL including scheme and path.
2712 * @param string $path Path relative to the network site URL. Blank string if
2713 * no path is specified.
2714 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
2715 * 'relative' or null.
2717 return apply_filters( 'network_site_url', $url, $path, $scheme );
2721 * Retrieve the home url for the current network.
2723 * Returns the home url with the appropriate protocol, 'https' if
2724 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2729 * @param string $path (optional) Path relative to the home url.
2730 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2731 * @return string Home url link with optional path appended.
2733 function network_home_url( $path = '', $scheme = null ) {
2734 if ( ! is_multisite() )
2735 return home_url($path, $scheme);
2737 $current_site = get_current_site();
2738 $orig_scheme = $scheme;
2740 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
2741 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
2743 if ( 'relative' == $scheme )
2744 $url = $current_site->path;
2746 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2748 if ( $path && is_string( $path ) )
2749 $url .= ltrim( $path, '/' );
2752 * Filter the network home URL.
2756 * @param string $url The complete network home URL including scheme and path.
2757 * @param string $path Path relative to the network home URL. Blank string
2758 * if no path is specified.
2759 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
2760 * 'relative' or null.
2762 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
2766 * Retrieve the url to the admin area for the network.
2770 * @param string $path Optional path relative to the admin url.
2771 * @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.
2772 * @return string Admin url link with optional path appended.
2774 function network_admin_url( $path = '', $scheme = 'admin' ) {
2775 if ( ! is_multisite() )
2776 return admin_url( $path, $scheme );
2778 $url = network_site_url('wp-admin/network/', $scheme);
2780 if ( $path && is_string( $path ) )
2781 $url .= ltrim($path, '/');
2784 * Filter the network admin URL.
2788 * @param string $url The complete network admin URL including scheme and path.
2789 * @param string $path Path relative to the network admin URL. Blank string if
2790 * no path is specified.
2792 return apply_filters( 'network_admin_url', $url, $path );
2796 * Retrieve the url to the admin area for the current user.
2800 * @param string $path Optional path relative to the admin url.
2801 * @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.
2802 * @return string Admin url link with optional path appended.
2804 function user_admin_url( $path = '', $scheme = 'admin' ) {
2805 $url = network_site_url('wp-admin/user/', $scheme);
2807 if ( $path && is_string( $path ) )
2808 $url .= ltrim($path, '/');
2811 * Filter the user admin URL for the current user.
2815 * @param string $url The complete URL including scheme and path.
2816 * @param string $path Path relative to the URL. Blank string if
2817 * no path is specified.
2819 return apply_filters( 'user_admin_url', $url, $path );
2823 * Retrieve the url to the admin area for either the current blog or the network depending on context.
2827 * @param string $path Optional path relative to the admin url.
2828 * @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.
2829 * @return string Admin url link with optional path appended.
2831 function self_admin_url($path = '', $scheme = 'admin') {
2832 if ( is_network_admin() )
2833 return network_admin_url($path, $scheme);
2834 elseif ( is_user_admin() )
2835 return user_admin_url($path, $scheme);
2837 return admin_url($path, $scheme);
2841 * Set the scheme for a URL
2845 * @param string $url Absolute url that includes a scheme
2846 * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
2847 * @return string $url URL with chosen scheme.
2849 function set_url_scheme( $url, $scheme = null ) {
2850 $orig_scheme = $scheme;
2853 $scheme = is_ssl() ? 'https' : 'http';
2854 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
2855 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
2856 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
2857 $scheme = is_ssl() ? 'https' : 'http';
2860 $url = trim( $url );
2861 if ( substr( $url, 0, 2 ) === '//' )
2862 $url = 'http:' . $url;
2864 if ( 'relative' == $scheme ) {
2865 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
2866 if ( $url !== '' && $url[0] === '/' )
2867 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
2869 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
2873 * Filter the resulting URL after setting the scheme.
2877 * @param string $url The complete URL including scheme and path.
2878 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
2879 * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
2880 * 'login_post', 'admin', 'rpc', or 'relative'.
2882 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
2886 * Get the URL to the user's dashboard.
2888 * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
2889 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
2890 * primary blog is returned.
2894 * @param int $user_id Optional. User ID. Defaults to current user.
2895 * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
2896 * @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.
2897 * @return string Dashboard url link with optional path appended.
2899 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
2900 $user_id = $user_id ? (int) $user_id : get_current_user_id();
2902 $blogs = get_blogs_of_user( $user_id );
2903 if ( ! is_super_admin() && empty($blogs) ) {
2904 $url = user_admin_url( $path, $scheme );
2905 } elseif ( ! is_multisite() ) {
2906 $url = admin_url( $path, $scheme );
2908 $current_blog = get_current_blog_id();
2909 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
2910 $url = admin_url( $path, $scheme );
2912 $active = get_active_blog_for_user( $user_id );
2914 $url = get_admin_url( $active->blog_id, $path, $scheme );
2916 $url = user_admin_url( $path, $scheme );
2921 * Filter the dashboard URL for a user.
2925 * @param string $url The complete URL including scheme and path.
2926 * @param int $user_id The user ID.
2927 * @param string $path Path relative to the URL. Blank string if no path is specified.
2928 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
2929 * 'login_post', 'admin', 'relative' or null.
2931 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
2935 * Get the URL to the user's profile editor.
2939 * @param int $user_id Optional. User ID. Defaults to current user.
2940 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
2941 * 'http' or 'https' can be passed to force those schemes.
2942 * @return string Dashboard url link with optional path appended.
2944 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
2945 $user_id = $user_id ? (int) $user_id : get_current_user_id();
2947 if ( is_user_admin() )
2948 $url = user_admin_url( 'profile.php', $scheme );
2949 elseif ( is_network_admin() )
2950 $url = network_admin_url( 'profile.php', $scheme );
2952 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
2955 * Filter the URL for a user's profile editor.
2959 * @param string $url The complete URL including scheme and path.
2960 * @param int $user_id The user ID.
2961 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
2962 * 'login_post', 'admin', 'relative' or null.
2964 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
2968 * Output rel=canonical for singular queries.
2972 function rel_canonical() {
2973 if ( !is_singular() )
2976 global $wp_the_query;
2977 if ( !$id = $wp_the_query->get_queried_object_id() )
2980 $link = get_permalink( $id );
2982 if ( $page = get_query_var('cpage') )
2983 $link = get_comments_pagenum_link( $page );
2985 echo "<link rel='canonical' href='$link' />\n";
2989 * Return a shortlink for a post, page, attachment, or blog.
2991 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
2992 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
2993 * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
2994 * via the get_shortlink filter.
2998 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
2999 * @param string $context Whether the id is a 'blog' id, 'post' id, or 'media' id. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determine the id and context. Default is 'post'.
3000 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3001 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3003 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3005 * Filter whether to preempt generating a shortlink for the given post.
3007 * Passing a truthy value to the filter will effectively short-circuit the
3008 * shortlink-generation process, returning that value instead.
3012 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3013 * @param int $id Post ID, or 0 for the current post.
3014 * @param string $context The context for the link. One of 'post' or 'query',
3015 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3017 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3019 if ( false !== $shortlink )
3024 if ( 'query' == $context && is_singular() ) {
3025 $post_id = $wp_query->get_queried_object_id();
3026 $post = get_post( $post_id );
3027 } elseif ( 'post' == $context ) {
3028 $post = get_post( $id );
3029 if ( ! empty( $post->ID ) )
3030 $post_id = $post->ID;
3035 // Return p= link for all public post types.
3036 if ( ! empty( $post_id ) ) {
3037 $post_type = get_post_type_object( $post->post_type );
3039 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3040 $shortlink = home_url( '/' );
3041 } elseif ( $post_type->public ) {
3042 $shortlink = home_url( '?p=' . $post_id );
3047 * Filter the shortlink for a post.
3051 * @param string $shortlink Shortlink URL.
3052 * @param int $id Post ID, or 0 for the current post.
3053 * @param string $context The context for the link. One of 'post' or 'query',
3054 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3056 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3060 * Inject rel=shortlink into head if a shortlink is defined for the current page.
3062 * Attached to the wp_head action.
3066 * @uses wp_get_shortlink()
3068 function wp_shortlink_wp_head() {
3069 $shortlink = wp_get_shortlink( 0, 'query' );
3071 if ( empty( $shortlink ) )
3074 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3078 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3080 * Attached to the wp action.
3084 * @uses wp_get_shortlink()
3086 function wp_shortlink_header() {
3087 if ( headers_sent() )
3090 $shortlink = wp_get_shortlink(0, 'query');
3092 if ( empty($shortlink) )
3095 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3099 * Display the Short Link for a Post
3101 * Must be called from inside "The Loop"
3103 * Call like the_shortlink(__('Shortlinkage FTW'))
3107 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3108 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3109 * @param string $before Optional HTML to display before the link.
3110 * @param string $after Optional HTML to display after the link.
3112 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3115 if ( empty( $text ) )
3116 $text = __('This is the short link.');
3118 if ( empty( $title ) )
3119 $title = the_title_attribute( array( 'echo' => false ) );
3121 $shortlink = wp_get_shortlink( $post->ID );
3123 if ( !empty( $shortlink ) ) {
3124 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3127 * Filter the shortlink anchor tag for a post.
3131 * @param string $link Shortlink anchor tag.
3132 * @param string $shortlink Shortlink URL.
3133 * @param string $text Shortlink's text.
3134 * @param string $title Shortlink's title attribute.
3136 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3137 echo $before, $link, $after;