3 * WordPress Link Template Functions
10 * Display the permalink for the current post.
13 * @uses apply_filters() Calls 'the_permalink' filter on the permalink string.
15 function the_permalink() {
16 echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
20 * Retrieve trailing slash string, if blog set for adding trailing slashes.
22 * Conditionally adds a trailing slash if the permalink structure has a trailing
23 * slash, strips the trailing slash if not. The string is passed through the
24 * 'user_trailingslashit' filter. Will remove trailing slash from string, if
25 * blog is not set to have them.
30 * @param string $string URL with or without a trailing slash.
31 * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
34 function user_trailingslashit($string, $type_of_url = '') {
36 if ( $wp_rewrite->use_trailing_slashes )
37 $string = trailingslashit($string);
39 $string = untrailingslashit($string);
41 // Note that $type_of_url can be one of following:
42 // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged, post_type_archive
43 $string = apply_filters('user_trailingslashit', $string, $type_of_url);
48 * Display permalink anchor for current post.
50 * The permalink mode title will use the post title for the 'a' element 'id'
51 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
55 * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
57 function permalink_anchor( $mode = 'id' ) {
59 switch ( strtolower( $mode ) ) {
61 $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
62 echo '<a id="'.$title.'"></a>';
66 echo '<a id="post-' . $post->ID . '"></a>';
72 * Retrieve full permalink for current post or post ID.
76 * @param int $id Optional. Post ID.
77 * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
80 function get_permalink( $id = 0, $leavename = false ) {
88 $leavename? '' : '%postname%',
92 $leavename? '' : '%pagename%',
95 if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
99 $post = get_post($id);
103 if ( empty($post->ID) )
106 if ( $post->post_type == 'page' )
107 return get_page_link($post->ID, $leavename, $sample);
108 elseif ( $post->post_type == 'attachment' )
109 return get_attachment_link( $post->ID, $leavename );
110 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
111 return get_post_permalink($post->ID, $leavename, $sample);
113 $permalink = get_option('permalink_structure');
115 $permalink = apply_filters('pre_post_link', $permalink, $post, $leavename);
117 if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
118 $unixtime = strtotime($post->post_date);
121 if ( strpos($permalink, '%category%') !== false ) {
122 $cats = get_the_category($post->ID);
124 usort($cats, '_usort_terms_by_ID'); // order by ID
125 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
126 $category_object = get_term( $category_object, 'category' );
127 $category = $category_object->slug;
128 if ( $parent = $category_object->parent )
129 $category = get_category_parents($parent, false, '/', true) . $category;
131 // show default category in permalinks, without
132 // having to assign it explicitly
133 if ( empty($category) ) {
134 $default_category = get_category( get_option( 'default_category' ) );
135 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
140 if ( strpos($permalink, '%author%') !== false ) {
141 $authordata = get_userdata($post->post_author);
142 $author = $authordata->user_nicename;
145 $date = explode(" ",date('Y m d H i s', $unixtime));
160 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
161 $permalink = user_trailingslashit($permalink, 'single');
162 } else { // if they're not using the fancy permalink option
163 $permalink = home_url('?p=' . $post->ID);
165 return apply_filters('post_link', $permalink, $post, $leavename);
169 * Retrieve the permalink for a post with a custom post type.
173 * @param int $id Optional. Post ID.
174 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
175 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
178 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
181 $post = get_post($id);
183 if ( is_wp_error( $post ) )
186 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
188 $slug = $post->post_name;
190 $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
192 $post_type = get_post_type_object($post->post_type);
194 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
195 if ( ! $leavename ) {
196 if ( $post_type->hierarchical )
197 $slug = get_page_uri($id);
198 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
200 $post_link = home_url( user_trailingslashit($post_link) );
202 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
203 $post_link = add_query_arg($post_type->query_var, $slug, '');
205 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
206 $post_link = home_url($post_link);
209 return apply_filters('post_type_link', $post_link, $post, $leavename, $sample);
213 * Retrieve permalink from post ID.
217 * @param int $post_id Optional. Post ID.
218 * @param mixed $deprecated Not used.
221 function post_permalink( $post_id = 0, $deprecated = '' ) {
222 if ( !empty( $deprecated ) )
223 _deprecated_argument( __FUNCTION__, '1.3' );
225 return get_permalink($post_id);
229 * Retrieve the permalink for current page or page ID.
231 * Respects page_on_front. Use this one.
235 * @param int|object $post Optional. Post ID or object.
236 * @param bool $leavename Optional, defaults to false. Whether to keep page name.
237 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
240 function get_page_link( $post = false, $leavename = false, $sample = false ) {
241 $post = get_post( $post );
243 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
244 $link = home_url('/');
246 $link = _get_page_link( $post, $leavename, $sample );
248 return apply_filters( 'page_link', $link, $post->ID, $sample );
252 * Retrieve the page permalink.
254 * Ignores page_on_front. Internal use only.
259 * @param int|object $post Optional. Post ID or object.
260 * @param bool $leavename Optional. Leave name.
261 * @param bool $sample Optional. Sample permalink.
264 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
267 $post = get_post( $post );
269 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
271 $link = $wp_rewrite->get_page_permastruct();
273 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
274 if ( ! $leavename ) {
275 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
278 $link = home_url($link);
279 $link = user_trailingslashit($link, 'page');
281 $link = home_url( '?page_id=' . $post->ID );
284 return apply_filters( '_get_page_link', $link, $post->ID );
288 * Retrieve permalink for attachment.
290 * This can be used in the WordPress Loop or outside of it.
294 * @param int|object $post Optional. Post ID or object.
295 * @param bool $leavename Optional. Leave name.
298 function get_attachment_link( $post = null, $leavename = false ) {
303 $post = get_post( $post );
304 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
306 if ( $wp_rewrite->using_permalinks() && $parent ) {
307 if ( 'page' == $parent->post_type )
308 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
310 $parentlink = get_permalink( $post->post_parent );
312 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
313 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
315 $name = $post->post_name;
317 if ( strpos($parentlink, '?') === false )
318 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
321 $link = str_replace( '%postname%', $name, $link );
325 $link = home_url( '/?attachment_id=' . $post->ID );
327 return apply_filters( 'attachment_link', $link, $post->ID );
331 * Retrieve the permalink for the year archives.
335 * @param int|bool $year False for current year or year for permalink.
338 function get_year_link($year) {
341 $year = gmdate('Y', current_time('timestamp'));
342 $yearlink = $wp_rewrite->get_year_permastruct();
343 if ( !empty($yearlink) ) {
344 $yearlink = str_replace('%year%', $year, $yearlink);
345 return apply_filters('year_link', home_url( user_trailingslashit($yearlink, 'year') ), $year);
347 return apply_filters('year_link', home_url('?m=' . $year), $year);
352 * Retrieve the permalink for the month archives with year.
356 * @param bool|int $year False for current year. Integer of year.
357 * @param bool|int $month False for current month. Integer of month.
360 function get_month_link($year, $month) {
363 $year = gmdate('Y', current_time('timestamp'));
365 $month = gmdate('m', current_time('timestamp'));
366 $monthlink = $wp_rewrite->get_month_permastruct();
367 if ( !empty($monthlink) ) {
368 $monthlink = str_replace('%year%', $year, $monthlink);
369 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
370 return apply_filters('month_link', home_url( user_trailingslashit($monthlink, 'month') ), $year, $month);
372 return apply_filters('month_link', home_url( '?m=' . $year . zeroise($month, 2) ), $year, $month);
377 * Retrieve the permalink for the day archives with year and month.
381 * @param bool|int $year False for current year. Integer of year.
382 * @param bool|int $month False for current month. Integer of month.
383 * @param bool|int $day False for current day. Integer of day.
386 function get_day_link($year, $month, $day) {
389 $year = gmdate('Y', current_time('timestamp'));
391 $month = gmdate('m', current_time('timestamp'));
393 $day = gmdate('j', current_time('timestamp'));
395 $daylink = $wp_rewrite->get_day_permastruct();
396 if ( !empty($daylink) ) {
397 $daylink = str_replace('%year%', $year, $daylink);
398 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
399 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
400 return apply_filters('day_link', home_url( user_trailingslashit($daylink, 'day') ), $year, $month, $day);
402 return apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2) . zeroise($day, 2) ), $year, $month, $day);
407 * Display the permalink for the feed type.
411 * @param string $anchor The link's anchor text.
412 * @param string $feed Optional, defaults to default feed. Feed type.
414 function the_feed_link( $anchor, $feed = '' ) {
415 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
416 echo apply_filters( 'the_feed_link', $link, $feed );
420 * Retrieve the permalink for the feed type.
424 * @param string $feed Optional, defaults to default feed. Feed type.
427 function get_feed_link($feed = '') {
430 $permalink = $wp_rewrite->get_feed_permastruct();
431 if ( '' != $permalink ) {
432 if ( false !== strpos($feed, 'comments_') ) {
433 $feed = str_replace('comments_', '', $feed);
434 $permalink = $wp_rewrite->get_comment_feed_permastruct();
437 if ( get_default_feed() == $feed )
440 $permalink = str_replace('%feed%', $feed, $permalink);
441 $permalink = preg_replace('#/+#', '/', "/$permalink");
442 $output = home_url( user_trailingslashit($permalink, 'feed') );
445 $feed = get_default_feed();
447 if ( false !== strpos($feed, 'comments_') )
448 $feed = str_replace('comments_', 'comments-', $feed);
450 $output = home_url("?feed={$feed}");
453 return apply_filters('feed_link', $output, $feed);
457 * Retrieve the permalink for the post comments feed.
461 * @param int $post_id Optional. Post ID.
462 * @param string $feed Optional. Feed type.
465 function get_post_comments_feed_link($post_id = 0, $feed = '') {
466 $post_id = absint( $post_id );
469 $post_id = get_the_ID();
471 if ( empty( $feed ) )
472 $feed = get_default_feed();
474 if ( '' != get_option('permalink_structure') ) {
475 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
476 $url = _get_page_link( $post_id );
478 $url = get_permalink($post_id);
480 $url = trailingslashit($url) . 'feed';
481 if ( $feed != get_default_feed() )
483 $url = user_trailingslashit($url, 'single_feed');
485 $type = get_post_field('post_type', $post_id);
486 if ( 'page' == $type )
487 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
489 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
492 return apply_filters('post_comments_feed_link', $url);
496 * Display the comment feed link for a post.
498 * Prints out the comment feed link for a post. Link text is placed in the
499 * anchor. If no link text is specified, default text is used. If no post ID is
500 * specified, the current post is used.
506 * @param string $link_text Descriptive text.
507 * @param int $post_id Optional post ID. Default to current post.
508 * @param string $feed Optional. Feed format.
509 * @return string Link to the comment feed for the current post.
511 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
512 $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
513 if ( empty($link_text) )
514 $link_text = __('Comments Feed');
516 echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
520 * Retrieve the feed link for a given author.
522 * Returns a link to the feed for all posts by a given author. A specific feed
523 * can be requested or left blank to get the default feed.
529 * @param int $author_id ID of an author.
530 * @param string $feed Optional. Feed type.
531 * @return string Link to the feed for the author specified by $author_id.
533 function get_author_feed_link( $author_id, $feed = '' ) {
534 $author_id = (int) $author_id;
535 $permalink_structure = get_option('permalink_structure');
538 $feed = get_default_feed();
540 if ( '' == $permalink_structure ) {
541 $link = home_url("?feed=$feed&author=" . $author_id);
543 $link = get_author_posts_url($author_id);
544 if ( $feed == get_default_feed() )
547 $feed_link = "feed/$feed";
549 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
552 $link = apply_filters('author_feed_link', $link, $feed);
558 * Retrieve the feed link for a category.
560 * Returns a link to the feed for all posts in a given category. A specific feed
561 * can be requested or left blank to get the default feed.
567 * @param int $cat_id ID of a category.
568 * @param string $feed Optional. Feed type.
569 * @return string Link to the feed for the category specified by $cat_id.
571 function get_category_feed_link($cat_id, $feed = '') {
572 return get_term_feed_link($cat_id, 'category', $feed);
576 * Retrieve the feed link for a term.
578 * Returns a link to the feed for all posts in a given term. A specific feed
579 * can be requested or left blank to get the default feed.
583 * @param int $term_id ID of a category.
584 * @param string $taxonomy Optional. Taxonomy of $term_id
585 * @param string $feed Optional. Feed type.
586 * @return string Link to the feed for the term specified by $term_id and $taxonomy.
588 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
589 $term_id = ( int ) $term_id;
591 $term = get_term( $term_id, $taxonomy );
593 if ( empty( $term ) || is_wp_error( $term ) )
596 if ( empty( $feed ) )
597 $feed = get_default_feed();
599 $permalink_structure = get_option( 'permalink_structure' );
601 if ( '' == $permalink_structure ) {
602 if ( 'category' == $taxonomy ) {
603 $link = home_url("?feed=$feed&cat=$term_id");
605 elseif ( 'post_tag' == $taxonomy ) {
606 $link = home_url("?feed=$feed&tag=$term->slug");
608 $t = get_taxonomy( $taxonomy );
609 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
612 $link = get_term_link( $term_id, $term->taxonomy );
613 if ( $feed == get_default_feed() )
616 $feed_link = "feed/$feed";
618 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
621 if ( 'category' == $taxonomy )
622 $link = apply_filters( 'category_feed_link', $link, $feed );
623 elseif ( 'post_tag' == $taxonomy )
624 $link = apply_filters( 'tag_feed_link', $link, $feed );
626 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
632 * Retrieve permalink for feed of tag.
636 * @param int $tag_id Tag ID.
637 * @param string $feed Optional. Feed type.
640 function get_tag_feed_link($tag_id, $feed = '') {
641 return get_term_feed_link($tag_id, 'post_tag', $feed);
645 * Retrieve edit tag link.
649 * @param int $tag_id Tag ID
650 * @param string $taxonomy Taxonomy
653 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
654 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
658 * Display or retrieve edit tag link with formatting.
662 * @param string $link Optional. Anchor text.
663 * @param string $before Optional. Display before edit link.
664 * @param string $after Optional. Display after edit link.
665 * @param int|object $tag Tag object or ID
666 * @return string HTML content.
668 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
669 $link = edit_term_link( $link, '', '', false, $tag );
670 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
674 * Retrieve edit term url.
678 * @param int $term_id Term ID
679 * @param string $taxonomy Taxonomy
680 * @param string $object_type The object type
683 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
684 $tax = get_taxonomy( $taxonomy );
685 if ( !current_user_can( $tax->cap->edit_terms ) )
688 $term = get_term( $term_id, $taxonomy );
692 'taxonomy' => $taxonomy,
693 'tag_ID' => $term->term_id,
697 $args['post_type'] = $object_type;
699 $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
701 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
705 * Display or retrieve edit term link with formatting.
709 * @param string $link Optional. Anchor text.
710 * @param string $before Optional. Display before edit link.
711 * @param string $after Optional. Display after edit link.
712 * @param object $term Term object
713 * @return string HTML content.
715 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
716 if ( is_null( $term ) ) {
717 $term = get_queried_object();
720 $tax = get_taxonomy( $term->taxonomy );
721 if ( !current_user_can($tax->cap->edit_terms) )
724 if ( empty( $link ) )
725 $link = __('Edit This');
727 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '" title="' . $link . '">' . $link . '</a>';
728 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
737 * Retrieve permalink for search.
740 * @param string $query Optional. The query string to use. If empty the current query is used.
743 function get_search_link( $query = '' ) {
747 $search = get_search_query( false );
749 $search = stripslashes($query);
751 $permastruct = $wp_rewrite->get_search_permastruct();
753 if ( empty( $permastruct ) ) {
754 $link = home_url('?s=' . urlencode($search) );
756 $search = urlencode($search);
757 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
758 $link = str_replace( '%search%', $search, $permastruct );
759 $link = home_url( user_trailingslashit( $link, 'search' ) );
762 return apply_filters( 'search_link', $link, $search );
766 * Retrieve the permalink for the feed of the search results.
770 * @param string $search_query Optional. Search query.
771 * @param string $feed Optional. Feed type.
774 function get_search_feed_link($search_query = '', $feed = '') {
776 $link = get_search_link($search_query);
779 $feed = get_default_feed();
781 $permastruct = $wp_rewrite->get_search_permastruct();
783 if ( empty($permastruct) ) {
784 $link = add_query_arg('feed', $feed, $link);
786 $link = trailingslashit($link);
787 $link .= "feed/$feed/";
790 $link = apply_filters('search_feed_link', $link, $feed, 'posts');
796 * Retrieve the permalink for the comments feed of the search results.
800 * @param string $search_query Optional. Search query.
801 * @param string $feed Optional. Feed type.
804 function get_search_comments_feed_link($search_query = '', $feed = '') {
808 $feed = get_default_feed();
810 $link = get_search_feed_link($search_query, $feed);
812 $permastruct = $wp_rewrite->get_search_permastruct();
814 if ( empty($permastruct) )
815 $link = add_query_arg('feed', 'comments-' . $feed, $link);
817 $link = add_query_arg('withcomments', 1, $link);
819 $link = apply_filters('search_feed_link', $link, $feed, 'comments');
825 * Retrieve the permalink for a post type archive.
829 * @param string $post_type Post type
832 function get_post_type_archive_link( $post_type ) {
834 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
837 if ( ! $post_type_obj->has_archive )
840 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
841 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
842 if ( $post_type_obj->rewrite['with_front'] )
843 $struct = $wp_rewrite->front . $struct;
845 $struct = $wp_rewrite->root . $struct;
846 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
848 $link = home_url( '?post_type=' . $post_type );
851 return apply_filters( 'post_type_archive_link', $link, $post_type );
855 * Retrieve the permalink for a post type archive feed.
859 * @param string $post_type Post type
860 * @param string $feed Optional. Feed type
863 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
864 $default_feed = get_default_feed();
865 if ( empty( $feed ) )
866 $feed = $default_feed;
868 if ( ! $link = get_post_type_archive_link( $post_type ) )
871 $post_type_obj = get_post_type_object( $post_type );
872 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
873 $link = trailingslashit( $link );
875 if ( $feed != $default_feed )
878 $link = add_query_arg( 'feed', $feed, $link );
881 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
885 * Retrieve edit posts link for post.
887 * Can be used within the WordPress loop or outside of it. Can be used with
888 * pages, posts, attachments, and revisions.
892 * @param int $id Optional. Post ID.
893 * @param string $context Optional, defaults to display. How to write the '&', defaults to '&'.
896 function get_edit_post_link( $id = 0, $context = 'display' ) {
897 if ( ! $post = get_post( $id ) )
900 if ( 'revision' === $post->post_type )
902 elseif ( 'display' == $context )
903 $action = '&action=edit';
905 $action = '&action=edit';
907 $post_type_object = get_post_type_object( $post->post_type );
908 if ( !$post_type_object )
911 if ( !current_user_can( 'edit_post', $post->ID ) )
914 return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
918 * Display edit post link for post.
922 * @param string $link Optional. Anchor text.
923 * @param string $before Optional. Display before edit link.
924 * @param string $after Optional. Display after edit link.
925 * @param int $id Optional. Post ID.
927 function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
928 if ( !$post = get_post( $id ) )
931 if ( !$url = get_edit_post_link( $post->ID ) )
934 if ( null === $link )
935 $link = __('Edit This');
937 $post_type_obj = get_post_type_object( $post->post_type );
938 $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( $post_type_obj->labels->edit_item ) . '">' . $link . '</a>';
939 echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
943 * Retrieve delete posts link for post.
945 * Can be used within the WordPress loop or outside of it, with any post type.
949 * @param int $id Optional. Post ID.
950 * @param string $deprecated Not used.
951 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
954 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
955 if ( ! empty( $deprecated ) )
956 _deprecated_argument( __FUNCTION__, '3.0' );
958 if ( !$post = get_post( $id ) )
961 $post_type_object = get_post_type_object( $post->post_type );
962 if ( !$post_type_object )
965 if ( !current_user_can( 'delete_post', $post->ID ) )
968 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
970 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
972 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
976 * Retrieve edit comment link.
980 * @param int $comment_id Optional. Comment ID.
983 function get_edit_comment_link( $comment_id = 0 ) {
984 $comment = get_comment( $comment_id );
986 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
989 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
990 return apply_filters( 'get_edit_comment_link', $location );
994 * Display or retrieve edit comment link with formatting.
998 * @param string $link Optional. Anchor text.
999 * @param string $before Optional. Display before edit link.
1000 * @param string $after Optional. Display after edit link.
1001 * @return string|null HTML content, if $echo is set to false.
1003 function edit_comment_link( $link = null, $before = '', $after = '' ) {
1006 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1009 if ( null === $link )
1010 $link = __('Edit This');
1012 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . esc_attr__( 'Edit comment' ) . '">' . $link . '</a>';
1013 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
1017 * Display edit bookmark (literally a URL external to blog) link.
1021 * @param int $link Optional. Bookmark ID.
1024 function get_edit_bookmark_link( $link = 0 ) {
1025 $link = get_bookmark( $link );
1027 if ( !current_user_can('manage_links') )
1030 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1031 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1035 * Display edit bookmark (literally a URL external to blog) link anchor content.
1039 * @param string $link Optional. Anchor text.
1040 * @param string $before Optional. Display before edit link.
1041 * @param string $after Optional. Display after edit link.
1042 * @param int $bookmark Optional. Bookmark ID.
1044 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1045 $bookmark = get_bookmark($bookmark);
1047 if ( !current_user_can('manage_links') )
1051 $link = __('Edit This');
1053 $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '" title="' . esc_attr__( 'Edit Link' ) . '">' . $link . '</a>';
1054 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1058 * Retrieve edit user link
1062 * @param int $user_id Optional. User ID. Defaults to the current user.
1063 * @return string URL to edit user page or empty string.
1065 function get_edit_user_link( $user_id = null ) {
1067 $user_id = get_current_user_id();
1069 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1072 $user = get_userdata( $user_id );
1077 if ( get_current_user_id() == $user->ID )
1078 $link = get_edit_profile_url( $user->ID );
1080 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1082 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1088 * Retrieve previous post that is adjacent to current post.
1092 * @param bool $in_same_cat Optional. Whether post should be in a same category.
1093 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1094 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1096 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
1097 return get_adjacent_post($in_same_cat, $excluded_categories);
1101 * Retrieve next post that is adjacent to current post.
1105 * @param bool $in_same_cat Optional. Whether post should be in a same category.
1106 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1107 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1109 function get_next_post($in_same_cat = false, $excluded_categories = '') {
1110 return get_adjacent_post($in_same_cat, $excluded_categories, false);
1114 * Retrieve adjacent post.
1116 * Can either be next or previous post.
1120 * @param bool $in_same_cat Optional. Whether post should be in a same category.
1121 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1122 * @param bool $previous Optional. Whether to retrieve previous post.
1123 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1125 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
1128 if ( ! $post = get_post() )
1131 $current_post_date = $post->post_date;
1134 $posts_in_ex_cats_sql = '';
1135 if ( $in_same_cat || ! empty( $excluded_categories ) ) {
1136 $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";
1138 if ( $in_same_cat ) {
1139 if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
1141 $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
1142 if ( ! $cat_array || is_wp_error( $cat_array ) )
1144 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
1147 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
1148 if ( ! empty( $excluded_categories ) ) {
1149 if ( ! is_array( $excluded_categories ) ) {
1150 // back-compat, $excluded_categories used to be IDs separated by " and "
1151 if ( strpos( $excluded_categories, ' and ' ) !== false ) {
1152 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
1153 $excluded_categories = explode( ' and ', $excluded_categories );
1155 $excluded_categories = explode( ',', $excluded_categories );
1159 $excluded_categories = array_map( 'intval', $excluded_categories );
1161 if ( ! empty( $cat_array ) ) {
1162 $excluded_categories = array_diff($excluded_categories, $cat_array);
1163 $posts_in_ex_cats_sql = '';
1166 if ( !empty($excluded_categories) ) {
1167 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
1172 $adjacent = $previous ? 'previous' : 'next';
1173 $op = $previous ? '<' : '>';
1174 $order = $previous ? 'DESC' : 'ASC';
1176 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
1177 $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' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
1178 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1180 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1181 $query_key = 'adjacent_post_' . md5($query);
1182 $result = wp_cache_get($query_key, 'counts');
1183 if ( false !== $result ) {
1185 $result = get_post( $result );
1189 $result = $wpdb->get_var( $query );
1190 if ( null === $result )
1193 wp_cache_set($query_key, $result, 'counts');
1196 $result = get_post( $result );
1202 * Get adjacent post relational link.
1204 * Can either be next or previous post relational link.
1208 * @param string $title Optional. Link title format.
1209 * @param bool $in_same_cat Optional. Whether link should be in a same category.
1210 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1211 * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
1214 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
1215 if ( $previous && is_attachment() && $post = get_post() )
1216 $post = get_post( $post->post_parent );
1218 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
1223 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1225 if ( empty( $post_title ) )
1226 $post_title = $previous ? __('Previous Post') : __('Next Post');
1228 $date = mysql2date(get_option('date_format'), $post->post_date);
1230 $title = str_replace('%title', $post_title, $title);
1231 $title = str_replace('%date', $date, $title);
1233 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1234 $link .= esc_attr( $title );
1235 $link .= "' href='" . get_permalink($post) . "' />\n";
1237 $adjacent = $previous ? 'previous' : 'next';
1238 return apply_filters( "{$adjacent}_post_rel_link", $link );
1242 * Display relational links for the posts adjacent to the current post.
1246 * @param string $title Optional. Link title format.
1247 * @param bool $in_same_cat Optional. Whether link should be in a same category.
1248 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1250 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1251 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
1252 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
1256 * Display relational links for the posts adjacent to the current post for single post pages.
1258 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1262 function adjacent_posts_rel_link_wp_head() {
1263 if ( !is_singular() || is_attachment() )
1265 adjacent_posts_rel_link();
1269 * Display relational link for the next post adjacent to the current post.
1273 * @param string $title Optional. Link title format.
1274 * @param bool $in_same_cat Optional. Whether link should be in a same category.
1275 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1277 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1278 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
1282 * Display relational link for the previous post adjacent to the current post.
1286 * @param string $title Optional. Link title format.
1287 * @param bool $in_same_cat Optional. Whether link should be in a same category.
1288 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1290 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1291 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
1295 * Retrieve boundary post.
1297 * Boundary being either the first or last post by publish date within the constraints specified
1298 * by $in_same_cat or $excluded_categories.
1302 * @param bool $in_same_cat Optional. Whether returned post should be in a same category.
1303 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1304 * @param bool $start Optional. Whether to retrieve first or last post.
1307 function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
1309 if ( ! $post || ! is_single() || is_attachment() )
1312 $cat_array = array();
1313 if( ! is_array( $excluded_categories ) )
1314 $excluded_categories = explode( ',', $excluded_categories );
1316 if ( $in_same_cat || ! empty( $excluded_categories ) ) {
1318 $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
1320 if ( ! empty( $excluded_categories ) ) {
1321 $excluded_categories = array_map( 'intval', $excluded_categories );
1322 $excluded_categories = array_diff( $excluded_categories, $cat_array );
1324 $inverse_cats = array();
1325 foreach ( $excluded_categories as $excluded_category )
1326 $inverse_cats[] = $excluded_category * -1;
1327 $excluded_categories = $inverse_cats;
1331 $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
1333 $order = $start ? 'ASC' : 'DESC';
1335 return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
1339 * Display previous post link that is adjacent to the current post.
1343 * @param string $format Optional. Link anchor format.
1344 * @param string $link Optional. Link permalink format.
1345 * @param bool $in_same_cat Optional. Whether link should be in a same category.
1346 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1348 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1349 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
1353 * Display next post link that is adjacent to the current post.
1357 * @param string $format Optional. Link anchor format.
1358 * @param string $link Optional. Link permalink format.
1359 * @param bool $in_same_cat Optional. Whether link should be in a same category.
1360 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1362 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1363 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
1367 * Display adjacent post link.
1369 * Can be either next post link or previous.
1373 * @param string $format Link anchor format.
1374 * @param string $link Link permalink format.
1375 * @param bool $in_same_cat Optional. Whether link should be in a same category.
1376 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
1377 * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
1379 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
1380 if ( $previous && is_attachment() )
1381 $post = get_post( get_post()->post_parent );
1383 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
1388 $title = $post->post_title;
1390 if ( empty( $post->post_title ) )
1391 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1393 $title = apply_filters( 'the_title', $title, $post->ID );
1394 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1395 $rel = $previous ? 'prev' : 'next';
1397 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1398 $inlink = str_replace( '%title', $title, $link );
1399 $inlink = str_replace( '%date', $date, $inlink );
1400 $inlink = $string . $inlink . '</a>';
1402 $output = str_replace( '%link', $inlink, $format );
1405 $adjacent = $previous ? 'previous' : 'next';
1407 echo apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
1411 * Retrieve links for page numbers.
1415 * @param int $pagenum Optional. Page ID.
1416 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
1417 * Otherwise, prepares the URL with esc_url_raw().
1420 function get_pagenum_link($pagenum = 1, $escape = true ) {
1423 $pagenum = (int) $pagenum;
1425 $request = remove_query_arg( 'paged' );
1427 $home_root = parse_url(home_url());
1428 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1429 $home_root = preg_quote( $home_root, '|' );
1431 $request = preg_replace('|^'. $home_root . '|i', '', $request);
1432 $request = preg_replace('|^/+|', '', $request);
1434 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1435 $base = trailingslashit( get_bloginfo( 'url' ) );
1437 if ( $pagenum > 1 ) {
1438 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1440 $result = $base . $request;
1443 $qs_regex = '|\?.*?$|';
1444 preg_match( $qs_regex, $request, $qs_match );
1446 if ( !empty( $qs_match[0] ) ) {
1447 $query_string = $qs_match[0];
1448 $request = preg_replace( $qs_regex, '', $request );
1453 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
1454 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
1455 $request = ltrim($request, '/');
1457 $base = trailingslashit( get_bloginfo( 'url' ) );
1459 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1460 $base .= $wp_rewrite->index . '/';
1462 if ( $pagenum > 1 ) {
1463 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
1466 $result = $base . $request . $query_string;
1469 $result = apply_filters('get_pagenum_link', $result);
1472 return esc_url( $result );
1474 return esc_url_raw( $result );
1478 * Retrieve next posts page link.
1480 * Backported from 2.1.3 to 2.0.10.
1484 * @param int $max_page Optional. Max pages.
1487 function get_next_posts_page_link($max_page = 0) {
1490 if ( !is_single() ) {
1493 $nextpage = intval($paged) + 1;
1494 if ( !$max_page || $max_page >= $nextpage )
1495 return get_pagenum_link($nextpage);
1500 * Display or return the next posts page link.
1504 * @param int $max_page Optional. Max pages.
1505 * @param boolean $echo Optional. Echo or return;
1507 function next_posts( $max_page = 0, $echo = true ) {
1508 $output = esc_url( get_next_posts_page_link( $max_page ) );
1517 * Return the next posts page link.
1521 * @param string $label Content for link text.
1522 * @param int $max_page Optional. Max pages.
1523 * @return string|null
1525 function get_next_posts_link( $label = null, $max_page = 0 ) {
1526 global $paged, $wp_query;
1529 $max_page = $wp_query->max_num_pages;
1534 $nextpage = intval($paged) + 1;
1536 if ( null === $label )
1537 $label = __( 'Next Page »' );
1539 if ( !is_single() && ( $nextpage <= $max_page ) ) {
1540 $attr = apply_filters( 'next_posts_link_attributes', '' );
1541 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
1546 * Display the next posts page link.
1549 * @uses get_next_posts_link()
1551 * @param string $label Content for link text.
1552 * @param int $max_page Optional. Max pages.
1554 function next_posts_link( $label = null, $max_page = 0 ) {
1555 echo get_next_posts_link( $label, $max_page );
1559 * Retrieve previous posts page link.
1561 * Will only return string, if not on a single page or post.
1563 * Backported to 2.0.10 from 2.1.3.
1567 * @return string|null
1569 function get_previous_posts_page_link() {
1572 if ( !is_single() ) {
1573 $nextpage = intval($paged) - 1;
1574 if ( $nextpage < 1 )
1576 return get_pagenum_link($nextpage);
1581 * Display or return the previous posts page link.
1585 * @param boolean $echo Optional. Echo or return;
1587 function previous_posts( $echo = true ) {
1588 $output = esc_url( get_previous_posts_page_link() );
1597 * Return the previous posts page link.
1601 * @param string $label Optional. Previous page link text.
1602 * @return string|null
1604 function get_previous_posts_link( $label = null ) {
1607 if ( null === $label )
1608 $label = __( '« Previous Page' );
1610 if ( !is_single() && $paged > 1 ) {
1611 $attr = apply_filters( 'previous_posts_link_attributes', '' );
1612 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
1617 * Display the previous posts page link.
1620 * @uses get_previous_posts_link()
1622 * @param string $label Optional. Previous page link text.
1624 function previous_posts_link( $label = null ) {
1625 echo get_previous_posts_link( $label );
1629 * Return post pages link navigation for previous and next pages.
1633 * @param string|array $args Optional args.
1634 * @return string The posts link navigation.
1636 function get_posts_nav_link( $args = array() ) {
1641 if ( !is_singular() ) {
1643 'sep' => ' — ',
1644 'prelabel' => __('« Previous Page'),
1645 'nxtlabel' => __('Next Page »'),
1647 $args = wp_parse_args( $args, $defaults );
1649 $max_num_pages = $wp_query->max_num_pages;
1650 $paged = get_query_var('paged');
1652 //only have sep if there's both prev and next results
1653 if ($paged < 2 || $paged >= $max_num_pages) {
1657 if ( $max_num_pages > 1 ) {
1658 $return = get_previous_posts_link($args['prelabel']);
1659 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
1660 $return .= get_next_posts_link($args['nxtlabel']);
1668 * Display post pages link navigation for previous and next pages.
1672 * @param string $sep Optional. Separator for posts navigation links.
1673 * @param string $prelabel Optional. Label for previous pages.
1674 * @param string $nxtlabel Optional Label for next pages.
1676 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
1677 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
1678 echo get_posts_nav_link($args);
1682 * Retrieve comments page number link.
1686 * @param int $pagenum Optional. Page number.
1689 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
1692 $pagenum = (int) $pagenum;
1694 $result = get_permalink();
1696 if ( 'newest' == get_option('default_comments_page') ) {
1697 if ( $pagenum != $max_page ) {
1698 if ( $wp_rewrite->using_permalinks() )
1699 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1701 $result = add_query_arg( 'cpage', $pagenum, $result );
1703 } elseif ( $pagenum > 1 ) {
1704 if ( $wp_rewrite->using_permalinks() )
1705 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1707 $result = add_query_arg( 'cpage', $pagenum, $result );
1710 $result .= '#comments';
1712 $result = apply_filters('get_comments_pagenum_link', $result);
1718 * Return the link to next comments page.
1722 * @param string $label Optional. Label for link text.
1723 * @param int $max_page Optional. Max page.
1724 * @return string|null
1726 function get_next_comments_link( $label = '', $max_page = 0 ) {
1729 if ( !is_singular() || !get_option('page_comments') )
1732 $page = get_query_var('cpage');
1734 $nextpage = intval($page) + 1;
1736 if ( empty($max_page) )
1737 $max_page = $wp_query->max_num_comment_pages;
1739 if ( empty($max_page) )
1740 $max_page = get_comment_pages_count();
1742 if ( $nextpage > $max_page )
1745 if ( empty($label) )
1746 $label = __('Newer Comments »');
1748 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>';
1752 * Display the link to next comments page.
1756 * @param string $label Optional. Label for link text.
1757 * @param int $max_page Optional. Max page.
1759 function next_comments_link( $label = '', $max_page = 0 ) {
1760 echo get_next_comments_link( $label, $max_page );
1764 * Return the previous comments page link.
1768 * @param string $label Optional. Label for comments link text.
1769 * @return string|null
1771 function get_previous_comments_link( $label = '' ) {
1772 if ( !is_singular() || !get_option('page_comments') )
1775 $page = get_query_var('cpage');
1777 if ( intval($page) <= 1 )
1780 $prevpage = intval($page) - 1;
1782 if ( empty($label) )
1783 $label = __('« Older Comments');
1785 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>';
1789 * Display the previous comments page link.
1793 * @param string $label Optional. Label for comments link text.
1795 function previous_comments_link( $label = '' ) {
1796 echo get_previous_comments_link( $label );
1800 * Create pagination links for the comments on the current post.
1802 * @see paginate_links()
1805 * @param string|array $args Optional args. See paginate_links().
1806 * @return string Markup for pagination links.
1808 function paginate_comments_links($args = array()) {
1811 if ( !is_singular() || !get_option('page_comments') )
1814 $page = get_query_var('cpage');
1817 $max_page = get_comment_pages_count();
1819 'base' => add_query_arg( 'cpage', '%#%' ),
1821 'total' => $max_page,
1824 'add_fragment' => '#comments'
1826 if ( $wp_rewrite->using_permalinks() )
1827 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
1829 $args = wp_parse_args( $args, $defaults );
1830 $page_links = paginate_links( $args );
1832 if ( $args['echo'] )
1839 * Retrieve the Press This bookmarklet link.
1841 * Use this in 'a' element 'href' attribute.
1847 function get_shortcut_link() {
1848 // In case of breaking changes, version this. #WP20071
1849 $link = "javascript:
1855 s=(e?e():(k)?k():(x?x.createRange().text:0)),
1856 f='" . admin_url('press-this.php') . "',
1858 e=encodeURIComponent,
1859 u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
1860 a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
1861 if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
1864 $link = str_replace(array("\r", "\n", "\t"), '', $link);
1866 return apply_filters('shortcut_link', $link);
1870 * Retrieve the home url for the current site.
1872 * Returns the 'home' option with the appropriate protocol, 'https' if
1873 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1876 * @package WordPress
1879 * @uses get_home_url()
1881 * @param string $path (optional) Path relative to the home url.
1882 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
1883 * @return string Home url link with optional path appended.
1885 function home_url( $path = '', $scheme = null ) {
1886 return get_home_url( null, $path, $scheme );
1890 * Retrieve the home url for a given site.
1892 * Returns the 'home' option with the appropriate protocol, 'https' if
1893 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1896 * @package WordPress
1899 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
1900 * @param string $path (optional) Path relative to the home url.
1901 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
1902 * @return string Home url link with optional path appended.
1904 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
1905 $orig_scheme = $scheme;
1907 if ( empty( $blog_id ) || !is_multisite() ) {
1908 $url = get_option( 'home' );
1910 switch_to_blog( $blog_id );
1911 $url = get_option( 'home' );
1912 restore_current_blog();
1915 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
1916 if ( is_ssl() && ! is_admin() )
1919 $scheme = parse_url( $url, PHP_URL_SCHEME );
1922 $url = set_url_scheme( $url, $scheme );
1924 if ( $path && is_string( $path ) )
1925 $url .= '/' . ltrim( $path, '/' );
1927 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
1931 * Retrieve the site url for the current site.
1933 * Returns the 'site_url' option with the appropriate protocol, 'https' if
1934 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1937 * @package WordPress
1940 * @uses get_site_url()
1942 * @param string $path Optional. Path relative to the site url.
1943 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
1944 * @return string Site url link with optional path appended.
1946 function site_url( $path = '', $scheme = null ) {
1947 return get_site_url( null, $path, $scheme );
1951 * Retrieve the site url for a given site.
1953 * Returns the 'site_url' option with the appropriate protocol, 'https' if
1954 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1957 * @package WordPress
1960 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
1961 * @param string $path Optional. Path relative to the site url.
1962 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
1963 * @return string Site url link with optional path appended.
1965 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
1966 if ( empty( $blog_id ) || !is_multisite() ) {
1967 $url = get_option( 'siteurl' );
1969 switch_to_blog( $blog_id );
1970 $url = get_option( 'siteurl' );
1971 restore_current_blog();
1974 $url = set_url_scheme( $url, $scheme );
1976 if ( $path && is_string( $path ) )
1977 $url .= '/' . ltrim( $path, '/' );
1979 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
1983 * Retrieve the url to the admin area for the current site.
1985 * @package WordPress
1988 * @param string $path Optional path relative to the admin url.
1989 * @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.
1990 * @return string Admin url link with optional path appended.
1992 function admin_url( $path = '', $scheme = 'admin' ) {
1993 return get_admin_url( null, $path, $scheme );
1997 * Retrieve the url to the admin area for a given site.
1999 * @package WordPress
2002 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2003 * @param string $path Optional path relative to the admin url.
2004 * @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.
2005 * @return string Admin url link with optional path appended.
2007 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2008 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2010 if ( $path && is_string( $path ) )
2011 $url .= ltrim( $path, '/' );
2013 return apply_filters( 'admin_url', $url, $path, $blog_id );
2017 * Retrieve the url to the includes directory.
2019 * @package WordPress
2022 * @param string $path Optional. Path relative to the includes url.
2023 * @param string $scheme Optional. Scheme to give the includes url context.
2024 * @return string Includes url link with optional path appended.
2026 function includes_url( $path = '', $scheme = null ) {
2027 $url = site_url( '/' . WPINC . '/', $scheme );
2029 if ( $path && is_string( $path ) )
2030 $url .= ltrim($path, '/');
2032 return apply_filters('includes_url', $url, $path);
2036 * Retrieve the url to the content directory.
2038 * @package WordPress
2041 * @param string $path Optional. Path relative to the content url.
2042 * @return string Content url link with optional path appended.
2044 function content_url($path = '') {
2045 $url = set_url_scheme( WP_CONTENT_URL );
2047 if ( $path && is_string( $path ) )
2048 $url .= '/' . ltrim($path, '/');
2050 return apply_filters('content_url', $url, $path);
2054 * Retrieve the url to the plugins directory or to a specific file within that directory.
2055 * You can hardcode the plugin slug in $path or pass __FILE__ as a second argument to get the correct folder name.
2057 * @package WordPress
2060 * @param string $path Optional. Path relative to the plugins url.
2061 * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
2062 * @return string Plugins url link with optional path appended.
2064 function plugins_url($path = '', $plugin = '') {
2066 $mu_plugin_dir = WPMU_PLUGIN_DIR;
2067 foreach ( array('path', 'plugin', 'mu_plugin_dir') as $var ) {
2068 $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
2069 $$var = preg_replace('|/+|', '/', $$var);
2072 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2073 $url = WPMU_PLUGIN_URL;
2075 $url = WP_PLUGIN_URL;
2078 $url = set_url_scheme( $url );
2080 if ( !empty($plugin) && is_string($plugin) ) {
2081 $folder = dirname(plugin_basename($plugin));
2082 if ( '.' != $folder )
2083 $url .= '/' . ltrim($folder, '/');
2086 if ( $path && is_string( $path ) )
2087 $url .= '/' . ltrim($path, '/');
2089 return apply_filters('plugins_url', $url, $path, $plugin);
2093 * Retrieve the site url for the current network.
2095 * Returns the site url with the appropriate protocol, 'https' if
2096 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2099 * @package WordPress
2102 * @param string $path Optional. Path relative to the site url.
2103 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2104 * @return string Site url link with optional path appended.
2106 function network_site_url( $path = '', $scheme = null ) {
2107 global $current_site;
2109 if ( ! is_multisite() )
2110 return site_url($path, $scheme);
2112 if ( 'relative' == $scheme )
2113 $url = $current_site->path;
2115 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2117 if ( $path && is_string( $path ) )
2118 $url .= ltrim( $path, '/' );
2120 return apply_filters( 'network_site_url', $url, $path, $scheme );
2124 * Retrieve the home url for the current network.
2126 * Returns the home url with the appropriate protocol, 'https' if
2127 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2130 * @package WordPress
2133 * @param string $path (optional) Path relative to the home url.
2134 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2135 * @return string Home url link with optional path appended.
2137 function network_home_url( $path = '', $scheme = null ) {
2138 global $current_site;
2140 if ( ! is_multisite() )
2141 return home_url($path, $scheme);
2143 $orig_scheme = $scheme;
2145 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
2146 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
2148 if ( 'relative' == $scheme )
2149 $url = $current_site->path;
2151 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2153 if ( $path && is_string( $path ) )
2154 $url .= ltrim( $path, '/' );
2156 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
2160 * Retrieve the url to the admin area for the network.
2162 * @package WordPress
2165 * @param string $path Optional path relative to the admin url.
2166 * @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.
2167 * @return string Admin url link with optional path appended.
2169 function network_admin_url( $path = '', $scheme = 'admin' ) {
2170 if ( ! is_multisite() )
2171 return admin_url( $path, $scheme );
2173 $url = network_site_url('wp-admin/network/', $scheme);
2175 if ( $path && is_string( $path ) )
2176 $url .= ltrim($path, '/');
2178 return apply_filters('network_admin_url', $url, $path);
2182 * Retrieve the url to the admin area for the current user.
2184 * @package WordPress
2187 * @param string $path Optional path relative to the admin url.
2188 * @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.
2189 * @return string Admin url link with optional path appended.
2191 function user_admin_url( $path = '', $scheme = 'admin' ) {
2192 $url = network_site_url('wp-admin/user/', $scheme);
2194 if ( $path && is_string( $path ) )
2195 $url .= ltrim($path, '/');
2197 return apply_filters('user_admin_url', $url, $path);
2201 * Retrieve the url to the admin area for either the current blog or the network depending on context.
2203 * @package WordPress
2206 * @param string $path Optional path relative to the admin url.
2207 * @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.
2208 * @return string Admin url link with optional path appended.
2210 function self_admin_url($path = '', $scheme = 'admin') {
2211 if ( is_network_admin() )
2212 return network_admin_url($path, $scheme);
2213 elseif ( is_user_admin() )
2214 return user_admin_url($path, $scheme);
2216 return admin_url($path, $scheme);
2220 * Set the scheme for a URL
2224 * @param string $url Absolute url that includes a scheme
2225 * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
2226 * @return string $url URL with chosen scheme.
2228 function set_url_scheme( $url, $scheme = null ) {
2229 $orig_scheme = $scheme;
2230 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2231 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
2233 elseif ( ( 'login' == $scheme ) && force_ssl_admin() )
2235 elseif ( ( 'admin' == $scheme ) && force_ssl_admin() )
2238 $scheme = ( is_ssl() ? 'https' : 'http' );
2241 if ( 'relative' == $scheme )
2242 $url = preg_replace( '#^.+://[^/]*#', '', $url );
2244 $url = preg_replace( '#^.+://#', $scheme . '://', $url );
2246 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
2250 * Get the URL to the user's dashboard.
2252 * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
2253 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
2254 * primary blog is returned.
2258 * @param int $user_id User ID
2259 * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
2260 * @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.
2261 * @return string Dashboard url link with optional path appended.
2263 function get_dashboard_url( $user_id, $path = '', $scheme = 'admin' ) {
2264 $user_id = (int) $user_id;
2266 $blogs = get_blogs_of_user( $user_id );
2267 if ( ! is_super_admin() && empty($blogs) ) {
2268 $url = user_admin_url( $path, $scheme );
2269 } elseif ( ! is_multisite() ) {
2270 $url = admin_url( $path, $scheme );
2272 $current_blog = get_current_blog_id();
2273 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
2274 $url = admin_url( $path, $scheme );
2276 $active = get_active_blog_for_user( $user_id );
2278 $url = get_admin_url( $active->blog_id, $path, $scheme );
2280 $url = user_admin_url( $path, $scheme );
2284 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
2288 * Get the URL to the user's profile editor.
2292 * @param int $user User ID
2293 * @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.
2294 * @return string Dashboard url link with optional path appended.
2296 function get_edit_profile_url( $user, $scheme = 'admin' ) {
2297 $user = (int) $user;
2299 if ( is_user_admin() )
2300 $url = user_admin_url( 'profile.php', $scheme );
2301 elseif ( is_network_admin() )
2302 $url = network_admin_url( 'profile.php', $scheme );
2304 $url = get_dashboard_url( $user, 'profile.php', $scheme );
2306 return apply_filters( 'edit_profile_url', $url, $user, $scheme);
2310 * Output rel=canonical for singular queries.
2312 * @package WordPress
2315 function rel_canonical() {
2316 if ( !is_singular() )
2319 global $wp_the_query;
2320 if ( !$id = $wp_the_query->get_queried_object_id() )
2323 $link = get_permalink( $id );
2325 if ( $page = get_query_var('cpage') )
2326 $link = get_comments_pagenum_link( $page );
2328 echo "<link rel='canonical' href='$link' />\n";
2332 * Return a shortlink for a post, page, attachment, or blog.
2334 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
2335 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
2336 * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
2337 * via the get_shortlink filter.
2341 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
2342 * @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'.
2343 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
2344 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
2346 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
2347 // Allow plugins to short-circuit this function.
2348 $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
2349 if ( false !== $shortlink )
2354 if ( 'query' == $context && is_single() ) {
2355 $post_id = $wp_query->get_queried_object_id();
2356 } elseif ( 'post' == $context ) {
2357 $post = get_post($id);
2358 $post_id = $post->ID;
2363 // Return p= link for posts.
2364 if ( !empty($post_id) && '' != get_option('permalink_structure') ) {
2365 $post = get_post($post_id);
2366 if ( isset($post->post_type) && 'post' == $post->post_type )
2367 $shortlink = home_url('?p=' . $post->ID);
2370 return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);
2374 * Inject rel=shortlink into head if a shortlink is defined for the current page.
2376 * Attached to the wp_head action.
2380 * @uses wp_get_shortlink()
2382 function wp_shortlink_wp_head() {
2383 $shortlink = wp_get_shortlink( 0, 'query' );
2385 if ( empty( $shortlink ) )
2388 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
2392 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
2394 * Attached to the wp action.
2398 * @uses wp_get_shortlink()
2400 function wp_shortlink_header() {
2401 if ( headers_sent() )
2404 $shortlink = wp_get_shortlink(0, 'query');
2406 if ( empty($shortlink) )
2409 header('Link: <' . $shortlink . '>; rel=shortlink', false);
2413 * Display the Short Link for a Post
2415 * Must be called from inside "The Loop"
2417 * Call like the_shortlink(__('Shortlinkage FTW'))
2421 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
2422 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
2423 * @param string $before Optional HTML to display before the link.
2424 * @param string $after Optional HTML to display after the link.
2426 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
2429 if ( empty( $text ) )
2430 $text = __('This is the short link.');
2432 if ( empty( $title ) )
2433 $title = the_title_attribute( array( 'echo' => false ) );
2435 $shortlink = wp_get_shortlink( $post->ID );
2437 if ( !empty( $shortlink ) ) {
2438 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
2439 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
2440 echo $before, $link, $after;