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 ( $post_type->hierarchical ) {
261 $slug = get_page_uri( $id );
264 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
265 if ( ! $leavename ) {
266 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
268 $post_link = home_url( user_trailingslashit($post_link) );
270 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
271 $post_link = add_query_arg($post_type->query_var, $slug, '');
273 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
274 $post_link = home_url($post_link);
278 * Filter the permalink for a post with a custom post type.
282 * @param string $post_link The post's permalink.
283 * @param WP_Post $post The post in question.
284 * @param bool $leavename Whether to keep the post name.
285 * @param bool $sample Is it a sample permalink.
287 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
291 * Retrieve permalink from post ID.
295 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
296 * @param mixed $deprecated Not used.
299 function post_permalink( $post_id = 0, $deprecated = '' ) {
300 if ( !empty( $deprecated ) )
301 _deprecated_argument( __FUNCTION__, '1.3' );
303 return get_permalink($post_id);
307 * Retrieve the permalink for current page or page ID.
309 * Respects page_on_front. Use this one.
313 * @param int|object $post Optional. Post ID or object.
314 * @param bool $leavename Optional, defaults to false. Whether to keep page name.
315 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
318 function get_page_link( $post = false, $leavename = false, $sample = false ) {
319 $post = get_post( $post );
321 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
322 $link = home_url('/');
324 $link = _get_page_link( $post, $leavename, $sample );
327 * Filter the permalink for a page.
331 * @param string $link The page's permalink.
332 * @param int $post_id The ID of the page.
333 * @param bool $sample Is it a sample permalink.
335 return apply_filters( 'page_link', $link, $post->ID, $sample );
339 * Retrieve the page permalink.
341 * Ignores page_on_front. Internal use only.
346 * @param int|object $post Optional. Post ID or object.
347 * @param bool $leavename Optional. Leave name.
348 * @param bool $sample Optional. Sample permalink.
351 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
354 $post = get_post( $post );
356 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
358 $link = $wp_rewrite->get_page_permastruct();
360 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
361 if ( ! $leavename ) {
362 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
365 $link = home_url($link);
366 $link = user_trailingslashit($link, 'page');
368 $link = home_url( '?page_id=' . $post->ID );
372 * Filter the permalink for a non-page_on_front page.
376 * @param string $link The page's permalink.
377 * @param int $post_id The ID of the page.
379 return apply_filters( '_get_page_link', $link, $post->ID );
383 * Retrieve permalink for attachment.
385 * This can be used in the WordPress Loop or outside of it.
389 * @param int|object $post Optional. Post ID or object.
390 * @param bool $leavename Optional. Leave name.
393 function get_attachment_link( $post = null, $leavename = false ) {
398 $post = get_post( $post );
399 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
401 if ( $wp_rewrite->using_permalinks() && $parent ) {
402 if ( 'page' == $parent->post_type )
403 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
405 $parentlink = get_permalink( $post->post_parent );
407 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
408 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
410 $name = $post->post_name;
412 if ( strpos($parentlink, '?') === false )
413 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
416 $link = str_replace( '%postname%', $name, $link );
420 $link = home_url( '/?attachment_id=' . $post->ID );
423 * Filter the permalink for an attachment.
427 * @param string $link The attachment's permalink.
428 * @param int $post_id Attachment ID.
430 return apply_filters( 'attachment_link', $link, $post->ID );
434 * Retrieve the permalink for the year archives.
438 * @param int|bool $year False for current year or year for permalink.
441 function get_year_link($year) {
444 $year = gmdate('Y', current_time('timestamp'));
445 $yearlink = $wp_rewrite->get_year_permastruct();
446 if ( !empty($yearlink) ) {
447 $yearlink = str_replace('%year%', $year, $yearlink);
448 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
450 $yearlink = home_url( '?m=' . $year );
454 * Filter the year archive permalink.
458 * @param string $yearlink Permalink for the year archive.
459 * @param int $year Year for the archive.
461 return apply_filters( 'year_link', $yearlink, $year );
465 * Retrieve the permalink for the month archives with year.
469 * @param bool|int $year False for current year. Integer of year.
470 * @param bool|int $month False for current month. Integer of month.
473 function get_month_link($year, $month) {
476 $year = gmdate('Y', current_time('timestamp'));
478 $month = gmdate('m', current_time('timestamp'));
479 $monthlink = $wp_rewrite->get_month_permastruct();
480 if ( !empty($monthlink) ) {
481 $monthlink = str_replace('%year%', $year, $monthlink);
482 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
483 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
485 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
489 * Filter the month archive permalink.
493 * @param string $monthlink Permalink for the month archive.
494 * @param int $year Year for the archive.
495 * @param int $month The month for the archive.
497 return apply_filters( 'month_link', $monthlink, $year, $month );
501 * Retrieve the permalink for the day archives with year and month.
505 * @param bool|int $year False for current year. Integer of year.
506 * @param bool|int $month False for current month. Integer of month.
507 * @param bool|int $day False for current day. Integer of day.
510 function get_day_link($year, $month, $day) {
513 $year = gmdate('Y', current_time('timestamp'));
515 $month = gmdate('m', current_time('timestamp'));
517 $day = gmdate('j', current_time('timestamp'));
519 $daylink = $wp_rewrite->get_day_permastruct();
520 if ( !empty($daylink) ) {
521 $daylink = str_replace('%year%', $year, $daylink);
522 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
523 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
524 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
526 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
530 * Filter the day archive permalink.
534 * @param string $daylink Permalink for the day archive.
535 * @param int $year Year for the archive.
536 * @param int $month Month for the archive.
537 * @param int $day The day for the archive.
539 return apply_filters( 'day_link', $daylink, $year, $month, $day );
543 * Display the permalink for the feed type.
547 * @param string $anchor The link's anchor text.
548 * @param string $feed Optional, defaults to default feed. Feed type.
550 function the_feed_link( $anchor, $feed = '' ) {
551 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
554 * Filter the feed link anchor tag.
558 * @param string $link The complete anchor tag for a feed link.
559 * @param string $feed The feed type, or an empty string for the
562 echo apply_filters( 'the_feed_link', $link, $feed );
566 * Retrieve the permalink for the feed type.
570 * @param string $feed Optional, defaults to default feed. Feed type.
573 function get_feed_link($feed = '') {
576 $permalink = $wp_rewrite->get_feed_permastruct();
577 if ( '' != $permalink ) {
578 if ( false !== strpos($feed, 'comments_') ) {
579 $feed = str_replace('comments_', '', $feed);
580 $permalink = $wp_rewrite->get_comment_feed_permastruct();
583 if ( get_default_feed() == $feed )
586 $permalink = str_replace('%feed%', $feed, $permalink);
587 $permalink = preg_replace('#/+#', '/', "/$permalink");
588 $output = home_url( user_trailingslashit($permalink, 'feed') );
591 $feed = get_default_feed();
593 if ( false !== strpos($feed, 'comments_') )
594 $feed = str_replace('comments_', 'comments-', $feed);
596 $output = home_url("?feed={$feed}");
600 * Filter the feed type permalink.
604 * @param string $output The feed permalink.
605 * @param string $feed Feed type.
607 return apply_filters( 'feed_link', $output, $feed );
611 * Retrieve the permalink for the post comments feed.
615 * @param int $post_id Optional. Post ID.
616 * @param string $feed Optional. Feed type.
619 function get_post_comments_feed_link($post_id = 0, $feed = '') {
620 $post_id = absint( $post_id );
623 $post_id = get_the_ID();
625 if ( empty( $feed ) )
626 $feed = get_default_feed();
628 if ( '' != get_option('permalink_structure') ) {
629 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
630 $url = _get_page_link( $post_id );
632 $url = get_permalink($post_id);
634 $url = trailingslashit($url) . 'feed';
635 if ( $feed != get_default_feed() )
637 $url = user_trailingslashit($url, 'single_feed');
639 $type = get_post_field('post_type', $post_id);
640 if ( 'page' == $type )
641 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
643 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
647 * Filter the post comments feed permalink.
651 * @param string $url Post comments feed permalink.
653 return apply_filters( 'post_comments_feed_link', $url );
657 * Display the comment feed link for a post.
659 * Prints out the comment feed link for a post. Link text is placed in the
660 * anchor. If no link text is specified, default text is used. If no post ID is
661 * specified, the current post is used.
665 * @param string $link_text Descriptive text.
666 * @param int $post_id Optional post ID. Default to current post.
667 * @param string $feed Optional. Feed format.
668 * @return string Link to the comment feed for the current post.
670 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
671 $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
672 if ( empty($link_text) )
673 $link_text = __('Comments Feed');
676 * Filter the post comment feed link anchor tag.
680 * @param string $link The complete anchor tag for the comment feed link.
681 * @param int $post_id Post ID.
682 * @param string $feed The feed type, or an empty string for the default feed type.
684 echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
688 * Retrieve the feed link for a given author.
690 * Returns a link to the feed for all posts by a given author. A specific feed
691 * can be requested or left blank to get the default feed.
695 * @param int $author_id ID of an author.
696 * @param string $feed Optional. Feed type.
697 * @return string Link to the feed for the author specified by $author_id.
699 function get_author_feed_link( $author_id, $feed = '' ) {
700 $author_id = (int) $author_id;
701 $permalink_structure = get_option('permalink_structure');
704 $feed = get_default_feed();
706 if ( '' == $permalink_structure ) {
707 $link = home_url("?feed=$feed&author=" . $author_id);
709 $link = get_author_posts_url($author_id);
710 if ( $feed == get_default_feed() )
713 $feed_link = "feed/$feed";
715 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
719 * Filter the feed link for a given author.
723 * @param string $link The author feed link.
724 * @param string $feed Feed type.
726 $link = apply_filters( 'author_feed_link', $link, $feed );
732 * Retrieve the feed link for a category.
734 * Returns a link to the feed for all posts in a given category. A specific feed
735 * can be requested or left blank to get the default feed.
739 * @param int $cat_id ID of a category.
740 * @param string $feed Optional. Feed type.
741 * @return string Link to the feed for the category specified by $cat_id.
743 function get_category_feed_link($cat_id, $feed = '') {
744 return get_term_feed_link($cat_id, 'category', $feed);
748 * Retrieve the feed link for a term.
750 * Returns a link to the feed for all posts in a given term. A specific feed
751 * can be requested or left blank to get the default feed.
755 * @param int $term_id ID of a category.
756 * @param string $taxonomy Optional. Taxonomy of $term_id
757 * @param string $feed Optional. Feed type.
758 * @return string Link to the feed for the term specified by $term_id and $taxonomy.
760 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
761 $term_id = ( int ) $term_id;
763 $term = get_term( $term_id, $taxonomy );
765 if ( empty( $term ) || is_wp_error( $term ) )
768 if ( empty( $feed ) )
769 $feed = get_default_feed();
771 $permalink_structure = get_option( 'permalink_structure' );
773 if ( '' == $permalink_structure ) {
774 if ( 'category' == $taxonomy ) {
775 $link = home_url("?feed=$feed&cat=$term_id");
777 elseif ( 'post_tag' == $taxonomy ) {
778 $link = home_url("?feed=$feed&tag=$term->slug");
780 $t = get_taxonomy( $taxonomy );
781 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
784 $link = get_term_link( $term_id, $term->taxonomy );
785 if ( $feed == get_default_feed() )
788 $feed_link = "feed/$feed";
790 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
793 if ( 'category' == $taxonomy ) {
795 * Filter the category feed link.
799 * @param string $link The category feed link.
800 * @param string $feed Feed type.
802 $link = apply_filters( 'category_feed_link', $link, $feed );
803 } elseif ( 'post_tag' == $taxonomy ) {
805 * Filter the post tag feed link.
809 * @param string $link The tag feed link.
810 * @param string $feed Feed type.
812 $link = apply_filters( 'tag_feed_link', $link, $feed );
815 * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
819 * @param string $link The taxonomy feed link.
820 * @param string $feed Feed type.
821 * @param string $feed The taxonomy name.
823 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
830 * Retrieve permalink for feed of tag.
834 * @param int $tag_id Tag ID.
835 * @param string $feed Optional. Feed type.
838 function get_tag_feed_link($tag_id, $feed = '') {
839 return get_term_feed_link($tag_id, 'post_tag', $feed);
843 * Retrieve edit tag link.
847 * @param int $tag_id Tag ID
848 * @param string $taxonomy Taxonomy
851 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
853 * Filter the edit link for a tag (or term in another taxonomy).
857 * @param string $link The term edit link.
859 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
863 * Display or retrieve edit tag link with formatting.
867 * @param string $link Optional. Anchor text.
868 * @param string $before Optional. Display before edit link.
869 * @param string $after Optional. Display after edit link.
870 * @param object $tag Tag object.
871 * @return string HTML content.
873 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
874 $link = edit_term_link( $link, '', '', $tag, false );
877 * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
881 * @param string $link The anchor tag for the edit link.
883 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
887 * Retrieve edit term url.
891 * @param int $term_id Term ID
892 * @param string $taxonomy Taxonomy
893 * @param string $object_type The object type
896 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
897 $tax = get_taxonomy( $taxonomy );
898 if ( !current_user_can( $tax->cap->edit_terms ) )
901 $term = get_term( $term_id, $taxonomy );
905 'taxonomy' => $taxonomy,
906 'tag_ID' => $term->term_id,
910 $args['post_type'] = $object_type;
912 $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
915 * Filter the edit link for a term.
919 * @param string $location The edit link.
920 * @param int $term_id Term ID.
921 * @param string $taxonomy Taxonomy name.
922 * @param string $object_type The object type (eg. the post type).
924 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
928 * Display or retrieve edit term link with formatting.
932 * @param string $link Optional. Anchor text.
933 * @param string $before Optional. Display before edit link.
934 * @param string $after Optional. Display after edit link.
935 * @param object $term Term object.
936 * @return string HTML content.
938 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
939 if ( is_null( $term ) )
940 $term = get_queried_object();
945 $tax = get_taxonomy( $term->taxonomy );
946 if ( ! current_user_can( $tax->cap->edit_terms ) )
949 if ( empty( $link ) )
950 $link = __('Edit This');
952 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
955 * Filter the anchor tag for the edit link of a term.
959 * @param string $link The anchor tag for the edit link.
960 * @param int $term_id Term ID.
962 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
971 * Retrieve permalink for search.
975 * @param string $query Optional. The query string to use. If empty the current query is used.
978 function get_search_link( $query = '' ) {
982 $search = get_search_query( false );
984 $search = stripslashes($query);
986 $permastruct = $wp_rewrite->get_search_permastruct();
988 if ( empty( $permastruct ) ) {
989 $link = home_url('?s=' . urlencode($search) );
991 $search = urlencode($search);
992 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
993 $link = str_replace( '%search%', $search, $permastruct );
994 $link = home_url( user_trailingslashit( $link, 'search' ) );
998 * Filter the search permalink.
1002 * @param string $link Search permalink.
1003 * @param string $search The URL-encoded search term.
1005 return apply_filters( 'search_link', $link, $search );
1009 * Retrieve the permalink for the feed of the search results.
1013 * @param string $search_query Optional. Search query.
1014 * @param string $feed Optional. Feed type.
1017 function get_search_feed_link($search_query = '', $feed = '') {
1019 $link = get_search_link($search_query);
1022 $feed = get_default_feed();
1024 $permastruct = $wp_rewrite->get_search_permastruct();
1026 if ( empty($permastruct) ) {
1027 $link = add_query_arg('feed', $feed, $link);
1029 $link = trailingslashit($link);
1030 $link .= "feed/$feed/";
1034 * Filter the search feed link.
1038 * @param string $link Search feed link.
1039 * @param string $feed Feed type.
1040 * @param string $type The search type. One of 'posts' or 'comments'.
1042 $link = apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1048 * Retrieve the permalink for the comments feed of the search results.
1052 * @param string $search_query Optional. Search query.
1053 * @param string $feed Optional. Feed type.
1056 function get_search_comments_feed_link($search_query = '', $feed = '') {
1060 $feed = get_default_feed();
1062 $link = get_search_feed_link($search_query, $feed);
1064 $permastruct = $wp_rewrite->get_search_permastruct();
1066 if ( empty($permastruct) )
1067 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1069 $link = add_query_arg('withcomments', 1, $link);
1071 /** This filter is documented in wp-includes/link-template.php */
1072 $link = apply_filters('search_feed_link', $link, $feed, 'comments');
1078 * Retrieve the permalink for a post type archive.
1082 * @param string $post_type Post type
1085 function get_post_type_archive_link( $post_type ) {
1087 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1090 if ( ! $post_type_obj->has_archive )
1093 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1094 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1095 if ( $post_type_obj->rewrite['with_front'] )
1096 $struct = $wp_rewrite->front . $struct;
1098 $struct = $wp_rewrite->root . $struct;
1099 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1101 $link = home_url( '?post_type=' . $post_type );
1105 * Filter the post type archive permalink.
1109 * @param string $link The post type archive permalink.
1110 * @param string $post_type Post type name.
1112 return apply_filters( 'post_type_archive_link', $link, $post_type );
1116 * Retrieve the permalink for a post type archive feed.
1120 * @param string $post_type Post type
1121 * @param string $feed Optional. Feed type
1124 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1125 $default_feed = get_default_feed();
1126 if ( empty( $feed ) )
1127 $feed = $default_feed;
1129 if ( ! $link = get_post_type_archive_link( $post_type ) )
1132 $post_type_obj = get_post_type_object( $post_type );
1133 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1134 $link = trailingslashit( $link );
1136 if ( $feed != $default_feed )
1139 $link = add_query_arg( 'feed', $feed, $link );
1143 * Filter the post type archive feed link.
1147 * @param string $link The post type archive feed link.
1148 * @param string $feed Feed type.
1150 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1154 * Retrieve edit posts link for post.
1156 * Can be used within the WordPress loop or outside of it. Can be used with
1157 * pages, posts, attachments, and revisions.
1161 * @param int $id Optional. Post ID.
1162 * @param string $context Optional, defaults to display. How to write the '&', defaults to '&'.
1165 function get_edit_post_link( $id = 0, $context = 'display' ) {
1166 if ( ! $post = get_post( $id ) )
1169 if ( 'revision' === $post->post_type )
1171 elseif ( 'display' == $context )
1172 $action = '&action=edit';
1174 $action = '&action=edit';
1176 $post_type_object = get_post_type_object( $post->post_type );
1177 if ( !$post_type_object )
1180 if ( !current_user_can( 'edit_post', $post->ID ) )
1184 * Filter the post edit link.
1188 * @param string $link The edit link.
1189 * @param int $post_id Post ID.
1190 * @param string $context The link context. If set to 'display' then ampersands
1193 return apply_filters( 'get_edit_post_link', admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ), $post->ID, $context );
1197 * Display edit post link for post.
1201 * @param string $text Optional. Anchor text.
1202 * @param string $before Optional. Display before edit link.
1203 * @param string $after Optional. Display after edit link.
1204 * @param int $id Optional. Post ID.
1206 function edit_post_link( $text = null, $before = '', $after = '', $id = 0 ) {
1207 if ( ! $post = get_post( $id ) ) {
1211 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1215 if ( null === $text ) {
1216 $text = __( 'Edit This' );
1219 $link = '<a class="post-edit-link" href="' . $url . '">' . $text . '</a>';
1222 * Filter the post edit link anchor tag.
1226 * @param string $link Anchor tag for the edit link.
1227 * @param int $post_id Post ID.
1228 * @param string $text Anchor text.
1230 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1234 * Retrieve delete posts link for post.
1236 * Can be used within the WordPress loop or outside of it, with any post type.
1240 * @param int $id Optional. Post ID.
1241 * @param string $deprecated Not used.
1242 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1245 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1246 if ( ! empty( $deprecated ) )
1247 _deprecated_argument( __FUNCTION__, '3.0' );
1249 if ( !$post = get_post( $id ) )
1252 $post_type_object = get_post_type_object( $post->post_type );
1253 if ( !$post_type_object )
1256 if ( !current_user_can( 'delete_post', $post->ID ) )
1259 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1261 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1264 * Filter the post delete link.
1268 * @param string $link The delete link.
1269 * @param int $post_id Post ID.
1270 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1272 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1276 * Retrieve edit comment link.
1280 * @param int $comment_id Optional. Comment ID.
1283 function get_edit_comment_link( $comment_id = 0 ) {
1284 $comment = get_comment( $comment_id );
1286 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1289 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1292 * Filter the comment edit link.
1296 * @param string $location The edit link.
1298 return apply_filters( 'get_edit_comment_link', $location );
1302 * Display edit comment link with formatting.
1306 * @param string $text Optional. Anchor text.
1307 * @param string $before Optional. Display before edit link.
1308 * @param string $after Optional. Display after edit link.
1310 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1313 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1317 if ( null === $text ) {
1318 $text = __( 'Edit This' );
1321 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $text . '</a>';
1324 * Filter the comment edit link anchor tag.
1328 * @param string $link Anchor tag for the edit link.
1329 * @param int $comment_id Comment ID.
1330 * @param string $text Anchor text.
1332 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1336 * Display edit bookmark (literally a URL external to blog) link.
1340 * @param int $link Optional. Bookmark ID.
1343 function get_edit_bookmark_link( $link = 0 ) {
1344 $link = get_bookmark( $link );
1346 if ( !current_user_can('manage_links') )
1349 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1352 * Filter the bookmark (link) edit link.
1356 * @param string $location The edit link.
1357 * @param int $link_id Bookmark ID.
1359 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1363 * Display edit bookmark (literally a URL external to blog) link anchor content.
1367 * @param string $link Optional. Anchor text.
1368 * @param string $before Optional. Display before edit link.
1369 * @param string $after Optional. Display after edit link.
1370 * @param int $bookmark Optional. Bookmark ID.
1372 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1373 $bookmark = get_bookmark($bookmark);
1375 if ( !current_user_can('manage_links') )
1379 $link = __('Edit This');
1381 $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
1384 * Filter the bookmark edit link anchor tag.
1388 * @param string $link Anchor tag for the edit link.
1389 * @param int $link_id Bookmark ID.
1391 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1395 * Retrieve edit user link
1399 * @param int $user_id Optional. User ID. Defaults to the current user.
1400 * @return string URL to edit user page or empty string.
1402 function get_edit_user_link( $user_id = null ) {
1404 $user_id = get_current_user_id();
1406 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1409 $user = get_userdata( $user_id );
1414 if ( get_current_user_id() == $user->ID )
1415 $link = get_edit_profile_url( $user->ID );
1417 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1420 * Filter the user edit link.
1424 * @param string $link The edit link.
1425 * @param int $user_id User ID.
1427 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1433 * Retrieve previous post that is adjacent to current post.
1437 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1438 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1439 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1440 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1442 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1443 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1447 * Retrieve next post that is adjacent to current post.
1451 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1452 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1453 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1454 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1456 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1457 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1461 * Retrieve adjacent post.
1463 * Can either be next or previous post.
1467 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1468 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1469 * @param bool $previous Optional. Whether to retrieve previous post.
1470 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1471 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1473 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1476 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1479 $current_post_date = $post->post_date;
1484 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1485 $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";
1486 $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1488 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1489 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1490 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1491 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1492 $excluded_terms = explode( ' and ', $excluded_terms );
1494 $excluded_terms = explode( ',', $excluded_terms );
1497 $excluded_terms = array_map( 'intval', $excluded_terms );
1500 if ( $in_same_term ) {
1501 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1503 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1505 // Remove any exclusions from the term array to include.
1506 $term_array = array_diff( $term_array, (array) $excluded_terms );
1507 $term_array = array_map( 'intval', $term_array );
1509 if ( ! $term_array || is_wp_error( $term_array ) )
1512 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1515 if ( ! empty( $excluded_terms ) ) {
1516 $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, ',' ) . ') )';
1520 $adjacent = $previous ? 'previous' : 'next';
1521 $op = $previous ? '<' : '>';
1522 $order = $previous ? 'DESC' : 'ASC';
1525 * Filter the JOIN clause in the SQL for an adjacent post query.
1527 * The dynamic portion of the hook name, $adjacent, refers to the type
1528 * of adjacency, 'next' or 'previous'.
1532 * @param string $join The JOIN clause in the SQL.
1533 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1534 * @param array $excluded_terms Array of excluded term IDs.
1536 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
1539 * Filter the WHERE clause in the SQL for an adjacent post query.
1541 * The dynamic portion of the hook name, $adjacent, refers to the type
1542 * of adjacency, 'next' or 'previous'.
1546 * @param string $where The WHERE clause in the SQL.
1547 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1548 * @param array $excluded_terms Array of excluded term IDs.
1550 $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 );
1553 * Filter the ORDER BY clause in the SQL for an adjacent post query.
1555 * The dynamic portion of the hook name, $adjacent, refers to the type
1556 * of adjacency, 'next' or 'previous'.
1560 * @param string $order_by The ORDER BY clause in the SQL.
1562 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1564 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1565 $query_key = 'adjacent_post_' . md5( $query );
1566 $result = wp_cache_get( $query_key, 'counts' );
1567 if ( false !== $result ) {
1569 $result = get_post( $result );
1573 $result = $wpdb->get_var( $query );
1574 if ( null === $result )
1577 wp_cache_set( $query_key, $result, 'counts' );
1580 $result = get_post( $result );
1586 * Get adjacent post relational link.
1588 * Can either be next or previous post relational link.
1592 * @param string $title Optional. Link title format.
1593 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1594 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1595 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1596 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1599 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1600 if ( $previous && is_attachment() && $post = get_post() )
1601 $post = get_post( $post->post_parent );
1603 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1605 if ( empty( $post ) )
1608 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1610 if ( empty( $post_title ) )
1611 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1613 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1615 $title = str_replace( '%title', $post_title, $title );
1616 $title = str_replace( '%date', $date, $title );
1618 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1619 $link .= esc_attr( $title );
1620 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1622 $adjacent = $previous ? 'previous' : 'next';
1625 * Filter the adjacent post relational link.
1627 * The dynamic portion of the hook name, $adjacent, refers to the type
1628 * of adjacency, 'next' or 'previous'.
1632 * @param string $link The relational link.
1634 return apply_filters( "{$adjacent}_post_rel_link", $link );
1638 * Display relational links for the posts adjacent to the current post.
1642 * @param string $title Optional. Link title format.
1643 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1644 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1645 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1647 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1648 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1649 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1653 * Display relational links for the posts adjacent to the current post for single post pages.
1655 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1659 function adjacent_posts_rel_link_wp_head() {
1660 if ( ! is_single() || is_attachment() ) {
1663 adjacent_posts_rel_link();
1667 * Display relational link for the next post adjacent to the current post.
1671 * @param string $title Optional. Link title format.
1672 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1673 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1674 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1676 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1677 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1681 * Display relational link for the previous post adjacent to the current post.
1685 * @param string $title Optional. Link title format.
1686 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1687 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1688 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1690 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1691 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1695 * Retrieve boundary post.
1697 * Boundary being either the first or last post by publish date within the constraints specified
1698 * by $in_same_term or $excluded_terms.
1702 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1703 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1704 * @param bool $start Optional. Whether to retrieve first or last post.
1705 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1706 * @return mixed Array containing the boundary post object if successful, null otherwise.
1708 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1710 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1713 $query_args = array(
1714 'posts_per_page' => 1,
1715 'order' => $start ? 'ASC' : 'DESC',
1716 'update_post_term_cache' => false,
1717 'update_post_meta_cache' => false
1720 $term_array = array();
1722 if ( ! is_array( $excluded_terms ) ) {
1723 if ( ! empty( $excluded_terms ) )
1724 $excluded_terms = explode( ',', $excluded_terms );
1726 $excluded_terms = array();
1729 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1730 if ( $in_same_term )
1731 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1733 if ( ! empty( $excluded_terms ) ) {
1734 $excluded_terms = array_map( 'intval', $excluded_terms );
1735 $excluded_terms = array_diff( $excluded_terms, $term_array );
1737 $inverse_terms = array();
1738 foreach ( $excluded_terms as $excluded_term )
1739 $inverse_terms[] = $excluded_term * -1;
1740 $excluded_terms = $inverse_terms;
1743 $query_args[ 'tax_query' ] = array( array(
1744 'taxonomy' => $taxonomy,
1745 'terms' => array_merge( $term_array, $excluded_terms )
1749 return get_posts( $query_args );
1753 * Get previous post link that is adjacent to the current post.
1757 * @param string $format Optional. Link anchor format.
1758 * @param string $link Optional. Link permalink format.
1759 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1760 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1761 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1764 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1765 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1769 * Display previous post link that is adjacent to the current post.
1772 * @see get_previous_post_link()
1774 * @param string $format Optional. Link anchor format.
1775 * @param string $link Optional. Link permalink format.
1776 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1777 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1778 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1780 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1781 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1785 * Get next post link that is adjacent to the current post.
1789 * @param string $format Optional. Link anchor format.
1790 * @param string $link Optional. Link permalink format.
1791 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1792 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1793 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1796 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1797 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1801 * Display next post link that is adjacent to the current post.
1804 * @see get_next_post_link()
1806 * @param string $format Optional. Link anchor format.
1807 * @param string $link Optional. Link permalink format.
1808 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1809 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1810 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1812 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1813 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1817 * Get adjacent post link.
1819 * Can be either next post link or previous.
1823 * @param string $format Link anchor format.
1824 * @param string $link Link permalink format.
1825 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1826 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1827 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1828 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1831 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1832 if ( $previous && is_attachment() )
1833 $post = get_post( get_post()->post_parent );
1835 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1840 $title = $post->post_title;
1842 if ( empty( $post->post_title ) )
1843 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1845 /** This filter is documented in wp-includes/post-template.php */
1846 $title = apply_filters( 'the_title', $title, $post->ID );
1848 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1849 $rel = $previous ? 'prev' : 'next';
1851 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1852 $inlink = str_replace( '%title', $title, $link );
1853 $inlink = str_replace( '%date', $date, $inlink );
1854 $inlink = $string . $inlink . '</a>';
1856 $output = str_replace( '%link', $inlink, $format );
1859 $adjacent = $previous ? 'previous' : 'next';
1862 * Filter the adjacent post link.
1864 * The dynamic portion of the hook name, $adjacent, refers to the type
1865 * of adjacency, 'next' or 'previous'.
1869 * @param string $output The adjacent post link.
1870 * @param string $format Link anchor format.
1871 * @param string $link Link permalink format.
1872 * @param WP_Post $post The adjacent post.
1874 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
1878 * Display adjacent post link.
1880 * Can be either next post link or previous.
1883 * @uses get_adjacent_post_link()
1885 * @param string $format Link anchor format.
1886 * @param string $link Link permalink format.
1887 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1888 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
1889 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1890 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1893 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1894 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
1898 * Retrieve links for page numbers.
1902 * @param int $pagenum Optional. Page ID.
1903 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
1904 * Otherwise, prepares the URL with esc_url_raw().
1907 function get_pagenum_link($pagenum = 1, $escape = true ) {
1910 $pagenum = (int) $pagenum;
1912 $request = remove_query_arg( 'paged' );
1914 $home_root = parse_url(home_url());
1915 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1916 $home_root = preg_quote( $home_root, '|' );
1918 $request = preg_replace('|^'. $home_root . '|i', '', $request);
1919 $request = preg_replace('|^/+|', '', $request);
1921 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1922 $base = trailingslashit( get_bloginfo( 'url' ) );
1924 if ( $pagenum > 1 ) {
1925 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1927 $result = $base . $request;
1930 $qs_regex = '|\?.*?$|';
1931 preg_match( $qs_regex, $request, $qs_match );
1933 if ( !empty( $qs_match[0] ) ) {
1934 $query_string = $qs_match[0];
1935 $request = preg_replace( $qs_regex, '', $request );
1940 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
1941 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
1942 $request = ltrim($request, '/');
1944 $base = trailingslashit( get_bloginfo( 'url' ) );
1946 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1947 $base .= $wp_rewrite->index . '/';
1949 if ( $pagenum > 1 ) {
1950 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
1953 $result = $base . $request . $query_string;
1957 * Filter the page number link for the current request.
1961 * @param string $result The page number link.
1963 $result = apply_filters( 'get_pagenum_link', $result );
1966 return esc_url( $result );
1968 return esc_url_raw( $result );
1972 * Retrieve next posts page link.
1974 * Backported from 2.1.3 to 2.0.10.
1978 * @param int $max_page Optional. Max pages.
1981 function get_next_posts_page_link($max_page = 0) {
1984 if ( !is_single() ) {
1987 $nextpage = intval($paged) + 1;
1988 if ( !$max_page || $max_page >= $nextpage )
1989 return get_pagenum_link($nextpage);
1994 * Display or return the next posts page link.
1998 * @param int $max_page Optional. Max pages.
1999 * @param boolean $echo Optional. Echo or return;
2001 function next_posts( $max_page = 0, $echo = true ) {
2002 $output = esc_url( get_next_posts_page_link( $max_page ) );
2011 * Return the next posts page link.
2015 * @param string $label Content for link text.
2016 * @param int $max_page Optional. Max pages.
2017 * @return string|null
2019 function get_next_posts_link( $label = null, $max_page = 0 ) {
2020 global $paged, $wp_query;
2023 $max_page = $wp_query->max_num_pages;
2028 $nextpage = intval($paged) + 1;
2030 if ( null === $label )
2031 $label = __( 'Next Page »' );
2033 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2035 * Filter the anchor tag attributes for the next posts page link.
2039 * @param string $attributes Attributes for the anchor tag.
2041 $attr = apply_filters( 'next_posts_link_attributes', '' );
2043 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2048 * Display the next posts page link.
2051 * @uses get_next_posts_link()
2053 * @param string $label Content for link text.
2054 * @param int $max_page Optional. Max pages.
2056 function next_posts_link( $label = null, $max_page = 0 ) {
2057 echo get_next_posts_link( $label, $max_page );
2061 * Retrieve previous posts page link.
2063 * Will only return string, if not on a single page or post.
2065 * Backported to 2.0.10 from 2.1.3.
2069 * @return string|null
2071 function get_previous_posts_page_link() {
2074 if ( !is_single() ) {
2075 $nextpage = intval($paged) - 1;
2076 if ( $nextpage < 1 )
2078 return get_pagenum_link($nextpage);
2083 * Display or return the previous posts page link.
2087 * @param boolean $echo Optional. Echo or return;
2089 function previous_posts( $echo = true ) {
2090 $output = esc_url( get_previous_posts_page_link() );
2099 * Return the previous posts page link.
2103 * @param string $label Optional. Previous page link text.
2104 * @return string|null
2106 function get_previous_posts_link( $label = null ) {
2109 if ( null === $label )
2110 $label = __( '« Previous Page' );
2112 if ( !is_single() && $paged > 1 ) {
2114 * Filter the anchor tag attributes for the previous posts page link.
2118 * @param string $attributes Attributes for the anchor tag.
2120 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2121 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2126 * Display the previous posts page link.
2129 * @uses get_previous_posts_link()
2131 * @param string $label Optional. Previous page link text.
2133 function previous_posts_link( $label = null ) {
2134 echo get_previous_posts_link( $label );
2138 * Return post pages link navigation for previous and next pages.
2142 * @param string|array $args Optional args.
2143 * @return string The posts link navigation.
2145 function get_posts_nav_link( $args = array() ) {
2150 if ( !is_singular() ) {
2152 'sep' => ' — ',
2153 'prelabel' => __('« Previous Page'),
2154 'nxtlabel' => __('Next Page »'),
2156 $args = wp_parse_args( $args, $defaults );
2158 $max_num_pages = $wp_query->max_num_pages;
2159 $paged = get_query_var('paged');
2161 //only have sep if there's both prev and next results
2162 if ($paged < 2 || $paged >= $max_num_pages) {
2166 if ( $max_num_pages > 1 ) {
2167 $return = get_previous_posts_link($args['prelabel']);
2168 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2169 $return .= get_next_posts_link($args['nxtlabel']);
2177 * Display post pages link navigation for previous and next pages.
2181 * @param string $sep Optional. Separator for posts navigation links.
2182 * @param string $prelabel Optional. Label for previous pages.
2183 * @param string $nxtlabel Optional Label for next pages.
2185 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2186 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2187 echo get_posts_nav_link($args);
2191 * Retrieve comments page number link.
2195 * @param int $pagenum Optional. Page number.
2198 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2201 $pagenum = (int) $pagenum;
2203 $result = get_permalink();
2205 if ( 'newest' == get_option('default_comments_page') ) {
2206 if ( $pagenum != $max_page ) {
2207 if ( $wp_rewrite->using_permalinks() )
2208 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
2210 $result = add_query_arg( 'cpage', $pagenum, $result );
2212 } elseif ( $pagenum > 1 ) {
2213 if ( $wp_rewrite->using_permalinks() )
2214 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
2216 $result = add_query_arg( 'cpage', $pagenum, $result );
2219 $result .= '#comments';
2222 * Filter the comments page number link for the current request.
2226 * @param string $result The comments page number link.
2228 $result = apply_filters( 'get_comments_pagenum_link', $result );
2234 * Return the link to next comments page.
2238 * @param string $label Optional. Label for link text.
2239 * @param int $max_page Optional. Max page.
2240 * @return string|null
2242 function get_next_comments_link( $label = '', $max_page = 0 ) {
2245 if ( !is_singular() || !get_option('page_comments') )
2248 $page = get_query_var('cpage');
2250 $nextpage = intval($page) + 1;
2252 if ( empty($max_page) )
2253 $max_page = $wp_query->max_num_comment_pages;
2255 if ( empty($max_page) )
2256 $max_page = get_comment_pages_count();
2258 if ( $nextpage > $max_page )
2261 if ( empty($label) )
2262 $label = __('Newer Comments »');
2265 * Filter the anchor tag attributes for the next comments page link.
2269 * @param string $attributes Attributes for the anchor tag.
2271 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>';
2275 * Display the link to next comments page.
2279 * @param string $label Optional. Label for link text.
2280 * @param int $max_page Optional. Max page.
2282 function next_comments_link( $label = '', $max_page = 0 ) {
2283 echo get_next_comments_link( $label, $max_page );
2287 * Return the previous comments page link.
2291 * @param string $label Optional. Label for comments link text.
2292 * @return string|null
2294 function get_previous_comments_link( $label = '' ) {
2295 if ( !is_singular() || !get_option('page_comments') )
2298 $page = get_query_var('cpage');
2300 if ( intval($page) <= 1 )
2303 $prevpage = intval($page) - 1;
2305 if ( empty($label) )
2306 $label = __('« Older Comments');
2309 * Filter the anchor tag attributes for the previous comments page link.
2313 * @param string $attributes Attributes for the anchor tag.
2315 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>';
2319 * Display the previous comments page link.
2323 * @param string $label Optional. Label for comments link text.
2325 function previous_comments_link( $label = '' ) {
2326 echo get_previous_comments_link( $label );
2330 * Create pagination links for the comments on the current post.
2332 * @see paginate_links()
2335 * @param string|array $args Optional args. See paginate_links().
2336 * @return string Markup for pagination links.
2338 function paginate_comments_links($args = array()) {
2341 if ( !is_singular() || !get_option('page_comments') )
2344 $page = get_query_var('cpage');
2347 $max_page = get_comment_pages_count();
2349 'base' => add_query_arg( 'cpage', '%#%' ),
2351 'total' => $max_page,
2354 'add_fragment' => '#comments'
2356 if ( $wp_rewrite->using_permalinks() )
2357 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
2359 $args = wp_parse_args( $args, $defaults );
2360 $page_links = paginate_links( $args );
2362 if ( $args['echo'] )
2369 * Retrieve the Press This bookmarklet link.
2371 * Use this in 'a' element 'href' attribute.
2377 function get_shortcut_link() {
2378 // In case of breaking changes, version this. #WP20071
2379 $link = "javascript:
2385 s=(e?e():(k)?k():(x?x.createRange().text:0)),
2386 f='" . admin_url('press-this.php') . "',
2388 e=encodeURIComponent,
2389 u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
2390 a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
2391 if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
2394 $link = str_replace(array("\r", "\n", "\t"), '', $link);
2397 * Filter the Press This bookmarklet link.
2401 * @param string $link The Press This bookmarklet link.
2403 return apply_filters( 'shortcut_link', $link );
2407 * Retrieve the home url for the current site.
2409 * Returns the 'home' option with the appropriate protocol, 'https' if
2410 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2415 * @uses get_home_url()
2417 * @param string $path (optional) Path relative to the home url.
2418 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2419 * @return string Home url link with optional path appended.
2421 function home_url( $path = '', $scheme = null ) {
2422 return get_home_url( null, $path, $scheme );
2426 * Retrieve the home url for a given site.
2428 * Returns the 'home' option with the appropriate protocol, 'https' if
2429 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2434 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2435 * @param string $path (optional) Path relative to the home url.
2436 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2437 * @return string Home url link with optional path appended.
2439 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2440 $orig_scheme = $scheme;
2442 if ( empty( $blog_id ) || !is_multisite() ) {
2443 $url = get_option( 'home' );
2445 switch_to_blog( $blog_id );
2446 $url = get_option( 'home' );
2447 restore_current_blog();
2450 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2451 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
2454 $scheme = parse_url( $url, PHP_URL_SCHEME );
2457 $url = set_url_scheme( $url, $scheme );
2459 if ( $path && is_string( $path ) )
2460 $url .= '/' . ltrim( $path, '/' );
2463 * Filter the home URL.
2467 * @param string $url The complete home URL including scheme and path.
2468 * @param string $path Path relative to the home URL. Blank string if no path is specified.
2469 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
2470 * @param int|null $blog_id Blog ID, or null for the current blog.
2472 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
2476 * Retrieve the site url for the current site.
2478 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2479 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2484 * @uses get_site_url()
2486 * @param string $path Optional. Path relative to the site url.
2487 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2488 * @return string Site url link with optional path appended.
2490 function site_url( $path = '', $scheme = null ) {
2491 return get_site_url( null, $path, $scheme );
2495 * Retrieve the site url for a given site.
2497 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2498 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2503 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2504 * @param string $path Optional. Path relative to the site url.
2505 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
2506 * @return string Site url link with optional path appended.
2508 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2509 if ( empty( $blog_id ) || !is_multisite() ) {
2510 $url = get_option( 'siteurl' );
2512 switch_to_blog( $blog_id );
2513 $url = get_option( 'siteurl' );
2514 restore_current_blog();
2517 $url = set_url_scheme( $url, $scheme );
2519 if ( $path && is_string( $path ) )
2520 $url .= '/' . ltrim( $path, '/' );
2523 * Filter the site URL.
2527 * @param string $url The complete site URL including scheme and path.
2528 * @param string $path Path relative to the site URL. Blank string if no path is specified.
2529 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
2530 * 'login_post', 'admin', 'relative' or null.
2531 * @param int|null $blog_id Blog ID, or null for the current blog.
2533 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
2537 * Retrieve the url to the admin area for the current site.
2541 * @param string $path Optional path relative to the admin url.
2542 * @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.
2543 * @return string Admin url link with optional path appended.
2545 function admin_url( $path = '', $scheme = 'admin' ) {
2546 return get_admin_url( null, $path, $scheme );
2550 * Retrieve the url to the admin area for a given site.
2554 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2555 * @param string $path Optional path relative to the admin url.
2556 * @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.
2557 * @return string Admin url link with optional path appended.
2559 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2560 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2562 if ( $path && is_string( $path ) )
2563 $url .= ltrim( $path, '/' );
2566 * Filter the admin area URL.
2570 * @param string $url The complete admin area URL including scheme and path.
2571 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
2572 * @param int|null $blog_id Blog ID, or null for the current blog.
2574 return apply_filters( 'admin_url', $url, $path, $blog_id );
2578 * Retrieve the url to the includes directory.
2582 * @param string $path Optional. Path relative to the includes url.
2583 * @param string $scheme Optional. Scheme to give the includes url context.
2584 * @return string Includes url link with optional path appended.
2586 function includes_url( $path = '', $scheme = null ) {
2587 $url = site_url( '/' . WPINC . '/', $scheme );
2589 if ( $path && is_string( $path ) )
2590 $url .= ltrim($path, '/');
2593 * Filter the URL to the includes directory.
2597 * @param string $url The complete URL to the includes directory including scheme and path.
2598 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
2599 * if no path is specified.
2601 return apply_filters( 'includes_url', $url, $path );
2605 * Retrieve the url to the content directory.
2609 * @param string $path Optional. Path relative to the content url.
2610 * @return string Content url link with optional path appended.
2612 function content_url($path = '') {
2613 $url = set_url_scheme( WP_CONTENT_URL );
2615 if ( $path && is_string( $path ) )
2616 $url .= '/' . ltrim($path, '/');
2619 * Filter the URL to the content directory.
2623 * @param string $url The complete URL to the content directory including scheme and path.
2624 * @param string $path Path relative to the URL to the content directory. Blank string
2625 * if no path is specified.
2627 return apply_filters( 'content_url', $url, $path);
2631 * Retrieve a URL within the plugins or mu-plugins directory.
2633 * Defaults to the plugins directory URL if no arguments are supplied.
2637 * @param string $path Optional. Extra path appended to the end of the URL, including
2638 * the relative directory if $plugin is supplied. Default empty.
2639 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
2640 * The URL will be relative to its directory. Default empty.
2641 * Typically this is done by passing `__FILE__` as the argument.
2642 * @return string Plugins URL link with optional paths appended.
2644 function plugins_url( $path = '', $plugin = '' ) {
2646 $path = wp_normalize_path( $path );
2647 $plugin = wp_normalize_path( $plugin );
2648 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
2650 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2651 $url = WPMU_PLUGIN_URL;
2653 $url = WP_PLUGIN_URL;
2656 $url = set_url_scheme( $url );
2658 if ( !empty($plugin) && is_string($plugin) ) {
2659 $folder = dirname(plugin_basename($plugin));
2660 if ( '.' != $folder )
2661 $url .= '/' . ltrim($folder, '/');
2664 if ( $path && is_string( $path ) )
2665 $url .= '/' . ltrim($path, '/');
2668 * Filter the URL to the plugins directory.
2672 * @param string $url The complete URL to the plugins directory including scheme and path.
2673 * @param string $path Path relative to the URL to the plugins directory. Blank string
2674 * if no path is specified.
2675 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
2678 return apply_filters( 'plugins_url', $url, $path, $plugin );
2682 * Retrieve the site url for the current network.
2684 * Returns the site url with the appropriate protocol, 'https' if
2685 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2690 * @param string $path Optional. Path relative to the site url.
2691 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2692 * @return string Site url link with optional path appended.
2694 function network_site_url( $path = '', $scheme = null ) {
2695 if ( ! is_multisite() )
2696 return site_url($path, $scheme);
2698 $current_site = get_current_site();
2700 if ( 'relative' == $scheme )
2701 $url = $current_site->path;
2703 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2705 if ( $path && is_string( $path ) )
2706 $url .= ltrim( $path, '/' );
2709 * Filter the network site URL.
2713 * @param string $url The complete network site URL including scheme and path.
2714 * @param string $path Path relative to the network site URL. Blank string if
2715 * no path is specified.
2716 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
2717 * 'relative' or null.
2719 return apply_filters( 'network_site_url', $url, $path, $scheme );
2723 * Retrieve the home url for the current network.
2725 * Returns the home url with the appropriate protocol, 'https' if
2726 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2731 * @param string $path (optional) Path relative to the home url.
2732 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2733 * @return string Home url link with optional path appended.
2735 function network_home_url( $path = '', $scheme = null ) {
2736 if ( ! is_multisite() )
2737 return home_url($path, $scheme);
2739 $current_site = get_current_site();
2740 $orig_scheme = $scheme;
2742 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
2743 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
2745 if ( 'relative' == $scheme )
2746 $url = $current_site->path;
2748 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2750 if ( $path && is_string( $path ) )
2751 $url .= ltrim( $path, '/' );
2754 * Filter the network home URL.
2758 * @param string $url The complete network home URL including scheme and path.
2759 * @param string $path Path relative to the network home URL. Blank string
2760 * if no path is specified.
2761 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
2762 * 'relative' or null.
2764 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
2768 * Retrieve the url to the admin area for the network.
2772 * @param string $path Optional path relative to the admin url.
2773 * @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.
2774 * @return string Admin url link with optional path appended.
2776 function network_admin_url( $path = '', $scheme = 'admin' ) {
2777 if ( ! is_multisite() )
2778 return admin_url( $path, $scheme );
2780 $url = network_site_url('wp-admin/network/', $scheme);
2782 if ( $path && is_string( $path ) )
2783 $url .= ltrim($path, '/');
2786 * Filter the network admin URL.
2790 * @param string $url The complete network admin URL including scheme and path.
2791 * @param string $path Path relative to the network admin URL. Blank string if
2792 * no path is specified.
2794 return apply_filters( 'network_admin_url', $url, $path );
2798 * Retrieve the url to the admin area for the current user.
2802 * @param string $path Optional path relative to the admin url.
2803 * @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.
2804 * @return string Admin url link with optional path appended.
2806 function user_admin_url( $path = '', $scheme = 'admin' ) {
2807 $url = network_site_url('wp-admin/user/', $scheme);
2809 if ( $path && is_string( $path ) )
2810 $url .= ltrim($path, '/');
2813 * Filter the user admin URL for the current user.
2817 * @param string $url The complete URL including scheme and path.
2818 * @param string $path Path relative to the URL. Blank string if
2819 * no path is specified.
2821 return apply_filters( 'user_admin_url', $url, $path );
2825 * Retrieve the url to the admin area for either the current blog or the network depending on context.
2829 * @param string $path Optional path relative to the admin url.
2830 * @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.
2831 * @return string Admin url link with optional path appended.
2833 function self_admin_url($path = '', $scheme = 'admin') {
2834 if ( is_network_admin() )
2835 return network_admin_url($path, $scheme);
2836 elseif ( is_user_admin() )
2837 return user_admin_url($path, $scheme);
2839 return admin_url($path, $scheme);
2843 * Set the scheme for a URL
2847 * @param string $url Absolute url that includes a scheme
2848 * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
2849 * @return string $url URL with chosen scheme.
2851 function set_url_scheme( $url, $scheme = null ) {
2852 $orig_scheme = $scheme;
2855 $scheme = is_ssl() ? 'https' : 'http';
2856 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
2857 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
2858 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
2859 $scheme = is_ssl() ? 'https' : 'http';
2862 $url = trim( $url );
2863 if ( substr( $url, 0, 2 ) === '//' )
2864 $url = 'http:' . $url;
2866 if ( 'relative' == $scheme ) {
2867 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
2868 if ( $url !== '' && $url[0] === '/' )
2869 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
2871 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
2875 * Filter the resulting URL after setting the scheme.
2879 * @param string $url The complete URL including scheme and path.
2880 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
2881 * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
2882 * 'login_post', 'admin', 'rpc', or 'relative'.
2884 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
2888 * Get the URL to the user's dashboard.
2890 * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
2891 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
2892 * primary blog is returned.
2896 * @param int $user_id Optional. User ID. Defaults to current user.
2897 * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
2898 * @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.
2899 * @return string Dashboard url link with optional path appended.
2901 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
2902 $user_id = $user_id ? (int) $user_id : get_current_user_id();
2904 $blogs = get_blogs_of_user( $user_id );
2905 if ( ! is_super_admin() && empty($blogs) ) {
2906 $url = user_admin_url( $path, $scheme );
2907 } elseif ( ! is_multisite() ) {
2908 $url = admin_url( $path, $scheme );
2910 $current_blog = get_current_blog_id();
2911 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
2912 $url = admin_url( $path, $scheme );
2914 $active = get_active_blog_for_user( $user_id );
2916 $url = get_admin_url( $active->blog_id, $path, $scheme );
2918 $url = user_admin_url( $path, $scheme );
2923 * Filter the dashboard URL for a user.
2927 * @param string $url The complete URL including scheme and path.
2928 * @param int $user_id The user ID.
2929 * @param string $path Path relative to the URL. Blank string if no path is specified.
2930 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
2931 * 'login_post', 'admin', 'relative' or null.
2933 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
2937 * Get the URL to the user's profile editor.
2941 * @param int $user_id Optional. User ID. Defaults to current user.
2942 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
2943 * 'http' or 'https' can be passed to force those schemes.
2944 * @return string Dashboard url link with optional path appended.
2946 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
2947 $user_id = $user_id ? (int) $user_id : get_current_user_id();
2949 if ( is_user_admin() )
2950 $url = user_admin_url( 'profile.php', $scheme );
2951 elseif ( is_network_admin() )
2952 $url = network_admin_url( 'profile.php', $scheme );
2954 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
2957 * Filter the URL for a user's profile editor.
2961 * @param string $url The complete URL including scheme and path.
2962 * @param int $user_id The user ID.
2963 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
2964 * 'login_post', 'admin', 'relative' or null.
2966 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
2970 * Output rel=canonical for singular queries.
2974 function rel_canonical() {
2975 if ( !is_singular() )
2978 global $wp_the_query;
2979 if ( !$id = $wp_the_query->get_queried_object_id() )
2982 $link = get_permalink( $id );
2984 if ( $page = get_query_var('cpage') )
2985 $link = get_comments_pagenum_link( $page );
2987 echo "<link rel='canonical' href='$link' />\n";
2991 * Return a shortlink for a post, page, attachment, or blog.
2993 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
2994 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
2995 * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
2996 * via the get_shortlink filter.
3000 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
3001 * @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'.
3002 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3003 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3005 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3007 * Filter whether to preempt generating a shortlink for the given post.
3009 * Passing a truthy value to the filter will effectively short-circuit the
3010 * shortlink-generation process, returning that value instead.
3014 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3015 * @param int $id Post ID, or 0 for the current post.
3016 * @param string $context The context for the link. One of 'post' or 'query',
3017 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3019 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3021 if ( false !== $shortlink )
3026 if ( 'query' == $context && is_singular() ) {
3027 $post_id = $wp_query->get_queried_object_id();
3028 $post = get_post( $post_id );
3029 } elseif ( 'post' == $context ) {
3030 $post = get_post( $id );
3031 if ( ! empty( $post->ID ) )
3032 $post_id = $post->ID;
3037 // Return p= link for all public post types.
3038 if ( ! empty( $post_id ) ) {
3039 $post_type = get_post_type_object( $post->post_type );
3041 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3042 $shortlink = home_url( '/' );
3043 } elseif ( $post_type->public ) {
3044 $shortlink = home_url( '?p=' . $post_id );
3049 * Filter the shortlink for a post.
3053 * @param string $shortlink Shortlink URL.
3054 * @param int $id Post ID, or 0 for the current post.
3055 * @param string $context The context for the link. One of 'post' or 'query',
3056 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3058 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3062 * Inject rel=shortlink into head if a shortlink is defined for the current page.
3064 * Attached to the wp_head action.
3068 * @uses wp_get_shortlink()
3070 function wp_shortlink_wp_head() {
3071 $shortlink = wp_get_shortlink( 0, 'query' );
3073 if ( empty( $shortlink ) )
3076 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3080 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3082 * Attached to the wp action.
3086 * @uses wp_get_shortlink()
3088 function wp_shortlink_header() {
3089 if ( headers_sent() )
3092 $shortlink = wp_get_shortlink(0, 'query');
3094 if ( empty($shortlink) )
3097 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3101 * Display the Short Link for a Post
3103 * Must be called from inside "The Loop"
3105 * Call like the_shortlink(__('Shortlinkage FTW'))
3109 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3110 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3111 * @param string $before Optional HTML to display before the link.
3112 * @param string $after Optional HTML to display after the link.
3114 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3117 if ( empty( $text ) )
3118 $text = __('This is the short link.');
3120 if ( empty( $title ) )
3121 $title = the_title_attribute( array( 'echo' => false ) );
3123 $shortlink = wp_get_shortlink( $post->ID );
3125 if ( !empty( $shortlink ) ) {
3126 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3129 * Filter the shortlink anchor tag for a post.
3133 * @param string $link Shortlink anchor tag.
3134 * @param string $shortlink Shortlink URL.
3135 * @param string $text Shortlink's text.
3136 * @param string $title Shortlink's title attribute.
3138 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3139 echo $before, $link, $after;