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 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);
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 = $cats[0]->slug;
126 if ( $parent = $cats[0]->parent )
127 $category = get_category_parents($parent, false, '/', true) . $category;
129 // show default category in permalinks, without
130 // having to assign it explicitly
131 if ( empty($category) ) {
132 $default_category = get_category( get_option( 'default_category' ) );
133 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
138 if ( strpos($permalink, '%author%') !== false ) {
139 $authordata = get_userdata($post->post_author);
140 $author = $authordata->user_nicename;
143 $date = explode(" ",date('Y m d H i s', $unixtime));
158 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
159 $permalink = user_trailingslashit($permalink, 'single');
160 } else { // if they're not using the fancy permalink option
161 $permalink = home_url('?p=' . $post->ID);
163 return apply_filters('post_link', $permalink, $post, $leavename);
167 * Retrieve the permalink for a post with a custom post type.
171 * @param int $id Optional. Post ID.
172 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
173 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
176 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
179 $post = &get_post($id);
181 if ( is_wp_error( $post ) )
184 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
186 $slug = $post->post_name;
188 $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
190 $post_type = get_post_type_object($post->post_type);
192 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
193 if ( ! $leavename ) {
194 if ( $post_type->hierarchical )
195 $slug = get_page_uri($id);
196 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
198 $post_link = home_url( user_trailingslashit($post_link) );
200 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
201 $post_link = add_query_arg($post_type->query_var, $slug, '');
203 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
204 $post_link = home_url($post_link);
207 return apply_filters('post_type_link', $post_link, $post, $leavename, $sample);
211 * Retrieve permalink from post ID.
215 * @param int $post_id Optional. Post ID.
216 * @param mixed $deprecated Not used.
219 function post_permalink( $post_id = 0, $deprecated = '' ) {
220 if ( !empty( $deprecated ) )
221 _deprecated_argument( __FUNCTION__, '1.3' );
223 return get_permalink($post_id);
227 * Retrieve the permalink for current page or page ID.
229 * Respects page_on_front. Use this one.
233 * @param int $id Optional. Post ID.
234 * @param bool $leavename Optional, defaults to false. Whether to keep page name.
235 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
238 function get_page_link( $id = false, $leavename = false, $sample = false ) {
243 $id = (int) $post->ID;
245 if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
246 $link = home_url('/');
248 $link = _get_page_link( $id , $leavename, $sample );
250 return apply_filters('page_link', $link, $id, $sample);
254 * Retrieve the page permalink.
256 * Ignores page_on_front. Internal use only.
261 * @param int $id Optional. Post ID.
262 * @param bool $leavename Optional. Leave name.
263 * @param bool $sample Optional. Sample permalink.
266 function _get_page_link( $id = false, $leavename = false, $sample = false ) {
267 global $post, $wp_rewrite;
270 $id = (int) $post->ID;
272 $post = &get_post($id);
274 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
276 $link = $wp_rewrite->get_page_permastruct();
278 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
279 if ( ! $leavename ) {
280 $link = str_replace('%pagename%', get_page_uri($id), $link);
283 $link = home_url($link);
284 $link = user_trailingslashit($link, 'page');
286 $link = home_url("?page_id=$id");
289 return apply_filters( '_get_page_link', $link, $id );
293 * Retrieve permalink for attachment.
295 * This can be used in the WordPress Loop or outside of it.
299 * @param int $id Optional. Post ID.
302 function get_attachment_link($id = false) {
303 global $post, $wp_rewrite;
308 $id = (int) $post->ID;
310 $object = get_post($id);
311 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
312 $parent = get_post($object->post_parent);
313 if ( 'page' == $parent->post_type )
314 $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
316 $parentlink = get_permalink( $object->post_parent );
318 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
319 $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
321 $name = $object->post_name;
323 if ( strpos($parentlink, '?') === false )
324 $link = user_trailingslashit( trailingslashit($parentlink) . $name );
328 $link = home_url( "/?attachment_id=$id" );
330 return apply_filters('attachment_link', $link, $id);
334 * Retrieve the permalink for the year archives.
338 * @param int|bool $year False for current year or year for permalink.
341 function get_year_link($year) {
344 $year = gmdate('Y', current_time('timestamp'));
345 $yearlink = $wp_rewrite->get_year_permastruct();
346 if ( !empty($yearlink) ) {
347 $yearlink = str_replace('%year%', $year, $yearlink);
348 return apply_filters('year_link', home_url( user_trailingslashit($yearlink, 'year') ), $year);
350 return apply_filters('year_link', home_url('?m=' . $year), $year);
355 * Retrieve the permalink for the month archives with year.
359 * @param bool|int $year False for current year. Integer of year.
360 * @param bool|int $month False for current month. Integer of month.
363 function get_month_link($year, $month) {
366 $year = gmdate('Y', current_time('timestamp'));
368 $month = gmdate('m', current_time('timestamp'));
369 $monthlink = $wp_rewrite->get_month_permastruct();
370 if ( !empty($monthlink) ) {
371 $monthlink = str_replace('%year%', $year, $monthlink);
372 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
373 return apply_filters('month_link', home_url( user_trailingslashit($monthlink, 'month') ), $year, $month);
375 return apply_filters('month_link', home_url( '?m=' . $year . zeroise($month, 2) ), $year, $month);
380 * Retrieve the permalink for the day archives with year and month.
384 * @param bool|int $year False for current year. Integer of year.
385 * @param bool|int $month False for current month. Integer of month.
386 * @param bool|int $day False for current day. Integer of day.
389 function get_day_link($year, $month, $day) {
392 $year = gmdate('Y', current_time('timestamp'));
394 $month = gmdate('m', current_time('timestamp'));
396 $day = gmdate('j', current_time('timestamp'));
398 $daylink = $wp_rewrite->get_day_permastruct();
399 if ( !empty($daylink) ) {
400 $daylink = str_replace('%year%', $year, $daylink);
401 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
402 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
403 return apply_filters('day_link', home_url( user_trailingslashit($daylink, 'day') ), $year, $month, $day);
405 return apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2) . zeroise($day, 2) ), $year, $month, $day);
410 * Display the permalink for the feed type.
414 * @param string $anchor The link's anchor text.
415 * @param string $feed Optional, defaults to default feed. Feed type.
417 function the_feed_link( $anchor, $feed = '' ) {
418 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
419 echo apply_filters( 'the_feed_link', $link, $feed );
423 * Retrieve the permalink for the feed type.
427 * @param string $feed Optional, defaults to default feed. Feed type.
430 function get_feed_link($feed = '') {
433 $permalink = $wp_rewrite->get_feed_permastruct();
434 if ( '' != $permalink ) {
435 if ( false !== strpos($feed, 'comments_') ) {
436 $feed = str_replace('comments_', '', $feed);
437 $permalink = $wp_rewrite->get_comment_feed_permastruct();
440 if ( get_default_feed() == $feed )
443 $permalink = str_replace('%feed%', $feed, $permalink);
444 $permalink = preg_replace('#/+#', '/', "/$permalink");
445 $output = home_url( user_trailingslashit($permalink, 'feed') );
448 $feed = get_default_feed();
450 if ( false !== strpos($feed, 'comments_') )
451 $feed = str_replace('comments_', 'comments-', $feed);
453 $output = home_url("?feed={$feed}");
456 return apply_filters('feed_link', $output, $feed);
460 * Retrieve the permalink for the post comments feed.
464 * @param int $post_id Optional. Post ID.
465 * @param string $feed Optional. Feed type.
468 function get_post_comments_feed_link($post_id = 0, $feed = '') {
469 $post_id = absint( $post_id );
472 $post_id = get_the_ID();
474 if ( empty( $feed ) )
475 $feed = get_default_feed();
477 if ( '' != get_option('permalink_structure') ) {
478 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
479 $url = _get_page_link( $post_id );
481 $url = get_permalink($post_id);
483 $url = trailingslashit($url) . 'feed';
484 if ( $feed != get_default_feed() )
486 $url = user_trailingslashit($url, 'single_feed');
488 $type = get_post_field('post_type', $post_id);
489 if ( 'page' == $type )
490 $url = home_url("?feed=$feed&page_id=$post_id");
492 $url = home_url("?feed=$feed&p=$post_id");
495 return apply_filters('post_comments_feed_link', $url);
499 * Display the comment feed link for a post.
501 * Prints out the comment feed link for a post. Link text is placed in the
502 * anchor. If no link text is specified, default text is used. If no post ID is
503 * specified, the current post is used.
509 * @param string $link_text Descriptive text.
510 * @param int $post_id Optional post ID. Default to current post.
511 * @param string $feed Optional. Feed format.
512 * @return string Link to the comment feed for the current post.
514 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
515 $url = get_post_comments_feed_link($post_id, $feed);
516 if ( empty($link_text) )
517 $link_text = __('Comments Feed');
519 echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
523 * Retrieve the feed link for a given author.
525 * Returns a link to the feed for all posts by a given author. A specific feed
526 * can be requested or left blank to get the default feed.
532 * @param int $author_id ID of an author.
533 * @param string $feed Optional. Feed type.
534 * @return string Link to the feed for the author specified by $author_id.
536 function get_author_feed_link( $author_id, $feed = '' ) {
537 $author_id = (int) $author_id;
538 $permalink_structure = get_option('permalink_structure');
541 $feed = get_default_feed();
543 if ( '' == $permalink_structure ) {
544 $link = home_url("?feed=$feed&author=" . $author_id);
546 $link = get_author_posts_url($author_id);
547 if ( $feed == get_default_feed() )
550 $feed_link = "feed/$feed";
552 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
555 $link = apply_filters('author_feed_link', $link, $feed);
561 * Retrieve the feed link for a category.
563 * Returns a link to the feed for all post in a given category. A specific feed
564 * can be requested or left blank to get the default feed.
570 * @param int $cat_id ID of a category.
571 * @param string $feed Optional. Feed type.
572 * @return string Link to the feed for the category specified by $cat_id.
574 function get_category_feed_link($cat_id, $feed = '') {
575 return get_term_feed_link($cat_id, 'category', $feed);
579 * Retrieve the feed link for a taxonomy.
581 * Returns a link to the feed for all post in a given term. A specific feed
582 * can be requested or left blank to get the default feed.
586 * @param int $term_id ID of a category.
587 * @param string $taxonomy Optional. Taxonomy of $term_id
588 * @param string $feed Optional. Feed type.
589 * @return string Link to the feed for the taxonomy specified by $term_id and $taxonomy.
591 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
594 $term_id = ( int ) $term_id;
596 $term = get_term( $term_id, $taxonomy );
598 if ( empty( $term ) || is_wp_error( $term ) )
601 if ( empty( $feed ) )
602 $feed = get_default_feed();
604 $permalink_structure = get_option( 'permalink_structure' );
606 if ( '' == $permalink_structure ) {
607 if ( 'category' == $taxonomy ) {
608 $link = home_url("?feed=$feed&cat=$term_id");
610 elseif ( 'post_tag' == $taxonomy ) {
611 $link = home_url("?feed=$feed&tag=$term->slug");
613 $t = get_taxonomy( $taxonomy );
614 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
617 $link = get_term_link( $term_id, $term->taxonomy );
618 if ( $feed == get_default_feed() )
621 $feed_link = "feed/$feed";
623 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
626 if ( 'category' == $taxonomy )
627 $link = apply_filters( 'category_feed_link', $link, $feed );
628 elseif ( 'post_tag' == $taxonomy )
629 $link = apply_filters( 'category_feed_link', $link, $feed );
631 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
638 * Retrieve permalink for feed of tag.
642 * @param int $tag_id Tag ID.
643 * @param string $feed Optional. Feed type.
646 function get_tag_feed_link($tag_id, $feed = '') {
647 return get_term_feed_link($tag_id, 'post_tag', $feed);
651 * Retrieve edit tag link.
655 * @param int $tag_id Tag ID
656 * @param string $taxonomy Taxonomy
659 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
660 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
664 * Display or retrieve edit tag link with formatting.
668 * @param string $link Optional. Anchor text.
669 * @param string $before Optional. Display before edit link.
670 * @param string $after Optional. Display after edit link.
671 * @param int|object $tag Tag object or ID
672 * @return string HTML content.
674 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
675 $link = edit_term_link( $link, '', '', false, $tag );
676 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
680 * Retrieve edit term url.
684 * @param int $term_id Term ID
685 * @param string $taxonomy Taxonomy
686 * @param string $object_type The object type
689 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
690 $tax = get_taxonomy( $taxonomy );
691 if ( !current_user_can( $tax->cap->edit_terms ) )
694 $term = get_term( $term_id, $taxonomy );
698 'taxonomy' => $taxonomy,
699 'tag_ID' => $term->term_id,
703 $args['post_type'] = $object_type;
705 $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
707 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
711 * Display or retrieve edit term link with formatting.
715 * @param string $link Optional. Anchor text.
716 * @param string $before Optional. Display before edit link.
717 * @param string $after Optional. Display after edit link.
718 * @param object $term Term object
719 * @return string HTML content.
721 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
722 if ( is_null( $term ) ) {
723 $term = get_queried_object();
726 $tax = get_taxonomy( $term->taxonomy );
727 if ( !current_user_can($tax->cap->edit_terms) )
730 if ( empty( $link ) )
731 $link = __('Edit This');
733 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '" title="' . $link . '">' . $link . '</a>';
734 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
743 * Retrieve permalink for search.
746 * @param string $query Optional. The query string to use. If empty the current query is used.
749 function get_search_link( $query = '' ) {
753 $search = get_search_query( false );
755 $search = stripslashes($query);
757 $permastruct = $wp_rewrite->get_search_permastruct();
759 if ( empty( $permastruct ) ) {
760 $link = home_url('?s=' . urlencode($search) );
762 $search = urlencode($search);
763 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
764 $link = str_replace( '%search%', $search, $permastruct );
765 $link = home_url( user_trailingslashit( $link, 'search' ) );
768 return apply_filters( 'search_link', $link, $search );
772 * Retrieve the permalink for the feed of the search results.
776 * @param string $search_query Optional. Search query.
777 * @param string $feed Optional. Feed type.
780 function get_search_feed_link($search_query = '', $feed = '') {
782 $link = get_search_link($search_query);
785 $feed = get_default_feed();
787 $permastruct = $wp_rewrite->get_search_permastruct();
789 if ( empty($permastruct) ) {
790 $link = add_query_arg('feed', $feed, $link);
792 $link = trailingslashit($link);
793 $link .= "feed/$feed/";
796 $link = apply_filters('search_feed_link', $link, $feed, 'posts');
802 * Retrieve the permalink for the comments feed of the search results.
806 * @param string $search_query Optional. Search query.
807 * @param string $feed Optional. Feed type.
810 function get_search_comments_feed_link($search_query = '', $feed = '') {
814 $feed = get_default_feed();
816 $link = get_search_feed_link($search_query, $feed);
818 $permastruct = $wp_rewrite->get_search_permastruct();
820 if ( empty($permastruct) )
821 $link = add_query_arg('feed', 'comments-' . $feed, $link);
823 $link = add_query_arg('withcomments', 1, $link);
825 $link = apply_filters('search_feed_link', $link, $feed, 'comments');
831 * Retrieve the permalink for a post type archive.
835 * @param string $post_type Post type
838 function get_post_type_archive_link( $post_type ) {
840 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
843 if ( ! $post_type_obj->has_archive )
846 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
847 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
848 if ( $post_type_obj->rewrite['with_front'] )
849 $struct = $wp_rewrite->front . $struct;
851 $struct = $wp_rewrite->root . $struct;
852 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
854 $link = home_url( '?post_type=' . $post_type );
857 return apply_filters( 'post_type_archive_link', $link, $post_type );
861 * Retrieve the permalink for a post type archive feed.
865 * @param string $post_type Post type
866 * @param string $feed Optional. Feed type
869 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
870 $default_feed = get_default_feed();
871 if ( empty( $feed ) )
872 $feed = $default_feed;
874 if ( ! $link = get_post_type_archive_link( $post_type ) )
876 $post_type_obj = get_post_type_object( $post_type );
877 if ( $post_type_obj->rewrite['feeds'] && get_option( 'permalink_structure' ) ) {
878 $link = trailingslashit($link);
880 if ( $feed != $default_feed )
883 $link = add_query_arg( 'feed', $feed, $link );
886 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
890 * Retrieve edit posts link for post.
892 * Can be used within the WordPress loop or outside of it. Can be used with
893 * pages, posts, attachments, and revisions.
897 * @param int $id Optional. Post ID.
898 * @param string $context Optional, default to display. How to write the '&', defaults to '&'.
901 function get_edit_post_link( $id = 0, $context = 'display' ) {
902 if ( !$post = &get_post( $id ) )
905 if ( 'display' == $context )
906 $action = '&action=edit';
908 $action = '&action=edit';
910 $post_type_object = get_post_type_object( $post->post_type );
911 if ( !$post_type_object )
914 if ( !current_user_can( $post_type_object->cap->edit_post, $post->ID ) )
917 return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
921 * Display edit post link for post.
925 * @param string $link Optional. Anchor text.
926 * @param string $before Optional. Display before edit link.
927 * @param string $after Optional. Display after edit link.
928 * @param int $id Optional. Post ID.
930 function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
931 if ( !$post = &get_post( $id ) )
934 if ( !$url = get_edit_post_link( $post->ID ) )
937 if ( null === $link )
938 $link = __('Edit This');
940 $post_type_obj = get_post_type_object( $post->post_type );
941 $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( $post_type_obj->labels->edit_item ) . '">' . $link . '</a>';
942 echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
946 * Retrieve delete posts link for post.
948 * Can be used within the WordPress loop or outside of it, with any post type.
952 * @param int $id Optional. Post ID.
953 * @param string $deprecated Not used.
954 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
957 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
958 if ( ! empty( $deprecated ) )
959 _deprecated_argument( __FUNCTION__, '3.0' );
961 if ( !$post = &get_post( $id ) )
964 $post_type_object = get_post_type_object( $post->post_type );
965 if ( !$post_type_object )
968 if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
971 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
973 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
975 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-{$post->post_type}_{$post->ID}" ), $post->ID, $force_delete );
979 * Retrieve edit comment link.
983 * @param int $comment_id Optional. Comment ID.
986 function get_edit_comment_link( $comment_id = 0 ) {
987 $comment = &get_comment( $comment_id );
989 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
992 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
993 return apply_filters( 'get_edit_comment_link', $location );
997 * Display or retrieve edit comment link with formatting.
1001 * @param string $link Optional. Anchor text.
1002 * @param string $before Optional. Display before edit link.
1003 * @param string $after Optional. Display after edit link.
1004 * @return string|null HTML content, if $echo is set to false.
1006 function edit_comment_link( $link = null, $before = '', $after = '' ) {
1009 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1012 if ( null === $link )
1013 $link = __('Edit This');
1015 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . esc_attr__( 'Edit comment' ) . '">' . $link . '</a>';
1016 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
1020 * Display edit bookmark (literally a URL external to blog) link.
1024 * @param int $link Optional. Bookmark ID.
1027 function get_edit_bookmark_link( $link = 0 ) {
1028 $link = get_bookmark( $link );
1030 if ( !current_user_can('manage_links') )
1033 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1034 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1038 * Display edit bookmark (literally a URL external to blog) link anchor content.
1042 * @param string $link Optional. Anchor text.
1043 * @param string $before Optional. Display before edit link.
1044 * @param string $after Optional. Display after edit link.
1045 * @param int $bookmark Optional. Bookmark ID.
1047 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1048 $bookmark = get_bookmark($bookmark);
1050 if ( !current_user_can('manage_links') )
1054 $link = __('Edit This');
1056 $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '" title="' . esc_attr__( 'Edit Link' ) . '">' . $link . '</a>';
1057 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1063 * Retrieve previous post that is adjacent to current post.
1067 * @param bool $in_same_cat Optional. Whether post should be in same category.
1068 * @param string $excluded_categories Optional. Excluded categories IDs.
1069 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1071 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
1072 return get_adjacent_post($in_same_cat, $excluded_categories);
1076 * Retrieve next post that is adjacent to current post.
1080 * @param bool $in_same_cat Optional. Whether post should be in same category.
1081 * @param string $excluded_categories Optional. Excluded categories IDs.
1082 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1084 function get_next_post($in_same_cat = false, $excluded_categories = '') {
1085 return get_adjacent_post($in_same_cat, $excluded_categories, false);
1089 * Retrieve adjacent post.
1091 * Can either be next or previous post.
1095 * @param bool $in_same_cat Optional. Whether post should be in same category.
1096 * @param string $excluded_categories Optional. Excluded categories IDs.
1097 * @param bool $previous Optional. Whether to retrieve previous post.
1098 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1100 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
1101 global $post, $wpdb;
1103 if ( empty( $post ) )
1106 $current_post_date = $post->post_date;
1109 $posts_in_ex_cats_sql = '';
1110 if ( $in_same_cat || !empty($excluded_categories) ) {
1111 $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";
1113 if ( $in_same_cat ) {
1114 $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
1115 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
1118 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
1119 if ( !empty($excluded_categories) ) {
1120 $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
1121 if ( !empty($cat_array) ) {
1122 $excluded_categories = array_diff($excluded_categories, $cat_array);
1123 $posts_in_ex_cats_sql = '';
1126 if ( !empty($excluded_categories) ) {
1127 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
1132 $adjacent = $previous ? 'previous' : 'next';
1133 $op = $previous ? '<' : '>';
1134 $order = $previous ? 'DESC' : 'ASC';
1136 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
1137 $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 );
1138 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1140 $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
1141 $query_key = 'adjacent_post_' . md5($query);
1142 $result = wp_cache_get($query_key, 'counts');
1143 if ( false !== $result )
1146 $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
1147 if ( null === $result )
1150 wp_cache_set($query_key, $result, 'counts');
1155 * Get adjacent post relational link.
1157 * Can either be next or previous post relational link.
1161 * @param string $title Optional. Link title format.
1162 * @param bool $in_same_cat Optional. Whether link should be in same category.
1163 * @param string $excluded_categories Optional. Excluded categories IDs.
1164 * @param bool $previous Optional, default is true. Whether display link to previous post.
1167 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
1168 if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) )
1169 $post = & get_post($GLOBALS['post']->post_parent);
1171 $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
1176 if ( empty($post->post_title) )
1177 $post->post_title = $previous ? __('Previous Post') : __('Next Post');
1179 $date = mysql2date(get_option('date_format'), $post->post_date);
1181 $title = str_replace('%title', $post->post_title, $title);
1182 $title = str_replace('%date', $date, $title);
1183 $title = apply_filters('the_title', $title, $post->ID);
1185 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1186 $link .= esc_attr( $title );
1187 $link .= "' href='" . get_permalink($post) . "' />\n";
1189 $adjacent = $previous ? 'previous' : 'next';
1190 return apply_filters( "{$adjacent}_post_rel_link", $link );
1194 * Display relational links for the posts adjacent to the current post.
1198 * @param string $title Optional. Link title format.
1199 * @param bool $in_same_cat Optional. Whether link should be in same category.
1200 * @param string $excluded_categories Optional. Excluded categories IDs.
1202 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1203 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
1204 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
1208 * Display relational links for the posts adjacent to the current post for single post pages.
1210 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1214 function adjacent_posts_rel_link_wp_head() {
1215 if ( !is_singular() || is_attachment() )
1217 adjacent_posts_rel_link();
1221 * Display relational link for the next post adjacent to the current post.
1225 * @param string $title Optional. Link title format.
1226 * @param bool $in_same_cat Optional. Whether link should be in same category.
1227 * @param string $excluded_categories Optional. Excluded categories IDs.
1229 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1230 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
1234 * Display relational link for the previous post adjacent to the current post.
1238 * @param string $title Optional. Link title format.
1239 * @param bool $in_same_cat Optional. Whether link should be in same category.
1240 * @param string $excluded_categories Optional. Excluded categories IDs.
1242 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1243 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
1247 * Retrieve boundary post.
1249 * Boundary being either the first or last post by publish date within the constraints specified
1250 * by in same category or excluded categories.
1254 * @param bool $in_same_cat Optional. Whether returned post should be in same category.
1255 * @param string $excluded_categories Optional. Excluded categories IDs.
1256 * @param bool $start Optional. Whether to retrieve first or last post.
1259 function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
1262 if ( empty($post) || !is_single() || is_attachment() )
1265 $cat_array = array();
1266 $excluded_categories = array();
1267 if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
1268 if ( !empty($in_same_cat) ) {
1269 $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
1272 if ( !empty($excluded_categories) ) {
1273 $excluded_categories = array_map('intval', explode(',', $excluded_categories));
1275 if ( !empty($cat_array) )
1276 $excluded_categories = array_diff($excluded_categories, $cat_array);
1278 $inverse_cats = array();
1279 foreach ( $excluded_categories as $excluded_category)
1280 $inverse_cats[] = $excluded_category * -1;
1281 $excluded_categories = $inverse_cats;
1285 $categories = implode(',', array_merge($cat_array, $excluded_categories) );
1287 $order = $start ? 'ASC' : 'DESC';
1289 return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
1293 * Get boundary post relational link.
1295 * Can either be start or end post relational link.
1299 * @param string $title Optional. Link title format.
1300 * @param bool $in_same_cat Optional. Whether link should be in same category.
1301 * @param string $excluded_categories Optional. Excluded categories IDs.
1302 * @param bool $start Optional, default is true. Whether display link to first or last post.
1305 function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
1306 $posts = get_boundary_post($in_same_cat, $excluded_categories, $start);
1307 // If there is no post stop.
1308 if ( empty($posts) )
1311 // Even though we limited get_posts to return only 1 item it still returns an array of objects.
1314 if ( empty($post->post_title) )
1315 $post->post_title = $start ? __('First Post') : __('Last Post');
1317 $date = mysql2date(get_option('date_format'), $post->post_date);
1319 $title = str_replace('%title', $post->post_title, $title);
1320 $title = str_replace('%date', $date, $title);
1321 $title = apply_filters('the_title', $title, $post->ID);
1323 $link = $start ? "<link rel='start' title='" : "<link rel='end' title='";
1324 $link .= esc_attr($title);
1325 $link .= "' href='" . get_permalink($post) . "' />\n";
1327 $boundary = $start ? 'start' : 'end';
1328 return apply_filters( "{$boundary}_post_rel_link", $link );
1332 * Display relational link for the first post.
1336 * @param string $title Optional. Link title format.
1337 * @param bool $in_same_cat Optional. Whether link should be in same category.
1338 * @param string $excluded_categories Optional. Excluded categories IDs.
1340 function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1341 echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
1345 * Get site index relational link.
1351 function get_index_rel_link() {
1352 $link = "<link rel='index' title='" . esc_attr( get_bloginfo( 'name', 'display' ) ) . "' href='" . esc_url( user_trailingslashit( get_bloginfo( 'url', 'display' ) ) ) . "' />\n";
1353 return apply_filters( "index_rel_link", $link );
1357 * Display relational link for the site index.
1361 function index_rel_link() {
1362 echo get_index_rel_link();
1366 * Get parent post relational link.
1370 * @param string $title Optional. Link title format.
1373 function get_parent_post_rel_link($title = '%title') {
1374 if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
1375 $post = & get_post($GLOBALS['post']->post_parent);
1380 $date = mysql2date(get_option('date_format'), $post->post_date);
1382 $title = str_replace('%title', $post->post_title, $title);
1383 $title = str_replace('%date', $date, $title);
1384 $title = apply_filters('the_title', $title, $post->ID);
1386 $link = "<link rel='up' title='";
1387 $link .= esc_attr( $title );
1388 $link .= "' href='" . get_permalink($post) . "' />\n";
1390 return apply_filters( "parent_post_rel_link", $link );
1394 * Display relational link for parent item
1398 function parent_post_rel_link($title = '%title') {
1399 echo get_parent_post_rel_link($title);
1403 * Display previous post link that is adjacent to the current post.
1407 * @param string $format Optional. Link anchor format.
1408 * @param string $link Optional. Link permalink format.
1409 * @param bool $in_same_cat Optional. Whether link should be in same category.
1410 * @param string $excluded_categories Optional. Excluded categories IDs.
1412 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1413 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
1417 * Display next post link that is adjacent to the current post.
1421 * @param string $format Optional. Link anchor format.
1422 * @param string $link Optional. Link permalink format.
1423 * @param bool $in_same_cat Optional. Whether link should be in same category.
1424 * @param string $excluded_categories Optional. Excluded categories IDs.
1426 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1427 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
1431 * Display adjacent post link.
1433 * Can be either next post link or previous.
1437 * @param string $format Link anchor format.
1438 * @param string $link Link permalink format.
1439 * @param bool $in_same_cat Optional. Whether link should be in same category.
1440 * @param string $excluded_categories Optional. Excluded categories IDs.
1441 * @param bool $previous Optional, default is true. Whether display link to previous post.
1443 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
1444 if ( $previous && is_attachment() )
1445 $post = & get_post($GLOBALS['post']->post_parent);
1447 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
1452 $title = $post->post_title;
1454 if ( empty($post->post_title) )
1455 $title = $previous ? __('Previous Post') : __('Next Post');
1457 $title = apply_filters('the_title', $title, $post->ID);
1458 $date = mysql2date(get_option('date_format'), $post->post_date);
1459 $rel = $previous ? 'prev' : 'next';
1461 $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
1462 $link = str_replace('%title', $title, $link);
1463 $link = str_replace('%date', $date, $link);
1464 $link = $string . $link . '</a>';
1466 $format = str_replace('%link', $link, $format);
1468 $adjacent = $previous ? 'previous' : 'next';
1469 echo apply_filters( "{$adjacent}_post_link", $format, $link );
1473 * Retrieve get links for page numbers.
1477 * @param int $pagenum Optional. Page ID.
1480 function get_pagenum_link($pagenum = 1) {
1483 $pagenum = (int) $pagenum;
1485 $request = remove_query_arg( 'paged' );
1487 $home_root = parse_url(home_url());
1488 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1489 $home_root = preg_quote( trailingslashit( $home_root ), '|' );
1491 $request = preg_replace('|^'. $home_root . '|', '', $request);
1492 $request = preg_replace('|^/+|', '', $request);
1494 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1495 $base = trailingslashit( get_bloginfo( 'url' ) );
1497 if ( $pagenum > 1 ) {
1498 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1500 $result = $base . $request;
1503 $qs_regex = '|\?.*?$|';
1504 preg_match( $qs_regex, $request, $qs_match );
1506 if ( !empty( $qs_match[0] ) ) {
1507 $query_string = $qs_match[0];
1508 $request = preg_replace( $qs_regex, '', $request );
1513 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
1514 $request = preg_replace( '|^index\.php|', '', $request);
1515 $request = ltrim($request, '/');
1517 $base = trailingslashit( get_bloginfo( 'url' ) );
1519 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1520 $base .= 'index.php/';
1522 if ( $pagenum > 1 ) {
1523 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
1526 $result = $base . $request . $query_string;
1529 $result = apply_filters('get_pagenum_link', $result);
1535 * Retrieve next posts pages link.
1537 * Backported from 2.1.3 to 2.0.10.
1541 * @param int $max_page Optional. Max pages.
1544 function get_next_posts_page_link($max_page = 0) {
1547 if ( !is_single() ) {
1550 $nextpage = intval($paged) + 1;
1551 if ( !$max_page || $max_page >= $nextpage )
1552 return get_pagenum_link($nextpage);
1557 * Display or return the next posts pages link.
1561 * @param int $max_page Optional. Max pages.
1562 * @param boolean $echo Optional. Echo or return;
1564 function next_posts( $max_page = 0, $echo = true ) {
1565 $output = esc_url( get_next_posts_page_link( $max_page ) );
1574 * Return the next posts pages link.
1578 * @param string $label Content for link text.
1579 * @param int $max_page Optional. Max pages.
1580 * @return string|null
1582 function get_next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
1583 global $paged, $wp_query;
1586 $max_page = $wp_query->max_num_pages;
1591 $nextpage = intval($paged) + 1;
1593 if ( !is_single() && ( $nextpage <= $max_page ) ) {
1594 $attr = apply_filters( 'next_posts_link_attributes', '' );
1595 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
1600 * Display the next posts pages link.
1603 * @uses get_next_posts_link()
1605 * @param string $label Content for link text.
1606 * @param int $max_page Optional. Max pages.
1608 function next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
1609 echo get_next_posts_link( $label, $max_page );
1613 * Retrieve previous post pages link.
1615 * Will only return string, if not on a single page or post.
1617 * Backported to 2.0.10 from 2.1.3.
1621 * @return string|null
1623 function get_previous_posts_page_link() {
1626 if ( !is_single() ) {
1627 $nextpage = intval($paged) - 1;
1628 if ( $nextpage < 1 )
1630 return get_pagenum_link($nextpage);
1635 * Display or return the previous posts pages link.
1639 * @param boolean $echo Optional. Echo or return;
1641 function previous_posts( $echo = true ) {
1642 $output = esc_url( get_previous_posts_page_link() );
1651 * Return the previous posts pages link.
1655 * @param string $label Optional. Previous page link text.
1656 * @return string|null
1658 function get_previous_posts_link( $label = '« Previous Page' ) {
1661 if ( !is_single() && $paged > 1 ) {
1662 $attr = apply_filters( 'previous_posts_link_attributes', '' );
1663 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&$1', $label ) .'</a>';
1668 * Display the previous posts page link.
1671 * @uses get_previous_posts_link()
1673 * @param string $label Optional. Previous page link text.
1675 function previous_posts_link( $label = '« Previous Page' ) {
1676 echo get_previous_posts_link( $label );
1680 * Return post pages link navigation for previous and next pages.
1684 * @param string|array $args Optional args.
1685 * @return string The posts link navigation.
1687 function get_posts_nav_link( $args = array() ) {
1692 if ( !is_singular() ) {
1694 'sep' => ' — ',
1695 'prelabel' => __('« Previous Page'),
1696 'nxtlabel' => __('Next Page »'),
1698 $args = wp_parse_args( $args, $defaults );
1700 $max_num_pages = $wp_query->max_num_pages;
1701 $paged = get_query_var('paged');
1703 //only have sep if there's both prev and next results
1704 if ($paged < 2 || $paged >= $max_num_pages) {
1708 if ( $max_num_pages > 1 ) {
1709 $return = get_previous_posts_link($args['prelabel']);
1710 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
1711 $return .= get_next_posts_link($args['nxtlabel']);
1719 * Display post pages link navigation for previous and next pages.
1723 * @param string $sep Optional. Separator for posts navigation links.
1724 * @param string $prelabel Optional. Label for previous pages.
1725 * @param string $nxtlabel Optional Label for next pages.
1727 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
1728 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
1729 echo get_posts_nav_link($args);
1733 * Retrieve page numbers links.
1737 * @param int $pagenum Optional. Page number.
1740 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
1741 global $post, $wp_rewrite;
1743 $pagenum = (int) $pagenum;
1745 $result = get_permalink( $post->ID );
1747 if ( 'newest' == get_option('default_comments_page') ) {
1748 if ( $pagenum != $max_page ) {
1749 if ( $wp_rewrite->using_permalinks() )
1750 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1752 $result = add_query_arg( 'cpage', $pagenum, $result );
1754 } elseif ( $pagenum > 1 ) {
1755 if ( $wp_rewrite->using_permalinks() )
1756 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1758 $result = add_query_arg( 'cpage', $pagenum, $result );
1761 $result .= '#comments';
1763 $result = apply_filters('get_comments_pagenum_link', $result);
1769 * Return the link to next comments pages.
1773 * @param string $label Optional. Label for link text.
1774 * @param int $max_page Optional. Max page.
1775 * @return string|null
1777 function get_next_comments_link( $label = '', $max_page = 0 ) {
1780 if ( !is_singular() || !get_option('page_comments') )
1783 $page = get_query_var('cpage');
1785 $nextpage = intval($page) + 1;
1787 if ( empty($max_page) )
1788 $max_page = $wp_query->max_num_comment_pages;
1790 if ( empty($max_page) )
1791 $max_page = get_comment_pages_count();
1793 if ( $nextpage > $max_page )
1796 if ( empty($label) )
1797 $label = __('Newer Comments »');
1799 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>';
1803 * Display the link to next comments pages.
1807 * @param string $label Optional. Label for link text.
1808 * @param int $max_page Optional. Max page.
1810 function next_comments_link( $label = '', $max_page = 0 ) {
1811 echo get_next_comments_link( $label, $max_page );
1815 * Return the previous comments page link.
1819 * @param string $label Optional. Label for comments link text.
1820 * @return string|null
1822 function get_previous_comments_link( $label = '' ) {
1823 if ( !is_singular() || !get_option('page_comments') )
1826 $page = get_query_var('cpage');
1828 if ( intval($page) <= 1 )
1831 $prevpage = intval($page) - 1;
1833 if ( empty($label) )
1834 $label = __('« Older Comments');
1836 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>';
1840 * Display the previous comments page link.
1844 * @param string $label Optional. Label for comments link text.
1846 function previous_comments_link( $label = '' ) {
1847 echo get_previous_comments_link( $label );
1851 * Create pagination links for the comments on the current post.
1853 * @see paginate_links()
1856 * @param string|array $args Optional args. See paginate_links.
1857 * @return string Markup for pagination links.
1859 function paginate_comments_links($args = array()) {
1862 if ( !is_singular() || !get_option('page_comments') )
1865 $page = get_query_var('cpage');
1868 $max_page = get_comment_pages_count();
1870 'base' => add_query_arg( 'cpage', '%#%' ),
1872 'total' => $max_page,
1875 'add_fragment' => '#comments'
1877 if ( $wp_rewrite->using_permalinks() )
1878 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
1880 $args = wp_parse_args( $args, $defaults );
1881 $page_links = paginate_links( $args );
1883 if ( $args['echo'] )
1890 * Retrieve shortcut link.
1892 * Use this in 'a' element 'href' attribute.
1898 function get_shortcut_link() {
1899 $link = "javascript:
1905 s=(e?e():(k)?k():(x?x.createRange().text:0)),
1906 f='" . admin_url('press-this.php') . "',
1908 e=encodeURIComponent,
1909 u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
1910 a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
1911 if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
1914 $link = str_replace(array("\r", "\n", "\t"), '', $link);
1916 return apply_filters('shortcut_link', $link);
1920 * Retrieve the home url for the current site.
1922 * Returns the 'home' option with the appropriate protocol, 'https' if
1923 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1926 * @package WordPress
1929 * @uses get_home_url()
1931 * @param string $path (optional) Path relative to the home url.
1932 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http','https'
1933 * @return string Home url link with optional path appended.
1935 function home_url( $path = '', $scheme = null ) {
1936 return get_home_url(null, $path, $scheme);
1940 * Retrieve the home url for a given site.
1942 * Returns the 'home' option with the appropriate protocol, 'https' if
1943 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1946 * @package WordPress
1949 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
1950 * @param string $path (optional) Path relative to the home url.
1951 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http','https'
1952 * @return string Home url link with optional path appended.
1954 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
1955 $orig_scheme = $scheme;
1957 if ( !in_array( $scheme, array( 'http', 'https' ) ) )
1958 $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
1960 if ( empty( $blog_id ) || !is_multisite() )
1961 $url = get_option( 'home' );
1963 $url = get_blog_option( $blog_id, 'home' );
1965 if ( 'http' != $scheme )
1966 $url = str_replace( 'http://', "$scheme://", $url );
1968 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
1969 $url .= '/' . ltrim( $path, '/' );
1971 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
1975 * Retrieve the site url for the current site.
1977 * Returns the 'site_url' option with the appropriate protocol, 'https' if
1978 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1981 * @package WordPress
1984 * @uses get_site_url()
1986 * @param string $path Optional. Path relative to the site url.
1987 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
1988 * @return string Site url link with optional path appended.
1990 function site_url( $path = '', $scheme = null ) {
1991 return get_site_url(null, $path, $scheme);
1995 * Retrieve the site url for a given site.
1997 * Returns the 'site_url' option with the appropriate protocol, 'https' if
1998 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2001 * @package WordPress
2004 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2005 * @param string $path Optional. Path relative to the site url.
2006 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
2007 * @return string Site url link with optional path appended.
2009 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2010 // should the list of allowed schemes be maintained elsewhere?
2011 $orig_scheme = $scheme;
2012 if ( !in_array( $scheme, array( 'http', 'https' ) ) ) {
2013 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
2015 elseif ( ( 'login' == $scheme ) && force_ssl_admin() )
2017 elseif ( ( 'admin' == $scheme ) && force_ssl_admin() )
2020 $scheme = ( is_ssl() ? 'https' : 'http' );
2023 if ( empty( $blog_id ) || !is_multisite() )
2024 $url = get_option( 'siteurl' );
2026 $url = get_blog_option( $blog_id, 'siteurl' );
2028 if ( 'http' != $scheme )
2029 $url = str_replace( 'http://', "{$scheme}://", $url );
2031 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
2032 $url .= '/' . ltrim( $path, '/' );
2034 return apply_filters( 'site_url', $url, $path, $orig_scheme, $blog_id );
2038 * Retrieve the url to the admin area for the current site.
2040 * @package WordPress
2043 * @param string $path Optional path relative to the admin url
2044 * @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.
2045 * @return string Admin url link with optional path appended
2047 function admin_url( $path = '', $scheme = 'admin' ) {
2048 return get_admin_url(null, $path, $scheme);
2052 * Retrieve the url to the admin area for a given site.
2054 * @package WordPress
2057 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2058 * @param string $path Optional path relative to the admin url
2059 * @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.
2060 * @return string Admin url link with optional path appended
2062 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2063 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2065 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2066 $url .= ltrim($path, '/');
2068 return apply_filters('admin_url', $url, $path, $blog_id);
2072 * Retrieve the url to the includes directory.
2074 * @package WordPress
2077 * @param string $path Optional. Path relative to the includes url.
2078 * @return string Includes url link with optional path appended.
2080 function includes_url($path = '') {
2081 $url = site_url() . '/' . WPINC . '/';
2083 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2084 $url .= ltrim($path, '/');
2086 return apply_filters('includes_url', $url, $path);
2090 * Retrieve the url to the content directory.
2092 * @package WordPress
2095 * @param string $path Optional. Path relative to the content url.
2096 * @return string Content url link with optional path appended.
2098 function content_url($path = '') {
2099 $url = WP_CONTENT_URL;
2100 if ( 0 === strpos($url, 'http') && is_ssl() )
2101 $url = str_replace( 'http://', 'https://', $url );
2103 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2104 $url .= '/' . ltrim($path, '/');
2106 return apply_filters('content_url', $url, $path);
2110 * Retrieve the url to the plugins directory or to a specific file within that directory.
2111 * You can hardcode the plugin slug in $path or pass __FILE__ as a second argument to get the correct folder name.
2113 * @package WordPress
2116 * @param string $path Optional. Path relative to the plugins url.
2117 * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
2118 * @return string Plugins url link with optional path appended.
2120 function plugins_url($path = '', $plugin = '') {
2122 $mu_plugin_dir = WPMU_PLUGIN_DIR;
2123 foreach ( array('path', 'plugin', 'mu_plugin_dir') as $var ) {
2124 $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
2125 $$var = preg_replace('|/+|', '/', $$var);
2128 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2129 $url = WPMU_PLUGIN_URL;
2131 $url = WP_PLUGIN_URL;
2133 if ( 0 === strpos($url, 'http') && is_ssl() )
2134 $url = str_replace( 'http://', 'https://', $url );
2136 if ( !empty($plugin) && is_string($plugin) ) {
2137 $folder = dirname(plugin_basename($plugin));
2138 if ( '.' != $folder )
2139 $url .= '/' . ltrim($folder, '/');
2142 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2143 $url .= '/' . ltrim($path, '/');
2145 return apply_filters('plugins_url', $url, $path, $plugin);
2149 * Retrieve the site url for the current network.
2151 * Returns the site url with the appropriate protocol, 'https' if
2152 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2155 * @package WordPress
2158 * @param string $path Optional. Path relative to the site url.
2159 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
2160 * @return string Site url link with optional path appended.
2162 function network_site_url( $path = '', $scheme = null ) {
2163 global $current_site;
2165 if ( !is_multisite() )
2166 return site_url($path, $scheme);
2168 $orig_scheme = $scheme;
2169 if ( !in_array($scheme, array('http', 'https')) ) {
2170 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
2172 elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
2174 elseif ( ('admin' == $scheme) && force_ssl_admin() )
2177 $scheme = ( is_ssl() ? 'https' : 'http' );
2180 $url = $scheme . '://' . $current_site->domain . $current_site->path;
2182 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2183 $url .= ltrim($path, '/');
2185 return apply_filters('network_site_url', $url, $path, $orig_scheme);
2189 * Retrieve the home url for the current network.
2191 * Returns the home url with the appropriate protocol, 'https' if
2192 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2195 * @package WordPress
2198 * @param string $path (optional) Path relative to the home url.
2199 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http','https'
2200 * @return string Home url link with optional path appended.
2202 function network_home_url( $path = '', $scheme = null ) {
2203 global $current_site;
2205 if ( !is_multisite() )
2206 return home_url($path, $scheme);
2208 $orig_scheme = $scheme;
2210 if ( !in_array($scheme, array('http', 'https')) )
2211 $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
2213 $url = $scheme . '://' . $current_site->domain . $current_site->path;
2215 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
2216 $url .= ltrim( $path, '/' );
2218 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
2222 * Retrieve the url to the admin area for the network.
2224 * @package WordPress
2227 * @param string $path Optional path relative to the admin url
2228 * @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.
2229 * @return string Admin url link with optional path appended
2231 function network_admin_url( $path = '', $scheme = 'admin' ) {
2232 if ( ! is_multisite() )
2233 return admin_url( $path, $scheme );
2235 $url = network_site_url('wp-admin/network/', $scheme);
2237 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2238 $url .= ltrim($path, '/');
2240 return apply_filters('network_admin_url', $url, $path);
2244 * Retrieve the url to the admin area for the current user.
2246 * @package WordPress
2249 * @param string $path Optional path relative to the admin url
2250 * @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.
2251 * @return string Admin url link with optional path appended
2253 function user_admin_url( $path = '', $scheme = 'admin' ) {
2254 $url = network_site_url('wp-admin/user/', $scheme);
2256 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2257 $url .= ltrim($path, '/');
2259 return apply_filters('user_admin_url', $url, $path);
2263 * Retrieve the url to the admin area for either the current blog or the network depending on context.
2265 * @package WordPress
2268 * @param string $path Optional path relative to the admin url
2269 * @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.
2270 * @return string Admin url link with optional path appended
2272 function self_admin_url($path = '', $scheme = 'admin') {
2273 if ( is_network_admin() )
2274 return network_admin_url($path, $scheme);
2275 elseif ( is_user_admin() )
2276 return user_admin_url($path, $scheme);
2278 return admin_url($path, $scheme);
2282 * Get the URL to the user's dashboard.
2284 * If a user does not belong to any sites, the global user dashboard is used. If the user belongs to the current site,
2285 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
2286 * primary blog is returned.
2290 * @param int $user_id User ID
2291 * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
2292 * @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.
2293 * @return string Dashboard url link with optional path appended
2295 function get_dashboard_url( $user_id, $path = '', $scheme = 'admin' ) {
2296 $user_id = (int) $user_id;
2298 $blogs = get_blogs_of_user( $user_id );
2299 if ( empty($blogs) ) {
2300 $url = user_admin_url( $path, $scheme );
2301 } elseif ( ! is_multisite() ) {
2302 $url = admin_url( $path, $scheme );
2304 $current_blog = get_current_blog_id();
2305 if ( $current_blog && in_array($current_blog, array_keys($blogs)) ) {
2306 $url = admin_url( $path, $scheme );
2308 $active = get_active_blog_for_user( $user_id );
2310 $url = get_admin_url( $active->blog_id, $path, $scheme );
2312 $url = user_admin_url( $path, $scheme );
2316 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
2320 * Get the URL to the user's profile editor.
2324 * @param int $user User ID
2325 * @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.
2326 * @return string Dashboard url link with optional path appended
2328 function get_edit_profile_url( $user, $scheme = 'admin' ) {
2329 $user = (int) $user;
2331 if ( is_user_admin() )
2332 $url = user_admin_url( 'profile.php', $scheme );
2333 elseif ( is_network_admin() )
2334 $url = network_admin_url( 'profile.php', $scheme );
2336 $url = get_dashboard_url( $user, 'profile.php', $scheme );
2338 return apply_filters( 'edit_profile_url', $url, $user, $scheme);
2342 * Output rel=canonical for singular queries
2344 * @package WordPress
2347 function rel_canonical() {
2348 if ( !is_singular() )
2351 global $wp_the_query;
2352 if ( !$id = $wp_the_query->get_queried_object_id() )
2355 $link = get_permalink( $id );
2356 echo "<link rel='canonical' href='$link' />\n";
2360 * Return a shortlink for a post, page, attachment, or blog.
2362 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
2363 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
2364 * Plugins can short circuit this function via the pre_get_shortlink filter or filter the output
2365 * via the get_shortlink filter.
2369 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
2370 * @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'.
2371 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
2372 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
2374 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
2375 // Allow plugins to short-circuit this function.
2376 $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
2377 if ( false !== $shortlink )
2382 if ( 'query' == $context && is_single() ) {
2383 $post_id = $wp_query->get_queried_object_id();
2384 } elseif ( 'post' == $context ) {
2385 $post = get_post($id);
2386 $post_id = $post->ID;
2391 // Return p= link for posts.
2392 if ( !empty($post_id) && '' != get_option('permalink_structure') ) {
2393 $post = get_post($post_id);
2394 if ( isset($post->post_type) && 'post' == $post->post_type )
2395 $shortlink = home_url('?p=' . $post->ID);
2398 return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);
2402 * Inject rel=sortlink into head if a shortlink is defined for the current page.
2404 * Attached to the wp_head action.
2408 * @uses wp_get_shortlink()
2410 function wp_shortlink_wp_head() {
2411 $shortlink = wp_get_shortlink( 0, 'query' );
2413 if ( empty( $shortlink ) )
2416 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
2420 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
2422 * Attached to the wp action.
2426 * @uses wp_get_shortlink()
2428 function wp_shortlink_header() {
2429 if ( headers_sent() )
2432 $shortlink = wp_get_shortlink(0, 'query');
2434 if ( empty($shortlink) )
2437 header('Link: <' . $shortlink . '>; rel=shortlink', false);
2441 * Display the Short Link for a Post
2443 * Must be called from inside "The Loop"
2445 * Call like the_shortlink(__('Shortlinkage FTW'))
2449 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
2450 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
2451 * @param string $before Optional HTML to display before the link.
2452 * @param string $before Optional HTML to display after the link.
2454 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
2457 if ( empty( $text ) )
2458 $text = __('This is the short link.');
2460 if ( empty( $title ) )
2461 $title = the_title_attribute( array( 'echo' => FALSE ) );
2463 $shortlink = wp_get_shortlink( $post->ID );
2465 if ( !empty( $shortlink ) ) {
2466 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
2467 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
2468 echo $before, $link, $after;