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 a URL with or without a trailing slash.
31 * @param $type_of_url String 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
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 = 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) && ( ( isset($post->post_status) && !$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 $link = $wp_rewrite->get_page_permastruct();
276 if ( '' != $link && ( ( isset($post->post_status) && 'draft' != $post->post_status && 'pending' != $post->post_status ) || $sample ) ) {
278 $link = str_replace('%pagename%', get_page_uri($id), $link);
279 $link = home_url($link);
280 $link = user_trailingslashit($link, 'page');
282 $link = home_url("?page_id=$id");
285 return apply_filters( '_get_page_link', $link, $id );
289 * Retrieve permalink for attachment.
291 * This can be used in the WordPress Loop or outside of it.
295 * @param int $id Optional. Post ID.
298 function get_attachment_link($id = false) {
299 global $post, $wp_rewrite;
304 $id = (int) $post->ID;
306 $object = get_post($id);
307 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
308 $parent = get_post($object->post_parent);
309 if ( 'page' == $parent->post_type )
310 $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
312 $parentlink = get_permalink( $object->post_parent );
314 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
315 $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
317 $name = $object->post_name;
319 if ( strpos($parentlink, '?') === false )
320 $link = user_trailingslashit( trailingslashit($parentlink) . $name );
324 $link = home_url( "/?attachment_id=$id" );
326 return apply_filters('attachment_link', $link, $id);
330 * Retrieve the permalink for the year archives.
334 * @param int|bool $year False for current year or year for permalink.
337 function get_year_link($year) {
340 $year = gmdate('Y', current_time('timestamp'));
341 $yearlink = $wp_rewrite->get_year_permastruct();
342 if ( !empty($yearlink) ) {
343 $yearlink = str_replace('%year%', $year, $yearlink);
344 return apply_filters('year_link', home_url( user_trailingslashit($yearlink, 'year') ), $year);
346 return apply_filters('year_link', home_url('?m=' . $year), $year);
351 * Retrieve the permalink for the month archives with year.
355 * @param bool|int $year False for current year. Integer of year.
356 * @param bool|int $month False for current month. Integer of month.
359 function get_month_link($year, $month) {
362 $year = gmdate('Y', current_time('timestamp'));
364 $month = gmdate('m', current_time('timestamp'));
365 $monthlink = $wp_rewrite->get_month_permastruct();
366 if ( !empty($monthlink) ) {
367 $monthlink = str_replace('%year%', $year, $monthlink);
368 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
369 return apply_filters('month_link', home_url( user_trailingslashit($monthlink, 'month') ), $year, $month);
371 return apply_filters('month_link', home_url( '?m=' . $year . zeroise($month, 2) ), $year, $month);
376 * Retrieve the permalink for the day archives with year and month.
380 * @param bool|int $year False for current year. Integer of year.
381 * @param bool|int $month False for current month. Integer of month.
382 * @param bool|int $day False for current day. Integer of day.
385 function get_day_link($year, $month, $day) {
388 $year = gmdate('Y', current_time('timestamp'));
390 $month = gmdate('m', current_time('timestamp'));
392 $day = gmdate('j', current_time('timestamp'));
394 $daylink = $wp_rewrite->get_day_permastruct();
395 if ( !empty($daylink) ) {
396 $daylink = str_replace('%year%', $year, $daylink);
397 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
398 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
399 return apply_filters('day_link', home_url( user_trailingslashit($daylink, 'day') ), $year, $month, $day);
401 return apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2) . zeroise($day, 2) ), $year, $month, $day);
406 * Display the permalink for the feed type.
410 * @param string $anchor The link's anchor text.
411 * @param string $feed Optional, defaults to default feed. Feed type.
413 function the_feed_link( $anchor, $feed = '' ) {
414 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
415 echo apply_filters( 'the_feed_link', $link, $feed );
419 * Retrieve the permalink for the feed type.
423 * @param string $feed Optional, defaults to default feed. Feed type.
426 function get_feed_link($feed = '') {
429 $permalink = $wp_rewrite->get_feed_permastruct();
430 if ( '' != $permalink ) {
431 if ( false !== strpos($feed, 'comments_') ) {
432 $feed = str_replace('comments_', '', $feed);
433 $permalink = $wp_rewrite->get_comment_feed_permastruct();
436 if ( get_default_feed() == $feed )
439 $permalink = str_replace('%feed%', $feed, $permalink);
440 $permalink = preg_replace('#/+#', '/', "/$permalink");
441 $output = home_url( user_trailingslashit($permalink, 'feed') );
444 $feed = get_default_feed();
446 if ( false !== strpos($feed, 'comments_') )
447 $feed = str_replace('comments_', 'comments-', $feed);
449 $output = home_url("?feed={$feed}");
452 return apply_filters('feed_link', $output, $feed);
456 * Retrieve the permalink for the post comments feed.
460 * @param int $post_id Optional. Post ID.
461 * @param string $feed Optional. Feed type.
464 function get_post_comments_feed_link($post_id = '', $feed = '') {
467 if ( empty($post_id) )
468 $post_id = (int) $id;
471 $feed = get_default_feed();
473 if ( '' != get_option('permalink_structure') ) {
474 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
475 $url = _get_page_link( $post_id );
477 $url = get_permalink($post_id);
479 $url = trailingslashit($url) . 'feed';
480 if ( $feed != get_default_feed() )
482 $url = user_trailingslashit($url, 'single_feed');
484 $type = get_post_field('post_type', $post_id);
485 if ( 'page' == $type )
486 $url = home_url("?feed=$feed&page_id=$post_id");
488 $url = home_url("?feed=$feed&p=$post_id");
491 return apply_filters('post_comments_feed_link', $url);
495 * Display the comment feed link for a post.
497 * Prints out the comment feed link for a post. Link text is placed in the
498 * anchor. If no link text is specified, default text is used. If no post ID is
499 * specified, the current post is used.
505 * @param string $link_text Descriptive text.
506 * @param int $post_id Optional post ID. Default to current post.
507 * @param string $feed Optional. Feed format.
508 * @return string Link to the comment feed for the current post.
510 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
511 $url = get_post_comments_feed_link($post_id, $feed);
512 if ( empty($link_text) )
513 $link_text = __('Comments Feed');
515 echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
519 * Retrieve the feed link for a given author.
521 * Returns a link to the feed for all posts by a given author. A specific feed
522 * can be requested or left blank to get the default feed.
528 * @param int $author_id ID of an author.
529 * @param string $feed Optional. Feed type.
530 * @return string Link to the feed for the author specified by $author_id.
532 function get_author_feed_link( $author_id, $feed = '' ) {
533 $author_id = (int) $author_id;
534 $permalink_structure = get_option('permalink_structure');
537 $feed = get_default_feed();
539 if ( '' == $permalink_structure ) {
540 $link = home_url("?feed=$feed&author=" . $author_id);
542 $link = get_author_posts_url($author_id);
543 if ( $feed == get_default_feed() )
546 $feed_link = "feed/$feed";
548 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
551 $link = apply_filters('author_feed_link', $link, $feed);
557 * Retrieve the feed link for a category.
559 * Returns a link to the feed for all post in a given category. A specific feed
560 * can be requested or left blank to get the default feed.
566 * @param int $cat_id ID of a category.
567 * @param string $feed Optional. Feed type.
568 * @return string Link to the feed for the category specified by $cat_id.
570 function get_category_feed_link($cat_id, $feed = '') {
571 return get_term_feed_link($cat_id, 'category', $feed);
575 * Retrieve the feed link for a taxonomy.
577 * Returns a link to the feed for all post in a given term. A specific feed
578 * can be requested or left blank to get the default feed.
582 * @param int $term_id ID of a category.
583 * @param string $taxonomy Optional. Taxonomy of $term_id
584 * @param string $feed Optional. Feed type.
585 * @return string Link to the feed for the taxonomy specified by $term_id and $taxonomy.
587 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
590 $term_id = ( int ) $term_id;
592 $term = get_term( $term_id, $taxonomy );
594 if ( empty( $term ) || is_wp_error( $term ) )
597 if ( empty( $feed ) )
598 $feed = get_default_feed();
600 $permalink_structure = get_option( 'permalink_structure' );
602 if ( '' == $permalink_structure ) {
603 if ( 'category' == $taxonomy ) {
604 $link = home_url("?feed=$feed&cat=$term_id");
606 elseif ( 'post_tag' == $taxonomy ) {
607 $link = home_url("?feed=$feed&tag=$term->slug");
609 $t = get_taxonomy( $taxonomy );
610 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
613 $link = get_term_link( $term_id, $term->taxonomy );
614 if ( $feed == get_default_feed() )
617 $feed_link = "feed/$feed";
619 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
622 if ( 'category' == $taxonomy )
623 $link = apply_filters( 'category_feed_link', $link, $feed );
624 elseif ( 'post_tag' == $taxonomy )
625 $link = apply_filters( 'category_feed_link', $link, $feed );
627 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
634 * Retrieve permalink for feed of tag.
638 * @param int $tag_id Tag ID.
639 * @param string $feed Optional. Feed type.
642 function get_tag_feed_link($tag_id, $feed = '') {
643 return get_term_feed_link($tag_id, 'post_tag', $feed);
647 * Retrieve edit tag link.
651 * @param int $tag_id Tag ID
654 function get_edit_tag_link( $tag_id = 0, $taxonomy = 'post_tag' ) {
656 $tax = get_taxonomy($taxonomy);
657 if ( !current_user_can($tax->cap->edit_terms) )
660 $tag = get_term($tag_id, $taxonomy);
662 $location = admin_url('edit-tags.php?action=edit&taxonomy=' . $taxonomy . '&' . (!empty($post_type) ? 'post_type=' . $post_type .'&' : '') .'tag_ID=' . $tag->term_id);
663 return apply_filters( 'get_edit_tag_link', $location );
667 * Display or retrieve edit tag link with formatting.
671 * @param string $link Optional. Anchor text.
672 * @param string $before Optional. Display before edit link.
673 * @param string $after Optional. Display after edit link.
674 * @param int|object $tag Tag object or ID
675 * @return string|null HTML content, if $echo is set to false.
677 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
678 $tax = get_taxonomy('post_tag');
679 if ( !current_user_can($tax->cap->edit_terms) )
682 $tag = get_term($tag, 'post_tag');
685 $link = __('Edit This');
687 $link = '<a href="' . get_edit_tag_link( $tag->term_id ) . '" title="' . __( 'Edit Tag' ) . '">' . $link . '</a>';
688 echo $before . apply_filters( 'edit_tag_link', $link, $tag->term_id ) . $after;
692 * Retrieve permalink for search.
695 * @param string $query Optional. The query string to use. If empty the current query is used.
698 function get_search_link( $query = '' ) {
702 $search = get_search_query( false );
704 $search = stripslashes($query);
706 $permastruct = $wp_rewrite->get_search_permastruct();
708 if ( empty( $permastruct ) ) {
709 $link = home_url('?s=' . urlencode($search) );
711 $search = urlencode($search);
712 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
713 $link = str_replace( '%search%', $search, $permastruct );
714 $link = home_url( user_trailingslashit( $link, 'search' ) );
717 return apply_filters( 'search_link', $link, $search );
721 * Retrieve the permalink for the feed of the search results.
725 * @param string $search_query Optional. Search query.
726 * @param string $feed Optional. Feed type.
729 function get_search_feed_link($search_query = '', $feed = '') {
731 $link = get_search_link($search_query);
734 $feed = get_default_feed();
736 $permastruct = $wp_rewrite->get_search_permastruct();
738 if ( empty($permastruct) ) {
739 $link = add_query_arg('feed', $feed, $link);
741 $link = trailingslashit($link);
742 $link .= "feed/$feed/";
745 $link = apply_filters('search_feed_link', $link, $feed, 'posts');
751 * Retrieve the permalink for the comments feed of the search results.
755 * @param string $search_query Optional. Search query.
756 * @param string $feed Optional. Feed type.
759 function get_search_comments_feed_link($search_query = '', $feed = '') {
763 $feed = get_default_feed();
765 $link = get_search_feed_link($search_query, $feed);
767 $permastruct = $wp_rewrite->get_search_permastruct();
769 if ( empty($permastruct) )
770 $link = add_query_arg('feed', 'comments-' . $feed, $link);
772 $link = add_query_arg('withcomments', 1, $link);
774 $link = apply_filters('search_feed_link', $link, $feed, 'comments');
780 * Retrieve edit posts link for post.
782 * Can be used within the WordPress loop or outside of it. Can be used with
783 * pages, posts, attachments, and revisions.
787 * @param int $id Optional. Post ID.
788 * @param string $context Optional, default to display. How to write the '&', defaults to '&'.
791 function get_edit_post_link( $id = 0, $context = 'display' ) {
792 if ( !$post = &get_post( $id ) )
795 if ( 'display' == $context )
796 $action = '&action=edit';
798 $action = '&action=edit';
800 $post_type_object = get_post_type_object( $post->post_type );
801 if ( !$post_type_object )
804 if ( !current_user_can( $post_type_object->cap->edit_post, $post->ID ) )
807 return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
811 * Display edit post link for post.
815 * @param string $link Optional. Anchor text.
816 * @param string $before Optional. Display before edit link.
817 * @param string $after Optional. Display after edit link.
818 * @param int $id Optional. Post ID.
820 function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
821 if ( !$post = &get_post( $id ) )
824 if ( !$url = get_edit_post_link( $post->ID ) )
827 if ( null === $link )
828 $link = __('Edit This');
830 $post_type_obj = get_post_type_object( $post->post_type );
831 $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( $post_type_obj->labels->edit_item ) . '">' . $link . '</a>';
832 echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
836 * Retrieve delete posts link for post.
838 * Can be used within the WordPress loop or outside of it, with any post type.
842 * @param int $id Optional. Post ID.
843 * @param string $deprecated Not used.
844 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
847 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
848 if ( ! empty( $deprecated ) )
849 _deprecated_argument( __FUNCTION__, '3.0.0' );
851 if ( !$post = &get_post( $id ) )
854 $post_type_object = get_post_type_object( $post->post_type );
855 if ( !$post_type_object )
858 if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) )
861 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
863 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
865 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-{$post->post_type}_{$post->ID}" ), $post->ID, $force_delete );
869 * Retrieve edit comment link.
873 * @param int $comment_id Optional. Comment ID.
876 function get_edit_comment_link( $comment_id = 0 ) {
877 $comment = &get_comment( $comment_id );
878 $post = &get_post( $comment->comment_post_ID );
880 if ( $post->post_type == 'page' ) {
881 if ( !current_user_can( 'edit_page', $post->ID ) )
884 if ( !current_user_can( 'edit_post', $post->ID ) )
888 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
889 return apply_filters( 'get_edit_comment_link', $location );
893 * Display or retrieve edit comment link with formatting.
897 * @param string $link Optional. Anchor text.
898 * @param string $before Optional. Display before edit link.
899 * @param string $after Optional. Display after edit link.
900 * @return string|null HTML content, if $echo is set to false.
902 function edit_comment_link( $link = null, $before = '', $after = '' ) {
903 global $comment, $post;
905 if ( $post->post_type == 'page' ) {
906 if ( !current_user_can( 'edit_page', $post->ID ) )
909 if ( !current_user_can( 'edit_post', $post->ID ) )
913 if ( null === $link )
914 $link = __('Edit This');
916 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
917 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
921 * Display edit bookmark (literally a URL external to blog) link.
925 * @param int $link Optional. Bookmark ID.
928 function get_edit_bookmark_link( $link = 0 ) {
929 $link = get_bookmark( $link );
931 if ( !current_user_can('manage_links') )
934 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
935 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
939 * Display edit bookmark (literally a URL external to blog) link anchor content.
943 * @param string $link Optional. Anchor text.
944 * @param string $before Optional. Display before edit link.
945 * @param string $after Optional. Display after edit link.
946 * @param int $bookmark Optional. Bookmark ID.
948 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
949 $bookmark = get_bookmark($bookmark);
951 if ( !current_user_can('manage_links') )
955 $link = __('Edit This');
957 $link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit Link' ) . '">' . $link . '</a>';
958 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
964 * Retrieve previous post link that is adjacent to current post.
968 * @param bool $in_same_cat Optional. Whether link should be in same category.
969 * @param string $excluded_categories Optional. Excluded categories IDs.
972 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
973 return get_adjacent_post($in_same_cat, $excluded_categories);
977 * Retrieve next post link that is adjacent to current post.
981 * @param bool $in_same_cat Optional. Whether link should be in same category.
982 * @param string $excluded_categories Optional. Excluded categories IDs.
985 function get_next_post($in_same_cat = false, $excluded_categories = '') {
986 return get_adjacent_post($in_same_cat, $excluded_categories, false);
990 * Retrieve adjacent post link.
992 * Can either be next or previous post link.
996 * @param bool $in_same_cat Optional. Whether link should be in same category.
997 * @param string $excluded_categories Optional. Excluded categories IDs.
998 * @param bool $previous Optional. Whether to retrieve previous post.
1001 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
1002 global $post, $wpdb;
1004 if ( empty( $post ) )
1007 $current_post_date = $post->post_date;
1010 $posts_in_ex_cats_sql = '';
1011 if ( $in_same_cat || !empty($excluded_categories) ) {
1012 $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";
1014 if ( $in_same_cat ) {
1015 $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
1016 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
1019 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
1020 if ( !empty($excluded_categories) ) {
1021 $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
1022 if ( !empty($cat_array) ) {
1023 $excluded_categories = array_diff($excluded_categories, $cat_array);
1024 $posts_in_ex_cats_sql = '';
1027 if ( !empty($excluded_categories) ) {
1028 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
1033 $adjacent = $previous ? 'previous' : 'next';
1034 $op = $previous ? '<' : '>';
1035 $order = $previous ? 'DESC' : 'ASC';
1037 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
1038 $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 );
1039 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1041 $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
1042 $query_key = 'adjacent_post_' . md5($query);
1043 $result = wp_cache_get($query_key, 'counts');
1044 if ( false !== $result )
1047 $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
1048 if ( null === $result )
1051 wp_cache_set($query_key, $result, 'counts');
1056 * Get adjacent post relational link.
1058 * Can either be next or previous post relational link.
1062 * @param string $title Optional. Link title format.
1063 * @param bool $in_same_cat Optional. Whether link should be in same category.
1064 * @param string $excluded_categories Optional. Excluded categories IDs.
1065 * @param bool $previous Optional, default is true. Whether display link to previous post.
1068 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
1069 if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) )
1070 $post = & get_post($GLOBALS['post']->post_parent);
1072 $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
1077 if ( empty($post->post_title) )
1078 $post->post_title = $previous ? __('Previous Post') : __('Next Post');
1080 $date = mysql2date(get_option('date_format'), $post->post_date);
1082 $title = str_replace('%title', $post->post_title, $title);
1083 $title = str_replace('%date', $date, $title);
1084 $title = apply_filters('the_title', $title, $post->ID);
1086 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1087 $link .= esc_attr( $title );
1088 $link .= "' href='" . get_permalink($post) . "' />\n";
1090 $adjacent = $previous ? 'previous' : 'next';
1091 return apply_filters( "{$adjacent}_post_rel_link", $link );
1095 * Display relational links for the posts adjacent to the current post.
1099 * @param string $title Optional. Link title format.
1100 * @param bool $in_same_cat Optional. Whether link should be in same category.
1101 * @param string $excluded_categories Optional. Excluded categories IDs.
1103 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1104 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
1105 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
1109 * Display relational links for the posts adjacent to the current post for single post pages.
1111 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1115 function adjacent_posts_rel_link_wp_head() {
1116 if ( !is_singular() || is_attachment() )
1118 adjacent_posts_rel_link();
1122 * Display relational link for the next post adjacent to the current post.
1126 * @param string $title Optional. Link title format.
1127 * @param bool $in_same_cat Optional. Whether link should be in same category.
1128 * @param string $excluded_categories Optional. Excluded categories IDs.
1130 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1131 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
1135 * Display relational link for the previous post adjacent to the current post.
1139 * @param string $title Optional. Link title format.
1140 * @param bool $in_same_cat Optional. Whether link should be in same category.
1141 * @param string $excluded_categories Optional. Excluded categories IDs.
1143 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1144 echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
1148 * Retrieve boundary post.
1150 * Boundary being either the first or last post by publish date within the contraitns specified
1151 * by in same category or excluded categories.
1155 * @param bool $in_same_cat Optional. Whether returned post should be in same category.
1156 * @param string $excluded_categories Optional. Excluded categories IDs.
1157 * @param bool $previous Optional. Whether to retrieve first post.
1160 function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
1163 if ( empty($post) || !is_single() || is_attachment() )
1166 $cat_array = array();
1167 $excluded_categories = array();
1168 if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
1169 if ( !empty($in_same_cat) ) {
1170 $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
1173 if ( !empty($excluded_categories) ) {
1174 $excluded_categories = array_map('intval', explode(',', $excluded_categories));
1176 if ( !empty($cat_array) )
1177 $excluded_categories = array_diff($excluded_categories, $cat_array);
1179 $inverse_cats = array();
1180 foreach ( $excluded_categories as $excluded_category)
1181 $inverse_cats[] = $excluded_category * -1;
1182 $excluded_categories = $inverse_cats;
1186 $categories = implode(',', array_merge($cat_array, $excluded_categories) );
1188 $order = $start ? 'ASC' : 'DESC';
1190 return get_posts( array('numberposts' => 1, 'no_found_rows' => true, 'order' => $order, 'orderby' => 'ID', 'category' => $categories) );
1194 * Get boundary post relational link.
1196 * Can either be start or end post relational link.
1200 * @param string $title Optional. Link title format.
1201 * @param bool $in_same_cat Optional. Whether link should be in same category.
1202 * @param string $excluded_categories Optional. Excluded categories IDs.
1203 * @param bool $start Optional, default is true. Whether display link to first post.
1206 function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
1207 $posts = get_boundary_post($in_same_cat,$excluded_categories,$start);
1208 // If there is no post stop.
1209 if ( empty($posts) )
1212 // Even though we limited get_posts to return only 1 item it still returns an array of objects.
1215 if ( empty($post->post_title) )
1216 $post->post_title = $start ? __('First Post') : __('Last Post');
1218 $date = mysql2date(get_option('date_format'), $post->post_date);
1220 $title = str_replace('%title', $post->post_title, $title);
1221 $title = str_replace('%date', $date, $title);
1222 $title = apply_filters('the_title', $title, $post->ID);
1224 $link = $start ? "<link rel='start' title='" : "<link rel='end' title='";
1225 $link .= esc_attr($title);
1226 $link .= "' href='" . get_permalink($post) . "' />\n";
1228 $boundary = $start ? 'start' : 'end';
1229 return apply_filters( "{$boundary}_post_rel_link", $link );
1233 * Display relational link for the first post.
1237 * @param string $title Optional. Link title format.
1238 * @param bool $in_same_cat Optional. Whether link should be in same category.
1239 * @param string $excluded_categories Optional. Excluded categories IDs.
1241 function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1242 echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
1246 * Get site index relational link.
1252 function get_index_rel_link() {
1253 $link = "<link rel='index' title='" . esc_attr( get_bloginfo( 'name', 'display' ) ) . "' href='" . esc_url( user_trailingslashit( get_bloginfo( 'url', 'display' ) ) ) . "' />\n";
1254 return apply_filters( "index_rel_link", $link );
1258 * Display relational link for the site index.
1262 function index_rel_link() {
1263 echo get_index_rel_link();
1267 * Get parent post relational link.
1271 * @param string $title Optional. Link title format.
1274 function get_parent_post_rel_link($title = '%title') {
1275 if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
1276 $post = & get_post($GLOBALS['post']->post_parent);
1281 $date = mysql2date(get_option('date_format'), $post->post_date);
1283 $title = str_replace('%title', $post->post_title, $title);
1284 $title = str_replace('%date', $date, $title);
1285 $title = apply_filters('the_title', $title, $post->ID);
1287 $link = "<link rel='up' title='";
1288 $link .= esc_attr( $title );
1289 $link .= "' href='" . get_permalink($post) . "' />\n";
1291 return apply_filters( "parent_post_rel_link", $link );
1295 * Display relational link for parent item
1299 function parent_post_rel_link($title = '%title') {
1300 echo get_parent_post_rel_link($title);
1304 * Display previous post link that is adjacent to the current post.
1308 * @param string $format Optional. Link anchor format.
1309 * @param string $link Optional. Link permalink format.
1310 * @param bool $in_same_cat Optional. Whether link should be in same category.
1311 * @param string $excluded_categories Optional. Excluded categories IDs.
1313 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1314 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
1318 * Display next post link that is adjacent to the current post.
1322 * @param string $format Optional. Link anchor format.
1323 * @param string $link Optional. Link permalink format.
1324 * @param bool $in_same_cat Optional. Whether link should be in same category.
1325 * @param string $excluded_categories Optional. Excluded categories IDs.
1327 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1328 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
1332 * Display adjacent post link.
1334 * Can be either next post link or previous.
1338 * @param string $format Link anchor format.
1339 * @param string $link Link permalink format.
1340 * @param bool $in_same_cat Optional. Whether link should be in same category.
1341 * @param string $excluded_categories Optional. Excluded categories IDs.
1342 * @param bool $previous Optional, default is true. Whether display link to previous post.
1344 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
1345 if ( $previous && is_attachment() )
1346 $post = & get_post($GLOBALS['post']->post_parent);
1348 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
1353 $title = $post->post_title;
1355 if ( empty($post->post_title) )
1356 $title = $previous ? __('Previous Post') : __('Next Post');
1358 $title = apply_filters('the_title', $title, $post->ID);
1359 $date = mysql2date(get_option('date_format'), $post->post_date);
1360 $rel = $previous ? 'prev' : 'next';
1362 $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
1363 $link = str_replace('%title', $title, $link);
1364 $link = str_replace('%date', $date, $link);
1365 $link = $string . $link . '</a>';
1367 $format = str_replace('%link', $link, $format);
1369 $adjacent = $previous ? 'previous' : 'next';
1370 echo apply_filters( "{$adjacent}_post_link", $format, $link );
1374 * Retrieve get links for page numbers.
1378 * @param int $pagenum Optional. Page ID.
1381 function get_pagenum_link($pagenum = 1) {
1384 $pagenum = (int) $pagenum;
1386 $request = remove_query_arg( 'paged' );
1388 $home_root = parse_url(home_url());
1389 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1390 $home_root = preg_quote( trailingslashit( $home_root ), '|' );
1392 $request = preg_replace('|^'. $home_root . '|', '', $request);
1393 $request = preg_replace('|^/+|', '', $request);
1395 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1396 $base = trailingslashit( get_bloginfo( 'url' ) );
1398 if ( $pagenum > 1 ) {
1399 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1401 $result = $base . $request;
1404 $qs_regex = '|\?.*?$|';
1405 preg_match( $qs_regex, $request, $qs_match );
1407 if ( !empty( $qs_match[0] ) ) {
1408 $query_string = $qs_match[0];
1409 $request = preg_replace( $qs_regex, '', $request );
1414 $request = preg_replace( '|page/\d+/?$|', '', $request);
1415 $request = preg_replace( '|^index\.php|', '', $request);
1416 $request = ltrim($request, '/');
1418 $base = trailingslashit( get_bloginfo( 'url' ) );
1420 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1421 $base .= 'index.php/';
1423 if ( $pagenum > 1 ) {
1424 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
1427 $result = $base . $request . $query_string;
1430 $result = apply_filters('get_pagenum_link', $result);
1436 * Retrieve next posts pages link.
1438 * Backported from 2.1.3 to 2.0.10.
1442 * @param int $max_page Optional. Max pages.
1445 function get_next_posts_page_link($max_page = 0) {
1448 if ( !is_single() ) {
1451 $nextpage = intval($paged) + 1;
1452 if ( !$max_page || $max_page >= $nextpage )
1453 return get_pagenum_link($nextpage);
1458 * Display or return the next posts pages link.
1462 * @param int $max_page Optional. Max pages.
1463 * @param boolean $echo Optional. Echo or return;
1465 function next_posts( $max_page = 0, $echo = true ) {
1466 $output = esc_url( get_next_posts_page_link( $max_page ) );
1475 * Return the next posts pages link.
1479 * @param string $label Content for link text.
1480 * @param int $max_page Optional. Max pages.
1481 * @return string|null
1483 function get_next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
1484 global $paged, $wp_query;
1487 $max_page = $wp_query->max_num_pages;
1492 $nextpage = intval($paged) + 1;
1494 if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
1495 $attr = apply_filters( 'next_posts_link_attributes', '' );
1496 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
1501 * Display the next posts pages link.
1504 * @uses get_next_posts_link()
1506 * @param string $label Content for link text.
1507 * @param int $max_page Optional. Max pages.
1509 function next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
1510 echo get_next_posts_link( $label, $max_page );
1514 * Retrieve previous post pages link.
1516 * Will only return string, if not on a single page or post.
1518 * Backported to 2.0.10 from 2.1.3.
1522 * @return string|null
1524 function get_previous_posts_page_link() {
1527 if ( !is_single() ) {
1528 $nextpage = intval($paged) - 1;
1529 if ( $nextpage < 1 )
1531 return get_pagenum_link($nextpage);
1536 * Display or return the previous posts pages link.
1540 * @param boolean $echo Optional. Echo or return;
1542 function previous_posts( $echo = true ) {
1543 $output = esc_url( get_previous_posts_page_link() );
1552 * Return the previous posts pages link.
1556 * @param string $label Optional. Previous page link text.
1557 * @return string|null
1559 function get_previous_posts_link( $label = '« Previous Page' ) {
1562 if ( !is_single() && $paged > 1 ) {
1563 $attr = apply_filters( 'previous_posts_link_attributes', '' );
1564 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&$1', $label ) .'</a>';
1569 * Display the previous posts page link.
1572 * @uses get_previous_posts_link()
1574 * @param string $label Optional. Previous page link text.
1576 function previous_posts_link( $label = '« Previous Page' ) {
1577 echo get_previous_posts_link( $label );
1581 * Return post pages link navigation for previous and next pages.
1585 * @param string|array $args Optional args.
1586 * @return string The posts link navigation.
1588 function get_posts_nav_link( $args = array() ) {
1593 if ( !is_singular() ) {
1595 'sep' => ' — ',
1596 'prelabel' => __('« Previous Page'),
1597 'nxtlabel' => __('Next Page »'),
1599 $args = wp_parse_args( $args, $defaults );
1601 $max_num_pages = $wp_query->max_num_pages;
1602 $paged = get_query_var('paged');
1604 //only have sep if there's both prev and next results
1605 if ($paged < 2 || $paged >= $max_num_pages) {
1609 if ( $max_num_pages > 1 ) {
1610 $return = get_previous_posts_link($args['prelabel']);
1611 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
1612 $return .= get_next_posts_link($args['nxtlabel']);
1620 * Display post pages link navigation for previous and next pages.
1624 * @param string $sep Optional. Separator for posts navigation links.
1625 * @param string $prelabel Optional. Label for previous pages.
1626 * @param string $nxtlabel Optional Label for next pages.
1628 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
1629 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
1630 echo get_posts_nav_link($args);
1634 * Retrieve page numbers links.
1638 * @param int $pagenum Optional. Page number.
1641 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
1642 global $post, $wp_rewrite;
1644 $pagenum = (int) $pagenum;
1646 $result = get_permalink( $post->ID );
1648 if ( 'newest' == get_option('default_comments_page') ) {
1649 if ( $pagenum != $max_page ) {
1650 if ( $wp_rewrite->using_permalinks() )
1651 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1653 $result = add_query_arg( 'cpage', $pagenum, $result );
1655 } elseif ( $pagenum > 1 ) {
1656 if ( $wp_rewrite->using_permalinks() )
1657 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1659 $result = add_query_arg( 'cpage', $pagenum, $result );
1662 $result .= '#comments';
1664 $result = apply_filters('get_comments_pagenum_link', $result);
1670 * Return the link to next comments pages.
1674 * @param string $label Optional. Label for link text.
1675 * @param int $max_page Optional. Max page.
1676 * @return string|null
1678 function get_next_comments_link( $label = '', $max_page = 0 ) {
1681 if ( !is_singular() || !get_option('page_comments') )
1684 $page = get_query_var('cpage');
1686 $nextpage = intval($page) + 1;
1688 if ( empty($max_page) )
1689 $max_page = $wp_query->max_num_comment_pages;
1691 if ( empty($max_page) )
1692 $max_page = get_comment_pages_count();
1694 if ( $nextpage > $max_page )
1697 if ( empty($label) )
1698 $label = __('Newer Comments »');
1700 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>';
1704 * Display the link to next comments pages.
1708 * @param string $label Optional. Label for link text.
1709 * @param int $max_page Optional. Max page.
1711 function next_comments_link( $label = '', $max_page = 0 ) {
1712 echo get_next_comments_link( $label, $max_page );
1716 * Return the previous comments page link.
1720 * @param string $label Optional. Label for comments link text.
1721 * @return string|null
1723 function get_previous_comments_link( $label = '' ) {
1724 if ( !is_singular() || !get_option('page_comments') )
1727 $page = get_query_var('cpage');
1729 if ( intval($page) <= 1 )
1732 $prevpage = intval($page) - 1;
1734 if ( empty($label) )
1735 $label = __('« Older Comments');
1737 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>';
1741 * Display the previous comments page link.
1745 * @param string $label Optional. Label for comments link text.
1747 function previous_comments_link( $label = '' ) {
1748 echo get_previous_comments_link( $label );
1752 * Create pagination links for the comments on the current post.
1754 * @see paginate_links()
1757 * @param string|array $args Optional args. See paginate_links.
1758 * @return string Markup for pagination links.
1760 function paginate_comments_links($args = array()) {
1763 if ( !is_singular() || !get_option('page_comments') )
1766 $page = get_query_var('cpage');
1769 $max_page = get_comment_pages_count();
1771 'base' => add_query_arg( 'cpage', '%#%' ),
1773 'total' => $max_page,
1776 'add_fragment' => '#comments'
1778 if ( $wp_rewrite->using_permalinks() )
1779 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
1781 $args = wp_parse_args( $args, $defaults );
1782 $page_links = paginate_links( $args );
1784 if ( $args['echo'] )
1791 * Retrieve shortcut link.
1793 * Use this in 'a' element 'href' attribute.
1799 function get_shortcut_link() {
1800 $link = "javascript:
1806 s=(e?e():(k)?k():(x?x.createRange().text:0)),
1807 f='" . admin_url('press-this.php') . "',
1809 e=encodeURIComponent,
1810 u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
1811 a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
1812 if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
1815 $link = str_replace(array("\r", "\n", "\t"), '', $link);
1817 return apply_filters('shortcut_link', $link);
1821 * Retrieve the home url for the current site.
1823 * Returns the 'home' option with the appropriate protocol, 'https' if
1824 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1827 * @package WordPress
1830 * @uses get_home_url()
1832 * @param string $path (optional) Path relative to the home url.
1833 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http','https'
1834 * @return string Home url link with optional path appended.
1836 function home_url( $path = '', $scheme = null ) {
1837 return get_home_url(null, $path, $scheme);
1841 * Retrieve the home url for a given site.
1843 * Returns the 'home' option with the appropriate protocol, 'https' if
1844 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1847 * @package WordPress
1850 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
1851 * @param string $path (optional) Path relative to the home url.
1852 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http','https'
1853 * @return string Home url link with optional path appended.
1855 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
1856 $orig_scheme = $scheme;
1858 if ( !in_array( $scheme, array( 'http', 'https' ) ) )
1859 $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
1861 if ( empty( $blog_id ) || !is_multisite() )
1862 $home = get_option( 'home' );
1864 $home = get_blog_option( $blog_id, 'home' );
1866 $url = str_replace( 'http://', "$scheme://", $home );
1868 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
1869 $url .= '/' . ltrim( $path, '/' );
1871 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
1875 * Retrieve the site url for the current site.
1877 * Returns the 'site_url' option with the appropriate protocol, 'https' if
1878 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1881 * @package WordPress
1884 * @uses get_site_url()
1886 * @param string $path Optional. Path relative to the site url.
1887 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
1888 * @return string Site url link with optional path appended.
1890 function site_url( $path = '', $scheme = null ) {
1891 return get_site_url(null, $path, $scheme);
1895 * Retrieve the site url for a given site.
1897 * Returns the 'site_url' option with the appropriate protocol, 'https' if
1898 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1901 * @package WordPress
1904 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
1905 * @param string $path Optional. Path relative to the site url.
1906 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
1907 * @return string Site url link with optional path appended.
1909 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
1910 // should the list of allowed schemes be maintained elsewhere?
1911 $orig_scheme = $scheme;
1912 if ( !in_array( $scheme, array( 'http', 'https' ) ) ) {
1913 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
1915 elseif ( ( 'login' == $scheme ) && force_ssl_admin() )
1917 elseif ( ( 'admin' == $scheme ) && force_ssl_admin() )
1920 $scheme = ( is_ssl() ? 'https' : 'http' );
1923 if ( empty( $blog_id ) || !is_multisite() )
1924 $url = get_option( 'siteurl' );
1926 $url = get_blog_option( $blog_id, 'siteurl' );
1928 $url = str_replace( 'http://', "{$scheme}://", $url );
1930 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
1931 $url .= '/' . ltrim( $path, '/' );
1933 return apply_filters( 'site_url', $url, $path, $orig_scheme, $blog_id );
1937 * Retrieve the url to the admin area for the current site.
1939 * @package WordPress
1942 * @param string $path Optional path relative to the admin url
1943 * @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.
1944 * @return string Admin url link with optional path appended
1946 function admin_url( $path = '', $scheme = 'admin' ) {
1947 return get_admin_url(null, $path, $scheme);
1951 * Retrieve the url to the admin area for a given site.
1953 * @package WordPress
1956 * @param int $blog_id (optional) Blog ID. Defaults to current blog.
1957 * @param string $path Optional path relative to the admin url
1958 * @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.
1959 * @return string Admin url link with optional path appended
1961 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
1962 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
1964 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1965 $url .= ltrim($path, '/');
1967 return apply_filters('admin_url', $url, $path, $blog_id);
1971 * Retrieve the url to the includes directory.
1973 * @package WordPress
1976 * @param string $path Optional. Path relative to the includes url.
1977 * @return string Includes url link with optional path appended.
1979 function includes_url($path = '') {
1980 $url = site_url() . '/' . WPINC . '/';
1982 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1983 $url .= ltrim($path, '/');
1985 return apply_filters('includes_url', $url, $path);
1989 * Retrieve the url to the content directory.
1991 * @package WordPress
1994 * @param string $path Optional. Path relative to the content url.
1995 * @return string Content url link with optional path appended.
1997 function content_url($path = '') {
1998 $url = WP_CONTENT_URL;
1999 if ( 0 === strpos($url, 'http') && is_ssl() )
2000 $url = str_replace( 'http://', 'https://', $url );
2002 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2003 $url .= '/' . ltrim($path, '/');
2005 return apply_filters('content_url', $url, $path);
2009 * Retrieve the url to the plugins directory or to a specific file within that directory.
2010 * You can hardcode the plugin slug in $path or pass __FILE__ as a second argument to get the correct folder name.
2012 * @package WordPress
2015 * @param string $path Optional. Path relative to the plugins url.
2016 * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
2017 * @return string Plugins url link with optional path appended.
2019 function plugins_url($path = '', $plugin = '') {
2021 $mu_plugin_dir = WPMU_PLUGIN_DIR;
2022 foreach ( array('path', 'plugin', 'mu_plugin_dir') as $var ) {
2023 $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
2024 $$var = preg_replace('|/+|', '/', $$var);
2027 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2028 $url = WPMU_PLUGIN_URL;
2030 $url = WP_PLUGIN_URL;
2032 if ( 0 === strpos($url, 'http') && is_ssl() )
2033 $url = str_replace( 'http://', 'https://', $url );
2035 if ( !empty($plugin) && is_string($plugin) ) {
2036 $folder = dirname(plugin_basename($plugin));
2037 if ( '.' != $folder )
2038 $url .= '/' . ltrim($folder, '/');
2041 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2042 $url .= '/' . ltrim($path, '/');
2044 return apply_filters('plugins_url', $url, $path, $plugin);
2048 * Retrieve the site url for the current network.
2050 * Returns the site url with the appropriate protocol, 'https' if
2051 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2054 * @package WordPress
2057 * @param string $path Optional. Path relative to the site url.
2058 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
2059 * @return string Site url link with optional path appended.
2061 function network_site_url( $path = '', $scheme = null ) {
2062 global $current_site;
2064 if ( !is_multisite() )
2065 return site_url($path, $scheme);
2067 $orig_scheme = $scheme;
2068 if ( !in_array($scheme, array('http', 'https')) ) {
2069 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
2071 elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
2073 elseif ( ('admin' == $scheme) && force_ssl_admin() )
2076 $scheme = ( is_ssl() ? 'https' : 'http' );
2079 $url = 'http://' . $current_site->domain . $current_site->path;
2081 $url = str_replace( 'http://', "{$scheme}://", $url );
2083 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2084 $url .= ltrim($path, '/');
2086 return apply_filters('network_site_url', $url, $path, $orig_scheme);
2090 * Retrieve the home url for the current network.
2092 * Returns the home url with the appropriate protocol, 'https' if
2093 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2096 * @package WordPress
2099 * @param string $path (optional) Path relative to the home url.
2100 * @param string $scheme (optional) Scheme to give the home url context. Currently 'http','https'
2101 * @return string Home url link with optional path appended.
2103 function network_home_url( $path = '', $scheme = null ) {
2104 global $current_site;
2106 if ( !is_multisite() )
2107 return home_url($path, $scheme);
2109 $orig_scheme = $scheme;
2111 if ( !in_array($scheme, array('http', 'https')) )
2112 $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
2114 $url = 'http://' . $current_site->domain . $current_site->path;
2116 $url = str_replace( 'http://', "$scheme://", $url );
2118 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
2119 $url .= ltrim( $path, '/' );
2121 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
2125 * Retrieve the url to the admin area for the network.
2127 * @package WordPress
2130 * @param string $path Optional path relative to the admin url
2131 * @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.
2132 * @return string Admin url link with optional path appended
2134 function network_admin_url( $path = '', $scheme = 'admin' ) {
2135 $url = network_site_url('wp-admin/', $scheme);
2137 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
2138 $url .= ltrim($path, '/');
2140 return apply_filters('network_admin_url', $url, $path);
2144 * Output rel=canonical for singular queries
2146 * @package WordPress
2149 function rel_canonical() {
2150 if ( !is_singular() )
2153 global $wp_the_query;
2154 if ( !$id = $wp_the_query->get_queried_object_id() )
2157 $link = get_permalink( $id );
2158 echo "<link rel='canonical' href='$link' />\n";
2162 * Return a shortlink for a post, page, attachment, or blog.
2164 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
2165 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
2166 * Plugins can short circuit this function via the pre_get_shortlink filter or filter the output
2167 * via the get_shortlink filter.
2171 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
2172 * @param string $contex 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'.
2173 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
2174 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
2176 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
2177 // Allow plugins to short-circuit this function.
2178 $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
2179 if ( false !== $shortlink )
2184 if ( 'query' == $context && is_single() ) {
2185 $post_id = $wp_query->get_queried_object_id();
2186 } elseif ( 'post' == $context ) {
2187 $post = get_post($id);
2188 $post_id = $post->ID;
2193 // Return p= link for posts.
2194 if ( !empty($post_id) && '' != get_option('permalink_structure') ) {
2195 $post = get_post($post_id);
2196 if ( isset($post->post_type) && 'post' == $post->post_type )
2197 $shortlink = home_url('?p=' . $post->ID);
2200 return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);
2204 * Inject rel=sortlink into head if a shortlink is defined for the current page.
2206 * Attached to the wp_head action.
2210 * @uses wp_get_shortlink()
2212 function wp_shortlink_wp_head() {
2213 $shortlink = wp_get_shortlink( 0, 'query' );
2215 if ( empty( $shortlink ) )
2218 echo "<link rel='shortlink' href='" . esc_url_raw( $shortlink ) . "' />\n";
2222 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
2224 * Attached to the wp action.
2228 * @uses wp_get_shortlink()
2230 function wp_shortlink_header() {
2231 if ( headers_sent() )
2234 $shortlink = wp_get_shortlink(0, 'query');
2236 if ( empty($shortlink) )
2239 header('Link: <' . $shortlink . '>; rel=shortlink', false);
2243 * Display the Short Link for a Post
2245 * Must be called from inside "The Loop"
2247 * Call like the_shortlink(__('Shortlinkage FTW'))
2251 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
2252 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
2253 * @param string $before Optional HTML to display before the link.
2254 * @param string $before Optional HTML to display after the link.
2256 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
2259 if ( empty( $text ) )
2260 $text = __('This is the short link.');
2262 if ( empty( $title ) )
2263 $title = the_title_attribute( array( 'echo' => FALSE ) );
2265 $shortlink = wp_get_shortlink( $post->ID );
2267 if ( !empty( $shortlink ) ) {
2268 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
2269 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
2270 echo $before, $link, $after;