]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/link-template.php
WordPress 3.9
[autoinstalls/wordpress.git] / wp-includes / link-template.php
1 <?php
2 /**
3  * WordPress Link Template Functions
4  *
5  * @package WordPress
6  * @subpackage Template
7  */
8
9 /**
10  * Display the permalink for the current post.
11  *
12  * @since 1.2.0
13  */
14 function the_permalink() {
15         /**
16          * Filter the display of the permalink for the current post.
17          *
18          * @since 1.5.0
19          *
20          * @param string $permalink The permalink for the current post.
21          */
22         echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
23 }
24
25 /**
26  * Retrieve trailing slash string, if blog set for adding trailing slashes.
27  *
28  * Conditionally adds a trailing slash if the permalink structure has a trailing
29  * slash, strips the trailing slash if not. The string is passed through the
30  * 'user_trailingslashit' filter. Will remove trailing slash from string, if
31  * blog is not set to have them.
32  *
33  * @since 2.2.0
34  * @uses $wp_rewrite
35  *
36  * @param string $string URL with or without a trailing slash.
37  * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
38  * @return string
39  */
40 function user_trailingslashit($string, $type_of_url = '') {
41         global $wp_rewrite;
42         if ( $wp_rewrite->use_trailing_slashes )
43                 $string = trailingslashit($string);
44         else
45                 $string = untrailingslashit($string);
46
47         /**
48          * Filter the trailing slashed string, depending on whether the site is set
49          * to use training slashes.
50          *
51          * @since 2.2.0
52          *
53          * @param string $string      URL with or without a trailing slash.
54          * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
55          *                            'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',
56          *                            'month', 'day', 'paged', 'post_type_archive'.
57          */
58         $string = apply_filters( 'user_trailingslashit', $string, $type_of_url );
59         return $string;
60 }
61
62 /**
63  * Display permalink anchor for current post.
64  *
65  * The permalink mode title will use the post title for the 'a' element 'id'
66  * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
67  *
68  * @since 0.71
69  *
70  * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
71  */
72 function permalink_anchor( $mode = 'id' ) {
73         $post = get_post();
74         switch ( strtolower( $mode ) ) {
75                 case 'title':
76                         $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
77                         echo '<a id="'.$title.'"></a>';
78                         break;
79                 case 'id':
80                 default:
81                         echo '<a id="post-' . $post->ID . '"></a>';
82                         break;
83         }
84 }
85
86 /**
87  * Retrieve full permalink for current post or post ID.
88  *
89  * This function is an alias for get_permalink().
90  *
91  * @since 3.9.0
92  *
93  * @see get_permalink()
94  *
95  * @param int|WP_Post $id        Optional. Post ID or post object. Default is the current post.
96  * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
97  * @return string|bool The permalink URL or false if post does not exist.
98  */
99 function get_the_permalink( $id = 0, $leavename = false ) {
100         return get_permalink( $id, $leavename );
101 }
102
103 /**
104  * Retrieve full permalink for current post or post ID.
105  *
106  * @since 1.0.0
107  *
108  * @param int|WP_Post $id        Optional. Post ID or post object. Default current post.
109  * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
110  * @return string|bool The permalink URL or false if post does not exist.
111  */
112 function get_permalink( $id = 0, $leavename = false ) {
113         $rewritecode = array(
114                 '%year%',
115                 '%monthnum%',
116                 '%day%',
117                 '%hour%',
118                 '%minute%',
119                 '%second%',
120                 $leavename? '' : '%postname%',
121                 '%post_id%',
122                 '%category%',
123                 '%author%',
124                 $leavename? '' : '%pagename%',
125         );
126
127         if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
128                 $post = $id;
129                 $sample = true;
130         } else {
131                 $post = get_post($id);
132                 $sample = false;
133         }
134
135         if ( empty($post->ID) )
136                 return false;
137
138         if ( $post->post_type == 'page' )
139                 return get_page_link($post->ID, $leavename, $sample);
140         elseif ( $post->post_type == 'attachment' )
141                 return get_attachment_link( $post->ID, $leavename );
142         elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
143                 return get_post_permalink($post->ID, $leavename, $sample);
144
145         $permalink = get_option('permalink_structure');
146
147         /**
148          * Filter the permalink structure for a post before token replacement occurs.
149          *
150          * Only applies to posts with post_type of 'post'.
151          *
152          * @since 3.0.0
153          *
154          * @param string  $permalink The site's permalink structure.
155          * @param WP_Post $post      The post in question.
156          * @param bool    $leavename Whether to keep the post name.
157          */
158         $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
159
160         if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
161                 $unixtime = strtotime($post->post_date);
162
163                 $category = '';
164                 if ( strpos($permalink, '%category%') !== false ) {
165                         $cats = get_the_category($post->ID);
166                         if ( $cats ) {
167                                 usort($cats, '_usort_terms_by_ID'); // order by ID
168
169                                 /**
170                                  * Filter the category that gets used in the %category% permalink token.
171                                  *
172                                  * @since 3.5.0
173                                  *
174                                  * @param stdClass $cat  The category to use in the permalink.
175                                  * @param array    $cats Array of all categories associated with the post.
176                                  * @param WP_Post  $post The post in question.
177                                  */
178                                 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
179
180                                 $category_object = get_term( $category_object, 'category' );
181                                 $category = $category_object->slug;
182                                 if ( $parent = $category_object->parent )
183                                         $category = get_category_parents($parent, false, '/', true) . $category;
184                         }
185                         // show default category in permalinks, without
186                         // having to assign it explicitly
187                         if ( empty($category) ) {
188                                 $default_category = get_term( get_option( 'default_category' ), 'category' );
189                                 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
190                         }
191                 }
192
193                 $author = '';
194                 if ( strpos($permalink, '%author%') !== false ) {
195                         $authordata = get_userdata($post->post_author);
196                         $author = $authordata->user_nicename;
197                 }
198
199                 $date = explode(" ",date('Y m d H i s', $unixtime));
200                 $rewritereplace =
201                 array(
202                         $date[0],
203                         $date[1],
204                         $date[2],
205                         $date[3],
206                         $date[4],
207                         $date[5],
208                         $post->post_name,
209                         $post->ID,
210                         $category,
211                         $author,
212                         $post->post_name,
213                 );
214                 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
215                 $permalink = user_trailingslashit($permalink, 'single');
216         } else { // if they're not using the fancy permalink option
217                 $permalink = home_url('?p=' . $post->ID);
218         }
219
220         /**
221          * Filter the permalink for a post.
222          *
223          * Only applies to posts with post_type of 'post'.
224          *
225          * @since 1.5.0
226          *
227          * @param string  $permalink The post's permalink.
228          * @param WP_Post $post      The post in question.
229          * @param bool    $leavename Whether to keep the post name.
230          */
231         return apply_filters( 'post_link', $permalink, $post, $leavename );
232 }
233
234 /**
235  * Retrieve the permalink for a post with a custom post type.
236  *
237  * @since 3.0.0
238  *
239  * @param int $id Optional. Post ID.
240  * @param bool $leavename Optional, defaults to false. Whether to keep post name.
241  * @param bool $sample Optional, defaults to false. Is it a sample permalink.
242  * @return string
243  */
244 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
245         global $wp_rewrite;
246
247         $post = get_post($id);
248
249         if ( is_wp_error( $post ) )
250                 return $post;
251
252         $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
253
254         $slug = $post->post_name;
255
256         $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
257
258         $post_type = get_post_type_object($post->post_type);
259
260         if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
261                 if ( ! $leavename ) {
262                         if ( $post_type->hierarchical )
263                                 $slug = get_page_uri($id);
264                         $post_link = str_replace("%$post->post_type%", $slug, $post_link);
265                 }
266                 $post_link = home_url( user_trailingslashit($post_link) );
267         } else {
268                 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
269                         $post_link = add_query_arg($post_type->query_var, $slug, '');
270                 else
271                         $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
272                 $post_link = home_url($post_link);
273         }
274
275         /**
276          * Filter the permalink for a post with a custom post type.
277          *
278          * @since 3.0.0
279          *
280          * @param string  $post_link The post's permalink.
281          * @param WP_Post $post      The post in question.
282          * @param bool    $leavename Whether to keep the post name.
283          * @param bool    $sample    Is it a sample permalink.
284          */
285         return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
286 }
287
288 /**
289  * Retrieve permalink from post ID.
290  *
291  * @since 1.0.0
292  *
293  * @param int $post_id Optional. Post ID.
294  * @param mixed $deprecated Not used.
295  * @return string
296  */
297 function post_permalink( $post_id = 0, $deprecated = '' ) {
298         if ( !empty( $deprecated ) )
299                 _deprecated_argument( __FUNCTION__, '1.3' );
300
301         return get_permalink($post_id);
302 }
303
304 /**
305  * Retrieve the permalink for current page or page ID.
306  *
307  * Respects page_on_front. Use this one.
308  *
309  * @since 1.5.0
310  *
311  * @param int|object $post Optional. Post ID or object.
312  * @param bool $leavename Optional, defaults to false. Whether to keep page name.
313  * @param bool $sample Optional, defaults to false. Is it a sample permalink.
314  * @return string
315  */
316 function get_page_link( $post = false, $leavename = false, $sample = false ) {
317         $post = get_post( $post );
318
319         if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
320                 $link = home_url('/');
321         else
322                 $link = _get_page_link( $post, $leavename, $sample );
323
324         /**
325          * Filter the permalink for a page.
326          *
327          * @since 1.5.0
328          *
329          * @param string $link    The page's permalink.
330          * @param int    $post_id The ID of the page.
331          * @param bool   $sample  Is it a sample permalink.
332          */
333         return apply_filters( 'page_link', $link, $post->ID, $sample );
334 }
335
336 /**
337  * Retrieve the page permalink.
338  *
339  * Ignores page_on_front. Internal use only.
340  *
341  * @since 2.1.0
342  * @access private
343  *
344  * @param int|object $post Optional. Post ID or object.
345  * @param bool $leavename Optional. Leave name.
346  * @param bool $sample Optional. Sample permalink.
347  * @return string
348  */
349 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
350         global $wp_rewrite;
351
352         $post = get_post( $post );
353
354         $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
355
356         $link = $wp_rewrite->get_page_permastruct();
357
358         if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
359                 if ( ! $leavename ) {
360                         $link = str_replace('%pagename%', get_page_uri( $post ), $link);
361                 }
362
363                 $link = home_url($link);
364                 $link = user_trailingslashit($link, 'page');
365         } else {
366                 $link = home_url( '?page_id=' . $post->ID );
367         }
368
369         /**
370          * Filter the permalink for a non-page_on_front page.
371          *
372          * @since 2.1.0
373          *
374          * @param string $link    The page's permalink.
375          * @param int    $post_id The ID of the page.
376          */
377         return apply_filters( '_get_page_link', $link, $post->ID );
378 }
379
380 /**
381  * Retrieve permalink for attachment.
382  *
383  * This can be used in the WordPress Loop or outside of it.
384  *
385  * @since 2.0.0
386  *
387  * @param int|object $post Optional. Post ID or object.
388  * @param bool $leavename Optional. Leave name.
389  * @return string
390  */
391 function get_attachment_link( $post = null, $leavename = false ) {
392         global $wp_rewrite;
393
394         $link = false;
395
396         $post = get_post( $post );
397         $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
398
399         if ( $wp_rewrite->using_permalinks() && $parent ) {
400                 if ( 'page' == $parent->post_type )
401                         $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
402                 else
403                         $parentlink = get_permalink( $post->post_parent );
404
405                 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
406                         $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
407                 else
408                         $name = $post->post_name;
409
410                 if ( strpos($parentlink, '?') === false )
411                         $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
412
413                 if ( ! $leavename )
414                         $link = str_replace( '%postname%', $name, $link );
415         }
416
417         if ( ! $link )
418                 $link = home_url( '/?attachment_id=' . $post->ID );
419
420         /**
421          * Filter the permalink for an attachment.
422          *
423          * @since 2.0.0
424          *
425          * @param string $link    The attachment's permalink.
426          * @param int    $post_id Attachment ID.
427          */
428         return apply_filters( 'attachment_link', $link, $post->ID );
429 }
430
431 /**
432  * Retrieve the permalink for the year archives.
433  *
434  * @since 1.5.0
435  *
436  * @param int|bool $year False for current year or year for permalink.
437  * @return string
438  */
439 function get_year_link($year) {
440         global $wp_rewrite;
441         if ( !$year )
442                 $year = gmdate('Y', current_time('timestamp'));
443         $yearlink = $wp_rewrite->get_year_permastruct();
444         if ( !empty($yearlink) ) {
445                 $yearlink = str_replace('%year%', $year, $yearlink);
446                 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
447         } else {
448                 $yearlink = home_url( '?m=' . $year );
449         }
450
451         /**
452          * Filter the year archive permalink.
453          *
454          * @since 1.5.0
455          *
456          * @param string $yearlink Permalink for the year archive.
457          * @param int    $year     Year for the archive.
458          */
459         return apply_filters( 'year_link', $yearlink, $year );
460 }
461
462 /**
463  * Retrieve the permalink for the month archives with year.
464  *
465  * @since 1.0.0
466  *
467  * @param bool|int $year False for current year. Integer of year.
468  * @param bool|int $month False for current month. Integer of month.
469  * @return string
470  */
471 function get_month_link($year, $month) {
472         global $wp_rewrite;
473         if ( !$year )
474                 $year = gmdate('Y', current_time('timestamp'));
475         if ( !$month )
476                 $month = gmdate('m', current_time('timestamp'));
477         $monthlink = $wp_rewrite->get_month_permastruct();
478         if ( !empty($monthlink) ) {
479                 $monthlink = str_replace('%year%', $year, $monthlink);
480                 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
481                 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
482         } else {
483                 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
484         }
485
486         /**
487          * Filter the month archive permalink.
488          *
489          * @since 1.5.0
490          *
491          * @param string $monthlink Permalink for the month archive.
492          * @param int    $year      Year for the archive.
493          * @param int    $month     The month for the archive.
494          */
495         return apply_filters( 'month_link', $monthlink, $year, $month );
496 }
497
498 /**
499  * Retrieve the permalink for the day archives with year and month.
500  *
501  * @since 1.0.0
502  *
503  * @param bool|int $year False for current year. Integer of year.
504  * @param bool|int $month False for current month. Integer of month.
505  * @param bool|int $day False for current day. Integer of day.
506  * @return string
507  */
508 function get_day_link($year, $month, $day) {
509         global $wp_rewrite;
510         if ( !$year )
511                 $year = gmdate('Y', current_time('timestamp'));
512         if ( !$month )
513                 $month = gmdate('m', current_time('timestamp'));
514         if ( !$day )
515                 $day = gmdate('j', current_time('timestamp'));
516
517         $daylink = $wp_rewrite->get_day_permastruct();
518         if ( !empty($daylink) ) {
519                 $daylink = str_replace('%year%', $year, $daylink);
520                 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
521                 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
522                 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
523         } else {
524                 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
525         }
526
527         /**
528          * Filter the day archive permalink.
529          *
530          * @since 1.5.0
531          *
532          * @param string $daylink Permalink for the day archive.
533          * @param int    $year    Year for the archive.
534          * @param int    $month   Month for the archive.
535          * @param int    $day     The day for the archive.
536          */
537         return apply_filters( 'day_link', $daylink, $year, $month, $day );
538 }
539
540 /**
541  * Display the permalink for the feed type.
542  *
543  * @since 3.0.0
544  *
545  * @param string $anchor The link's anchor text.
546  * @param string $feed Optional, defaults to default feed. Feed type.
547  */
548 function the_feed_link( $anchor, $feed = '' ) {
549         $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
550
551         /**
552          * Filter the feed link anchor tag.
553          *
554          * @since 3.0.0
555          *
556          * @param string $link The complete anchor tag for a feed link.
557          * @param string $feed The feed type, or an empty string for the
558          *                     default feed type.
559          */
560         echo apply_filters( 'the_feed_link', $link, $feed );
561 }
562
563 /**
564  * Retrieve the permalink for the feed type.
565  *
566  * @since 1.5.0
567  *
568  * @param string $feed Optional, defaults to default feed. Feed type.
569  * @return string
570  */
571 function get_feed_link($feed = '') {
572         global $wp_rewrite;
573
574         $permalink = $wp_rewrite->get_feed_permastruct();
575         if ( '' != $permalink ) {
576                 if ( false !== strpos($feed, 'comments_') ) {
577                         $feed = str_replace('comments_', '', $feed);
578                         $permalink = $wp_rewrite->get_comment_feed_permastruct();
579                 }
580
581                 if ( get_default_feed() == $feed )
582                         $feed = '';
583
584                 $permalink = str_replace('%feed%', $feed, $permalink);
585                 $permalink = preg_replace('#/+#', '/', "/$permalink");
586                 $output =  home_url( user_trailingslashit($permalink, 'feed') );
587         } else {
588                 if ( empty($feed) )
589                         $feed = get_default_feed();
590
591                 if ( false !== strpos($feed, 'comments_') )
592                         $feed = str_replace('comments_', 'comments-', $feed);
593
594                 $output = home_url("?feed={$feed}");
595         }
596
597         /**
598          * Filter the feed type permalink.
599          *
600          * @since 1.5.0
601          *
602          * @param string $output The feed permalink.
603          * @param string $feed   Feed type.
604          */
605         return apply_filters( 'feed_link', $output, $feed );
606 }
607
608 /**
609  * Retrieve the permalink for the post comments feed.
610  *
611  * @since 2.2.0
612  *
613  * @param int $post_id Optional. Post ID.
614  * @param string $feed Optional. Feed type.
615  * @return string
616  */
617 function get_post_comments_feed_link($post_id = 0, $feed = '') {
618         $post_id = absint( $post_id );
619
620         if ( ! $post_id )
621                 $post_id = get_the_ID();
622
623         if ( empty( $feed ) )
624                 $feed = get_default_feed();
625
626         if ( '' != get_option('permalink_structure') ) {
627                 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
628                         $url = _get_page_link( $post_id );
629                 else
630                         $url = get_permalink($post_id);
631
632                 $url = trailingslashit($url) . 'feed';
633                 if ( $feed != get_default_feed() )
634                         $url .= "/$feed";
635                 $url = user_trailingslashit($url, 'single_feed');
636         } else {
637                 $type = get_post_field('post_type', $post_id);
638                 if ( 'page' == $type )
639                         $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
640                 else
641                         $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
642         }
643
644         /**
645          * Filter the post comments feed permalink.
646          *
647          * @since 1.5.1
648          *
649          * @param string $url Post comments feed permalink.
650          */
651         return apply_filters( 'post_comments_feed_link', $url );
652 }
653
654 /**
655  * Display the comment feed link for a post.
656  *
657  * Prints out the comment feed link for a post. Link text is placed in the
658  * anchor. If no link text is specified, default text is used. If no post ID is
659  * specified, the current post is used.
660  *
661  * @since 2.5.0
662  *
663  * @param string $link_text Descriptive text.
664  * @param int $post_id Optional post ID. Default to current post.
665  * @param string $feed Optional. Feed format.
666  * @return string Link to the comment feed for the current post.
667 */
668 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
669         $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
670         if ( empty($link_text) )
671                 $link_text = __('Comments Feed');
672
673         /**
674          * Filter the post comment feed link anchor tag.
675          *
676          * @since 2.8.0
677          *
678          * @param string $link    The complete anchor tag for the comment feed link.
679          * @param int    $post_id Post ID.
680          * @param string $feed    The feed type, or an empty string for the default feed type.
681          */
682         echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
683 }
684
685 /**
686  * Retrieve the feed link for a given author.
687  *
688  * Returns a link to the feed for all posts by a given author. A specific feed
689  * can be requested or left blank to get the default feed.
690  *
691  * @since 2.5.0
692  *
693  * @param int $author_id ID of an author.
694  * @param string $feed Optional. Feed type.
695  * @return string Link to the feed for the author specified by $author_id.
696 */
697 function get_author_feed_link( $author_id, $feed = '' ) {
698         $author_id = (int) $author_id;
699         $permalink_structure = get_option('permalink_structure');
700
701         if ( empty($feed) )
702                 $feed = get_default_feed();
703
704         if ( '' == $permalink_structure ) {
705                 $link = home_url("?feed=$feed&amp;author=" . $author_id);
706         } else {
707                 $link = get_author_posts_url($author_id);
708                 if ( $feed == get_default_feed() )
709                         $feed_link = 'feed';
710                 else
711                         $feed_link = "feed/$feed";
712
713                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
714         }
715
716         /**
717          * Filter the feed link for a given author.
718          *
719          * @since 1.5.1
720          *
721          * @param string $link The author feed link.
722          * @param string $feed Feed type.
723          */
724         $link = apply_filters( 'author_feed_link', $link, $feed );
725
726         return $link;
727 }
728
729 /**
730  * Retrieve the feed link for a category.
731  *
732  * Returns a link to the feed for all posts in a given category. A specific feed
733  * can be requested or left blank to get the default feed.
734  *
735  * @since 2.5.0
736  *
737  * @param int $cat_id ID of a category.
738  * @param string $feed Optional. Feed type.
739  * @return string Link to the feed for the category specified by $cat_id.
740 */
741 function get_category_feed_link($cat_id, $feed = '') {
742         return get_term_feed_link($cat_id, 'category', $feed);
743 }
744
745 /**
746  * Retrieve the feed link for a term.
747  *
748  * Returns a link to the feed for all posts in a given term. A specific feed
749  * can be requested or left blank to get the default feed.
750  *
751  * @since 3.0.0
752  *
753  * @param int $term_id ID of a category.
754  * @param string $taxonomy Optional. Taxonomy of $term_id
755  * @param string $feed Optional. Feed type.
756  * @return string Link to the feed for the term specified by $term_id and $taxonomy.
757 */
758 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
759         $term_id = ( int ) $term_id;
760
761         $term = get_term( $term_id, $taxonomy  );
762
763         if ( empty( $term ) || is_wp_error( $term ) )
764                 return false;
765
766         if ( empty( $feed ) )
767                 $feed = get_default_feed();
768
769         $permalink_structure = get_option( 'permalink_structure' );
770
771         if ( '' == $permalink_structure ) {
772                 if ( 'category' == $taxonomy ) {
773                         $link = home_url("?feed=$feed&amp;cat=$term_id");
774                 }
775                 elseif ( 'post_tag' == $taxonomy ) {
776                         $link = home_url("?feed=$feed&amp;tag=$term->slug");
777                 } else {
778                         $t = get_taxonomy( $taxonomy );
779                         $link = home_url("?feed=$feed&amp;$t->query_var=$term->slug");
780                 }
781         } else {
782                 $link = get_term_link( $term_id, $term->taxonomy );
783                 if ( $feed == get_default_feed() )
784                         $feed_link = 'feed';
785                 else
786                         $feed_link = "feed/$feed";
787
788                 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
789         }
790
791         if ( 'category' == $taxonomy ) {
792                 /**
793                  * Filter the category feed link.
794                  *
795                  * @since 1.5.1
796                  *
797                  * @param string $link The category feed link.
798                  * @param string $feed Feed type.
799                  */
800                 $link = apply_filters( 'category_feed_link', $link, $feed );
801         } elseif ( 'post_tag' == $taxonomy ) {
802                 /**
803                  * Filter the post tag feed link.
804                  *
805                  * @since 2.3.0
806                  *
807                  * @param string $link The tag feed link.
808                  * @param string $feed Feed type.
809                  */
810                 $link = apply_filters( 'tag_feed_link', $link, $feed );
811         } else {
812                 /**
813                  * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
814                  *
815                  * @since 3.0.0
816                  *
817                  * @param string $link The taxonomy feed link.
818                  * @param string $feed Feed type.
819                  * @param string $feed The taxonomy name.
820                  */
821                 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
822         }
823
824         return $link;
825 }
826
827 /**
828  * Retrieve permalink for feed of tag.
829  *
830  * @since 2.3.0
831  *
832  * @param int $tag_id Tag ID.
833  * @param string $feed Optional. Feed type.
834  * @return string
835  */
836 function get_tag_feed_link($tag_id, $feed = '') {
837         return get_term_feed_link($tag_id, 'post_tag', $feed);
838 }
839
840 /**
841  * Retrieve edit tag link.
842  *
843  * @since 2.7.0
844  *
845  * @param int $tag_id Tag ID
846  * @param string $taxonomy Taxonomy
847  * @return string
848  */
849 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
850         /**
851          * Filter the edit link for a tag (or term in another taxonomy).
852          *
853          * @since 2.7.0
854          *
855          * @param string $link The term edit link.
856          */
857         return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
858 }
859
860 /**
861  * Display or retrieve edit tag link with formatting.
862  *
863  * @since 2.7.0
864  *
865  * @param string $link Optional. Anchor text.
866  * @param string $before Optional. Display before edit link.
867  * @param string $after Optional. Display after edit link.
868  * @param object $tag Tag object.
869  * @return string HTML content.
870  */
871 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
872         $link = edit_term_link( $link, '', '', $tag, false );
873
874         /**
875          * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
876          *
877          * @since 2.7.0
878          *
879          * @param string $link The anchor tag for the edit link.
880          */
881         echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
882 }
883
884 /**
885  * Retrieve edit term url.
886  *
887  * @since 3.1.0
888  *
889  * @param int $term_id Term ID
890  * @param string $taxonomy Taxonomy
891  * @param string $object_type The object type
892  * @return string
893  */
894 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
895         $tax = get_taxonomy( $taxonomy );
896         if ( !current_user_can( $tax->cap->edit_terms ) )
897                 return;
898
899         $term = get_term( $term_id, $taxonomy );
900
901         $args = array(
902                 'action' => 'edit',
903                 'taxonomy' => $taxonomy,
904                 'tag_ID' => $term->term_id,
905         );
906
907         if ( $object_type )
908                 $args['post_type'] = $object_type;
909
910         $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
911
912         /**
913          * Filter the edit link for a term.
914          *
915          * @since 3.1.0
916          *
917          * @param string $location    The edit link.
918          * @param int    $term_id     Term ID.
919          * @param string $taxonomy    Taxonomy name.
920          * @param string $object_type The object type (eg. the post type).
921          */
922         return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
923 }
924
925 /**
926  * Display or retrieve edit term link with formatting.
927  *
928  * @since 3.1.0
929  *
930  * @param string $link Optional. Anchor text.
931  * @param string $before Optional. Display before edit link.
932  * @param string $after Optional. Display after edit link.
933  * @param object $term Term object.
934  * @return string HTML content.
935  */
936 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
937         if ( is_null( $term ) )
938                 $term = get_queried_object();
939
940         if ( ! $term )
941                 return;
942
943         $tax = get_taxonomy( $term->taxonomy );
944         if ( ! current_user_can( $tax->cap->edit_terms ) )
945                 return;
946
947         if ( empty( $link ) )
948                 $link = __('Edit This');
949
950         $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
951
952         /**
953          * Filter the anchor tag for the edit link of a term.
954          *
955          * @since 3.1.0
956          *
957          * @param string $link    The anchor tag for the edit link.
958          * @param int    $term_id Term ID.
959          */
960         $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
961
962         if ( $echo )
963                 echo $link;
964         else
965                 return $link;
966 }
967
968 /**
969  * Retrieve permalink for search.
970  *
971  * @since  3.0.0
972  *
973  * @param string $query Optional. The query string to use. If empty the current query is used.
974  * @return string
975  */
976 function get_search_link( $query = '' ) {
977         global $wp_rewrite;
978
979         if ( empty($query) )
980                 $search = get_search_query( false );
981         else
982                 $search = stripslashes($query);
983
984         $permastruct = $wp_rewrite->get_search_permastruct();
985
986         if ( empty( $permastruct ) ) {
987                 $link = home_url('?s=' . urlencode($search) );
988         } else {
989                 $search = urlencode($search);
990                 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
991                 $link = str_replace( '%search%', $search, $permastruct );
992                 $link = home_url( user_trailingslashit( $link, 'search' ) );
993         }
994
995         /**
996          * Filter the search permalink.
997          *
998          * @since 3.0.0
999          *
1000          * @param string $link   Search permalink.
1001          * @param string $search The URL-encoded search term.
1002          */
1003         return apply_filters( 'search_link', $link, $search );
1004 }
1005
1006 /**
1007  * Retrieve the permalink for the feed of the search results.
1008  *
1009  * @since 2.5.0
1010  *
1011  * @param string $search_query Optional. Search query.
1012  * @param string $feed Optional. Feed type.
1013  * @return string
1014  */
1015 function get_search_feed_link($search_query = '', $feed = '') {
1016         global $wp_rewrite;
1017         $link = get_search_link($search_query);
1018
1019         if ( empty($feed) )
1020                 $feed = get_default_feed();
1021
1022         $permastruct = $wp_rewrite->get_search_permastruct();
1023
1024         if ( empty($permastruct) ) {
1025                 $link = add_query_arg('feed', $feed, $link);
1026         } else {
1027                 $link = trailingslashit($link);
1028                 $link .= "feed/$feed/";
1029         }
1030
1031         /**
1032          * Filter the search feed link.
1033          *
1034          * @since 2.5.0
1035          *
1036          * @param string $link Search feed link.
1037          * @param string $feed Feed type.
1038          * @param string $type The search type. One of 'posts' or 'comments'.
1039          */
1040         $link = apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1041
1042         return $link;
1043 }
1044
1045 /**
1046  * Retrieve the permalink for the comments feed of the search results.
1047  *
1048  * @since 2.5.0
1049  *
1050  * @param string $search_query Optional. Search query.
1051  * @param string $feed Optional. Feed type.
1052  * @return string
1053  */
1054 function get_search_comments_feed_link($search_query = '', $feed = '') {
1055         global $wp_rewrite;
1056
1057         if ( empty($feed) )
1058                 $feed = get_default_feed();
1059
1060         $link = get_search_feed_link($search_query, $feed);
1061
1062         $permastruct = $wp_rewrite->get_search_permastruct();
1063
1064         if ( empty($permastruct) )
1065                 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1066         else
1067                 $link = add_query_arg('withcomments', 1, $link);
1068
1069         /** This filter is documented in wp-includes/link-template.php */
1070         $link = apply_filters('search_feed_link', $link, $feed, 'comments');
1071
1072         return $link;
1073 }
1074
1075 /**
1076  * Retrieve the permalink for a post type archive.
1077  *
1078  * @since 3.1.0
1079  *
1080  * @param string $post_type Post type
1081  * @return string
1082  */
1083 function get_post_type_archive_link( $post_type ) {
1084         global $wp_rewrite;
1085         if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1086                 return false;
1087
1088         if ( ! $post_type_obj->has_archive )
1089                 return false;
1090
1091         if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1092                 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1093                 if ( $post_type_obj->rewrite['with_front'] )
1094                         $struct = $wp_rewrite->front . $struct;
1095                 else
1096                         $struct = $wp_rewrite->root . $struct;
1097                 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1098         } else {
1099                 $link = home_url( '?post_type=' . $post_type );
1100         }
1101
1102         /**
1103          * Filter the post type archive permalink.
1104          *
1105          * @since 3.1.0
1106          *
1107          * @param string $link      The post type archive permalink.
1108          * @param string $post_type Post type name.
1109          */
1110         return apply_filters( 'post_type_archive_link', $link, $post_type );
1111 }
1112
1113 /**
1114  * Retrieve the permalink for a post type archive feed.
1115  *
1116  * @since 3.1.0
1117  *
1118  * @param string $post_type Post type
1119  * @param string $feed Optional. Feed type
1120  * @return string
1121  */
1122 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1123         $default_feed = get_default_feed();
1124         if ( empty( $feed ) )
1125                 $feed = $default_feed;
1126
1127         if ( ! $link = get_post_type_archive_link( $post_type ) )
1128                 return false;
1129
1130         $post_type_obj = get_post_type_object( $post_type );
1131         if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1132                 $link = trailingslashit( $link );
1133                 $link .= 'feed/';
1134                 if ( $feed != $default_feed )
1135                         $link .= "$feed/";
1136         } else {
1137                 $link = add_query_arg( 'feed', $feed, $link );
1138         }
1139
1140         /**
1141          * Filter the post type archive feed link.
1142          *
1143          * @since 3.1.0
1144          *
1145          * @param string $link The post type archive feed link.
1146          * @param string $feed Feed type.
1147          */
1148         return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1149 }
1150
1151 /**
1152  * Retrieve edit posts link for post.
1153  *
1154  * Can be used within the WordPress loop or outside of it. Can be used with
1155  * pages, posts, attachments, and revisions.
1156  *
1157  * @since 2.3.0
1158  *
1159  * @param int $id Optional. Post ID.
1160  * @param string $context Optional, defaults to display. How to write the '&', defaults to '&amp;'.
1161  * @return string
1162  */
1163 function get_edit_post_link( $id = 0, $context = 'display' ) {
1164         if ( ! $post = get_post( $id ) )
1165                 return;
1166
1167         if ( 'revision' === $post->post_type )
1168                 $action = '';
1169         elseif ( 'display' == $context )
1170                 $action = '&amp;action=edit';
1171         else
1172                 $action = '&action=edit';
1173
1174         $post_type_object = get_post_type_object( $post->post_type );
1175         if ( !$post_type_object )
1176                 return;
1177
1178         if ( !current_user_can( 'edit_post', $post->ID ) )
1179                 return;
1180
1181         /**
1182          * Filter the post edit link.
1183          *
1184          * @since 2.3.0
1185          *
1186          * @param string $link    The edit link.
1187          * @param int    $post_id Post ID.
1188          * @param string $context The link context. If set to 'display' then ampersands
1189          *                        are encoded.
1190          */
1191         return apply_filters( 'get_edit_post_link', admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ), $post->ID, $context );
1192 }
1193
1194 /**
1195  * Display edit post link for post.
1196  *
1197  * @since 1.0.0
1198  *
1199  * @param string $link Optional. Anchor text.
1200  * @param string $before Optional. Display before edit link.
1201  * @param string $after Optional. Display after edit link.
1202  * @param int $id Optional. Post ID.
1203  */
1204 function edit_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
1205         if ( !$post = get_post( $id ) )
1206                 return;
1207
1208         if ( !$url = get_edit_post_link( $post->ID ) )
1209                 return;
1210
1211         if ( null === $link )
1212                 $link = __('Edit This');
1213
1214         $post_type_obj = get_post_type_object( $post->post_type );
1215         $link = '<a class="post-edit-link" href="' . $url . '">' . $link . '</a>';
1216
1217         /**
1218          * Filter the post edit link anchor tag.
1219          *
1220          * @since 2.3.0
1221          *
1222          * @param string $link    Anchor tag for the edit link.
1223          * @param int    $post_id Post ID.
1224          */
1225         echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
1226 }
1227
1228 /**
1229  * Retrieve delete posts link for post.
1230  *
1231  * Can be used within the WordPress loop or outside of it, with any post type.
1232  *
1233  * @since 2.9.0
1234  *
1235  * @param int $id Optional. Post ID.
1236  * @param string $deprecated Not used.
1237  * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1238  * @return string
1239  */
1240 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1241         if ( ! empty( $deprecated ) )
1242                 _deprecated_argument( __FUNCTION__, '3.0' );
1243
1244         if ( !$post = get_post( $id ) )
1245                 return;
1246
1247         $post_type_object = get_post_type_object( $post->post_type );
1248         if ( !$post_type_object )
1249                 return;
1250
1251         if ( !current_user_can( 'delete_post', $post->ID ) )
1252                 return;
1253
1254         $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1255
1256         $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1257
1258         /**
1259          * Filter the post delete link.
1260          *
1261          * @since 2.9.0
1262          *
1263          * @param string $link         The delete link.
1264          * @param int    $post_id      Post ID.
1265          * @param bool   $force_delete Whether to bypass the trash and force deletion. Default false.
1266          */
1267         return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1268 }
1269
1270 /**
1271  * Retrieve edit comment link.
1272  *
1273  * @since 2.3.0
1274  *
1275  * @param int $comment_id Optional. Comment ID.
1276  * @return string
1277  */
1278 function get_edit_comment_link( $comment_id = 0 ) {
1279         $comment = get_comment( $comment_id );
1280
1281         if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1282                 return;
1283
1284         $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
1285
1286         /**
1287          * Filter the comment edit link.
1288          *
1289          * @since 2.3.0
1290          *
1291          * @param string $location The edit link.
1292          */
1293         return apply_filters( 'get_edit_comment_link', $location );
1294 }
1295
1296 /**
1297  * Display edit comment link with formatting.
1298  *
1299  * @since 1.0.0
1300  *
1301  * @param string $link Optional. Anchor text.
1302  * @param string $before Optional. Display before edit link.
1303  * @param string $after Optional. Display after edit link.
1304  */
1305 function edit_comment_link( $link = null, $before = '', $after = '' ) {
1306         global $comment;
1307
1308         if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1309                 return;
1310
1311         if ( null === $link )
1312                 $link = __('Edit This');
1313
1314         $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $link . '</a>';
1315
1316         /**
1317          * Filter the comment edit link anchor tag.
1318          *
1319          * @since 2.3.0
1320          *
1321          * @param string $link       Anchor tag for the edit link.
1322          * @param int    $comment_id Comment ID.
1323          */
1324         echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
1325 }
1326
1327 /**
1328  * Display edit bookmark (literally a URL external to blog) link.
1329  *
1330  * @since 2.7.0
1331  *
1332  * @param int $link Optional. Bookmark ID.
1333  * @return string
1334  */
1335 function get_edit_bookmark_link( $link = 0 ) {
1336         $link = get_bookmark( $link );
1337
1338         if ( !current_user_can('manage_links') )
1339                 return;
1340
1341         $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
1342
1343         /**
1344          * Filter the bookmark (link) edit link.
1345          *
1346          * @since 2.7.0
1347          *
1348          * @param string $location The edit link.
1349          * @param int    $link_id  Bookmark ID.
1350          */
1351         return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1352 }
1353
1354 /**
1355  * Display edit bookmark (literally a URL external to blog) link anchor content.
1356  *
1357  * @since 2.7.0
1358  *
1359  * @param string $link Optional. Anchor text.
1360  * @param string $before Optional. Display before edit link.
1361  * @param string $after Optional. Display after edit link.
1362  * @param int $bookmark Optional. Bookmark ID.
1363  */
1364 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1365         $bookmark = get_bookmark($bookmark);
1366
1367         if ( !current_user_can('manage_links') )
1368                 return;
1369
1370         if ( empty($link) )
1371                 $link = __('Edit This');
1372
1373         $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
1374
1375         /**
1376          * Filter the bookmark edit link anchor tag.
1377          *
1378          * @since 2.7.0
1379          *
1380          * @param string $link    Anchor tag for the edit link.
1381          * @param int    $link_id Bookmark ID.
1382          */
1383         echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1384 }
1385
1386 /**
1387  * Retrieve edit user link
1388  *
1389  * @since 3.5.0
1390  *
1391  * @param int $user_id Optional. User ID. Defaults to the current user.
1392  * @return string URL to edit user page or empty string.
1393  */
1394 function get_edit_user_link( $user_id = null ) {
1395         if ( ! $user_id )
1396                 $user_id = get_current_user_id();
1397
1398         if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1399                 return '';
1400
1401         $user = get_userdata( $user_id );
1402
1403         if ( ! $user )
1404                 return '';
1405
1406         if ( get_current_user_id() == $user->ID )
1407                 $link = get_edit_profile_url( $user->ID );
1408         else
1409                 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1410
1411         /**
1412          * Filter the user edit link.
1413          *
1414          * @since 3.5.0
1415          *
1416          * @param string $link    The edit link.
1417          * @param int    $user_id User ID.
1418          */
1419         return apply_filters( 'get_edit_user_link', $link, $user->ID );
1420 }
1421
1422 // Navigation links
1423
1424 /**
1425  * Retrieve previous post that is adjacent to current post.
1426  *
1427  * @since 1.5.0
1428  *
1429  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1430  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1431  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1432  * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1433  */
1434 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1435         return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1436 }
1437
1438 /**
1439  * Retrieve next post that is adjacent to current post.
1440  *
1441  * @since 1.5.0
1442  *
1443  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1444  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1445  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1446  * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1447  */
1448 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1449         return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1450 }
1451
1452 /**
1453  * Retrieve adjacent post.
1454  *
1455  * Can either be next or previous post.
1456  *
1457  * @since 2.5.0
1458  *
1459  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1460  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1461  * @param bool         $previous       Optional. Whether to retrieve previous post.
1462  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1463  * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1464  */
1465 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1466         global $wpdb;
1467
1468         if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1469                 return null;
1470
1471         $current_post_date = $post->post_date;
1472
1473         $join = '';
1474         $posts_in_ex_terms_sql = '';
1475         if ( $in_same_term || ! empty( $excluded_terms ) ) {
1476                 $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";
1477
1478                 if ( $in_same_term ) {
1479                         if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1480                                 return '';
1481                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1482                         if ( ! $term_array || is_wp_error( $term_array ) )
1483                                 return '';
1484                         $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
1485                 }
1486
1487                 $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1488                 if ( ! empty( $excluded_terms ) ) {
1489                         if ( ! is_array( $excluded_terms ) ) {
1490                                 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1491                                 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1492                                         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1493                                         $excluded_terms = explode( ' and ', $excluded_terms );
1494                                 } else {
1495                                         $excluded_terms = explode( ',', $excluded_terms );
1496                                 }
1497                         }
1498
1499                         $excluded_terms = array_map( 'intval', $excluded_terms );
1500
1501                         if ( ! empty( $term_array ) ) {
1502                                 $excluded_terms = array_diff( $excluded_terms, $term_array );
1503                                 $posts_in_ex_terms_sql = '';
1504                         }
1505
1506                         if ( ! empty( $excluded_terms ) ) {
1507                                 $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
1508                         }
1509                 }
1510         }
1511
1512         $adjacent = $previous ? 'previous' : 'next';
1513         $op = $previous ? '<' : '>';
1514         $order = $previous ? 'DESC' : 'ASC';
1515
1516         /**
1517          * Filter the JOIN clause in the SQL for an adjacent post query.
1518          *
1519          * The dynamic portion of the hook name, $adjacent, refers to the type
1520          * of adjacency, 'next' or 'previous'.
1521          *
1522          * @since 2.5.0
1523          *
1524          * @param string $join           The JOIN clause in the SQL.
1525          * @param bool   $in_same_term   Whether post should be in a same taxonomy term.
1526          * @param array  $excluded_terms Array of excluded term IDs.
1527          */
1528         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
1529
1530         /**
1531          * Filter the WHERE clause in the SQL for an adjacent post query.
1532          *
1533          * The dynamic portion of the hook name, $adjacent, refers to the type
1534          * of adjacency, 'next' or 'previous'.
1535          *
1536          * @since 2.5.0
1537          *
1538          * @param string $where          The WHERE clause in the SQL.
1539          * @param bool   $in_same_term   Whether post should be in a same taxonomy term.
1540          * @param array  $excluded_terms Array of excluded term IDs.
1541          */
1542         $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_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms );
1543
1544         /**
1545          * Filter the ORDER BY clause in the SQL for an adjacent post query.
1546          *
1547          * The dynamic portion of the hook name, $adjacent, refers to the type
1548          * of adjacency, 'next' or 'previous'.
1549          *
1550          * @since 2.5.0
1551          *
1552          * @param string $order_by The ORDER BY clause in the SQL.
1553          */
1554         $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1555
1556         $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1557         $query_key = 'adjacent_post_' . md5( $query );
1558         $result = wp_cache_get( $query_key, 'counts' );
1559         if ( false !== $result ) {
1560                 if ( $result )
1561                         $result = get_post( $result );
1562                 return $result;
1563         }
1564
1565         $result = $wpdb->get_var( $query );
1566         if ( null === $result )
1567                 $result = '';
1568
1569         wp_cache_set( $query_key, $result, 'counts' );
1570
1571         if ( $result )
1572                 $result = get_post( $result );
1573
1574         return $result;
1575 }
1576
1577 /**
1578  * Get adjacent post relational link.
1579  *
1580  * Can either be next or previous post relational link.
1581  *
1582  * @since 2.8.0
1583  *
1584  * @param string       $title          Optional. Link title format.
1585  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1586  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1587  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1588  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1589  * @return string
1590  */
1591 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1592         if ( $previous && is_attachment() && $post = get_post() )
1593                 $post = get_post( $post->post_parent );
1594         else
1595                 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1596
1597         if ( empty( $post ) )
1598                 return;
1599
1600         $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1601
1602         if ( empty( $post_title ) )
1603                 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1604
1605         $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1606
1607         $title = str_replace( '%title', $post_title, $title );
1608         $title = str_replace( '%date', $date, $title );
1609
1610         $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1611         $link .= esc_attr( $title );
1612         $link .= "' href='" . get_permalink( $post ) . "' />\n";
1613
1614         $adjacent = $previous ? 'previous' : 'next';
1615
1616         /**
1617          * Filter the adjacent post relational link.
1618          *
1619          * The dynamic portion of the hook name, $adjacent, refers to the type
1620          * of adjacency, 'next' or 'previous'.
1621          *
1622          * @since 2.8.0
1623          *
1624          * @param string $link The relational link.
1625          */
1626         return apply_filters( "{$adjacent}_post_rel_link", $link );
1627 }
1628
1629 /**
1630  * Display relational links for the posts adjacent to the current post.
1631  *
1632  * @since 2.8.0
1633  *
1634  * @param string       $title          Optional. Link title format.
1635  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1636  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1637  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1638  */
1639 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1640         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1641         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1642 }
1643
1644 /**
1645  * Display relational links for the posts adjacent to the current post for single post pages.
1646  *
1647  * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1648  * @since 3.0.0
1649  *
1650  */
1651 function adjacent_posts_rel_link_wp_head() {
1652         if ( !is_singular() || is_attachment() )
1653                 return;
1654         adjacent_posts_rel_link();
1655 }
1656
1657 /**
1658  * Display relational link for the next post adjacent to the current post.
1659  *
1660  * @since 2.8.0
1661  *
1662  * @param string       $title          Optional. Link title format.
1663  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1664  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1665  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1666  */
1667 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1668         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1669 }
1670
1671 /**
1672  * Display relational link for the previous post adjacent to the current post.
1673  *
1674  * @since 2.8.0
1675  *
1676  * @param string       $title          Optional. Link title format.
1677  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1678  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1679  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1680  */
1681 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1682         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1683 }
1684
1685 /**
1686  * Retrieve boundary post.
1687  *
1688  * Boundary being either the first or last post by publish date within the constraints specified
1689  * by $in_same_term or $excluded_terms.
1690  *
1691  * @since 2.8.0
1692  *
1693  * @param bool         $in_same_term   Optional. Whether returned post should be in a same taxonomy term.
1694  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1695  * @param bool         $start          Optional. Whether to retrieve first or last post.
1696  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1697  * @return mixed Array containing the boundary post object if successful, null otherwise.
1698  */
1699 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1700         $post = get_post();
1701         if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1702                 return null;
1703
1704         $query_args = array(
1705                 'posts_per_page' => 1,
1706                 'order' => $start ? 'ASC' : 'DESC',
1707                 'update_post_term_cache' => false,
1708                 'update_post_meta_cache' => false
1709         );
1710
1711         $term_array = array();
1712
1713         if ( ! is_array( $excluded_terms ) ) {
1714                 if ( ! empty( $excluded_terms ) )
1715                         $excluded_terms = explode( ',', $excluded_terms );
1716                 else
1717                         $excluded_terms = array();
1718         }
1719
1720         if ( $in_same_term || ! empty( $excluded_terms ) ) {
1721                 if ( $in_same_term )
1722                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1723
1724                 if ( ! empty( $excluded_terms ) ) {
1725                         $excluded_terms = array_map( 'intval', $excluded_terms );
1726                         $excluded_terms = array_diff( $excluded_terms, $term_array );
1727
1728                         $inverse_terms = array();
1729                         foreach ( $excluded_terms as $excluded_term )
1730                                 $inverse_terms[] = $excluded_term * -1;
1731                         $excluded_terms = $inverse_terms;
1732                 }
1733
1734                 $query_args[ 'tax_query' ] = array( array(
1735                         'taxonomy' => $taxonomy,
1736                         'terms' => array_merge( $term_array, $excluded_terms )
1737                 ) );
1738         }
1739
1740         return get_posts( $query_args );
1741 }
1742
1743 /*
1744  * Get previous post link that is adjacent to the current post.
1745  *
1746  * @since 3.7.0
1747  *
1748  * @param string       $format         Optional. Link anchor format.
1749  * @param string       $link           Optional. Link permalink format.
1750  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1751  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1752  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1753  * @return string
1754  */
1755 function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
1756         return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, true, $taxonomy );
1757 }
1758
1759 /**
1760  * Display previous post link that is adjacent to the current post.
1761  *
1762  * @since 1.5.0
1763  * @see get_previous_post_link()
1764  *
1765  * @param string       $format         Optional. Link anchor format.
1766  * @param string       $link           Optional. Link permalink format.
1767  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1768  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1769  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1770  */
1771 function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
1772         echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
1773 }
1774
1775 /**
1776  * Get next post link that is adjacent to the current post.
1777  *
1778  * @since 3.7.0
1779  *
1780  * @param string       $format         Optional. Link anchor format.
1781  * @param string       $link           Optional. Link permalink format.
1782  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1783  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1784  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1785  * @return string
1786  */
1787 function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
1788         return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, false, $taxonomy );
1789 }
1790
1791 /**
1792  * Display next post link that is adjacent to the current post.
1793  *
1794  * @since 1.5.0
1795  * @see get_next_post_link()
1796  *
1797  * @param string       $format         Optional. Link anchor format.
1798  * @param string       $link           Optional. Link permalink format.
1799  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1800  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1801  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1802  */
1803 function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
1804          echo get_next_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
1805 }
1806
1807 /**
1808  * Get adjacent post link.
1809  *
1810  * Can be either next post link or previous.
1811  *
1812  * @since 3.7.0
1813  *
1814  * @param string       $format         Link anchor format.
1815  * @param string       $link           Link permalink format.
1816  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1817  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1818  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1819  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1820  * @return string
1821  */
1822 function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1823         if ( $previous && is_attachment() )
1824                 $post = get_post( get_post()->post_parent );
1825         else
1826                 $post = get_adjacent_post( $in_same_cat, $excluded_terms, $previous, $taxonomy );
1827
1828         if ( ! $post ) {
1829                 $output = '';
1830         } else {
1831                 $title = $post->post_title;
1832
1833                 if ( empty( $post->post_title ) )
1834                         $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1835
1836                 /** This filter is documented in wp-includes/post-template.php */
1837                 $title = apply_filters( 'the_title', $title, $post->ID );
1838
1839                 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1840                 $rel = $previous ? 'prev' : 'next';
1841
1842                 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1843                 $inlink = str_replace( '%title', $title, $link );
1844                 $inlink = str_replace( '%date', $date, $inlink );
1845                 $inlink = $string . $inlink . '</a>';
1846
1847                 $output = str_replace( '%link', $inlink, $format );
1848         }
1849
1850         $adjacent = $previous ? 'previous' : 'next';
1851
1852         /**
1853          * Filter the adjacent post link.
1854          *
1855          * The dynamic portion of the hook name, $adjacent, refers to the type
1856          * of adjacency, 'next' or 'previous'.
1857          *
1858          * @since 2.6.0
1859          *
1860          * @param string  $output The adjacent post link.
1861          * @param string  $format Link anchor format.
1862          * @param string  $link   Link permalink format.
1863          * @param WP_Post $post   The adjacent post.
1864          */
1865         return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
1866 }
1867
1868 /**
1869  * Display adjacent post link.
1870  *
1871  * Can be either next post link or previous.
1872  *
1873  * @since 2.5.0
1874  * @uses get_adjacent_post_link()
1875  *
1876  * @param string       $format         Link anchor format.
1877  * @param string       $link           Link permalink format.
1878  * @param bool         $in_same_cat    Optional. Whether link should be in a same category.
1879  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
1880  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1881  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1882  * @return string
1883  */
1884 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1885         echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, $previous, $taxonomy );
1886 }
1887
1888 /**
1889  * Retrieve links for page numbers.
1890  *
1891  * @since 1.5.0
1892  *
1893  * @param int $pagenum Optional. Page ID.
1894  * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
1895 *       Otherwise, prepares the URL with esc_url_raw().
1896  * @return string
1897  */
1898 function get_pagenum_link($pagenum = 1, $escape = true ) {
1899         global $wp_rewrite;
1900
1901         $pagenum = (int) $pagenum;
1902
1903         $request = remove_query_arg( 'paged' );
1904
1905         $home_root = parse_url(home_url());
1906         $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1907         $home_root = preg_quote( $home_root, '|' );
1908
1909         $request = preg_replace('|^'. $home_root . '|i', '', $request);
1910         $request = preg_replace('|^/+|', '', $request);
1911
1912         if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1913                 $base = trailingslashit( get_bloginfo( 'url' ) );
1914
1915                 if ( $pagenum > 1 ) {
1916                         $result = add_query_arg( 'paged', $pagenum, $base . $request );
1917                 } else {
1918                         $result = $base . $request;
1919                 }
1920         } else {
1921                 $qs_regex = '|\?.*?$|';
1922                 preg_match( $qs_regex, $request, $qs_match );
1923
1924                 if ( !empty( $qs_match[0] ) ) {
1925                         $query_string = $qs_match[0];
1926                         $request = preg_replace( $qs_regex, '', $request );
1927                 } else {
1928                         $query_string = '';
1929                 }
1930
1931                 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
1932                 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
1933                 $request = ltrim($request, '/');
1934
1935                 $base = trailingslashit( get_bloginfo( 'url' ) );
1936
1937                 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1938                         $base .= $wp_rewrite->index . '/';
1939
1940                 if ( $pagenum > 1 ) {
1941                         $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
1942                 }
1943
1944                 $result = $base . $request . $query_string;
1945         }
1946
1947         /**
1948          * Filter the page number link for the current request.
1949          *
1950          * @since 2.5.0
1951          *
1952          * @param string $result The page number link.
1953          */
1954         $result = apply_filters( 'get_pagenum_link', $result );
1955
1956         if ( $escape )
1957                 return esc_url( $result );
1958         else
1959                 return esc_url_raw( $result );
1960 }
1961
1962 /**
1963  * Retrieve next posts page link.
1964  *
1965  * Backported from 2.1.3 to 2.0.10.
1966  *
1967  * @since 2.0.10
1968  *
1969  * @param int $max_page Optional. Max pages.
1970  * @return string
1971  */
1972 function get_next_posts_page_link($max_page = 0) {
1973         global $paged;
1974
1975         if ( !is_single() ) {
1976                 if ( !$paged )
1977                         $paged = 1;
1978                 $nextpage = intval($paged) + 1;
1979                 if ( !$max_page || $max_page >= $nextpage )
1980                         return get_pagenum_link($nextpage);
1981         }
1982 }
1983
1984 /**
1985  * Display or return the next posts page link.
1986  *
1987  * @since 0.71
1988  *
1989  * @param int $max_page Optional. Max pages.
1990  * @param boolean $echo Optional. Echo or return;
1991  */
1992 function next_posts( $max_page = 0, $echo = true ) {
1993         $output = esc_url( get_next_posts_page_link( $max_page ) );
1994
1995         if ( $echo )
1996                 echo $output;
1997         else
1998                 return $output;
1999 }
2000
2001 /**
2002  * Return the next posts page link.
2003  *
2004  * @since 2.7.0
2005  *
2006  * @param string $label Content for link text.
2007  * @param int $max_page Optional. Max pages.
2008  * @return string|null
2009  */
2010 function get_next_posts_link( $label = null, $max_page = 0 ) {
2011         global $paged, $wp_query;
2012
2013         if ( !$max_page )
2014                 $max_page = $wp_query->max_num_pages;
2015
2016         if ( !$paged )
2017                 $paged = 1;
2018
2019         $nextpage = intval($paged) + 1;
2020
2021         if ( null === $label )
2022                 $label = __( 'Next Page &raquo;' );
2023
2024         if ( !is_single() && ( $nextpage <= $max_page ) ) {
2025                 /**
2026                  * Filter the anchor tag attributes for the next posts page link.
2027                  *
2028                  * @since 2.7.0
2029                  *
2030                  * @param string $attributes Attributes for the anchor tag.
2031                  */
2032                 $attr = apply_filters( 'next_posts_link_attributes', '' );
2033
2034                 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
2035         }
2036 }
2037
2038 /**
2039  * Display the next posts page link.
2040  *
2041  * @since 0.71
2042  * @uses get_next_posts_link()
2043  *
2044  * @param string $label Content for link text.
2045  * @param int $max_page Optional. Max pages.
2046  */
2047 function next_posts_link( $label = null, $max_page = 0 ) {
2048         echo get_next_posts_link( $label, $max_page );
2049 }
2050
2051 /**
2052  * Retrieve previous posts page link.
2053  *
2054  * Will only return string, if not on a single page or post.
2055  *
2056  * Backported to 2.0.10 from 2.1.3.
2057  *
2058  * @since 2.0.10
2059  *
2060  * @return string|null
2061  */
2062 function get_previous_posts_page_link() {
2063         global $paged;
2064
2065         if ( !is_single() ) {
2066                 $nextpage = intval($paged) - 1;
2067                 if ( $nextpage < 1 )
2068                         $nextpage = 1;
2069                 return get_pagenum_link($nextpage);
2070         }
2071 }
2072
2073 /**
2074  * Display or return the previous posts page link.
2075  *
2076  * @since 0.71
2077  *
2078  * @param boolean $echo Optional. Echo or return;
2079  */
2080 function previous_posts( $echo = true ) {
2081         $output = esc_url( get_previous_posts_page_link() );
2082
2083         if ( $echo )
2084                 echo $output;
2085         else
2086                 return $output;
2087 }
2088
2089 /**
2090  * Return the previous posts page link.
2091  *
2092  * @since 2.7.0
2093  *
2094  * @param string $label Optional. Previous page link text.
2095  * @return string|null
2096  */
2097 function get_previous_posts_link( $label = null ) {
2098         global $paged;
2099
2100         if ( null === $label )
2101                 $label = __( '&laquo; Previous Page' );
2102
2103         if ( !is_single() && $paged > 1 ) {
2104                 /**
2105                  * Filter the anchor tag attributes for the previous posts page link.
2106                  *
2107                  * @since 2.7.0
2108                  *
2109                  * @param string $attributes Attributes for the anchor tag.
2110                  */
2111                 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2112                 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
2113         }
2114 }
2115
2116 /**
2117  * Display the previous posts page link.
2118  *
2119  * @since 0.71
2120  * @uses get_previous_posts_link()
2121  *
2122  * @param string $label Optional. Previous page link text.
2123  */
2124 function previous_posts_link( $label = null ) {
2125         echo get_previous_posts_link( $label );
2126 }
2127
2128 /**
2129  * Return post pages link navigation for previous and next pages.
2130  *
2131  * @since 2.8.0
2132  *
2133  * @param string|array $args Optional args.
2134  * @return string The posts link navigation.
2135  */
2136 function get_posts_nav_link( $args = array() ) {
2137         global $wp_query;
2138
2139         $return = '';
2140
2141         if ( !is_singular() ) {
2142                 $defaults = array(
2143                         'sep' => ' &#8212; ',
2144                         'prelabel' => __('&laquo; Previous Page'),
2145                         'nxtlabel' => __('Next Page &raquo;'),
2146                 );
2147                 $args = wp_parse_args( $args, $defaults );
2148
2149                 $max_num_pages = $wp_query->max_num_pages;
2150                 $paged = get_query_var('paged');
2151
2152                 //only have sep if there's both prev and next results
2153                 if ($paged < 2 || $paged >= $max_num_pages) {
2154                         $args['sep'] = '';
2155                 }
2156
2157                 if ( $max_num_pages > 1 ) {
2158                         $return = get_previous_posts_link($args['prelabel']);
2159                         $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
2160                         $return .= get_next_posts_link($args['nxtlabel']);
2161                 }
2162         }
2163         return $return;
2164
2165 }
2166
2167 /**
2168  * Display post pages link navigation for previous and next pages.
2169  *
2170  * @since 0.71
2171  *
2172  * @param string $sep Optional. Separator for posts navigation links.
2173  * @param string $prelabel Optional. Label for previous pages.
2174  * @param string $nxtlabel Optional Label for next pages.
2175  */
2176 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2177         $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2178         echo get_posts_nav_link($args);
2179 }
2180
2181 /**
2182  * Retrieve comments page number link.
2183  *
2184  * @since 2.7.0
2185  *
2186  * @param int $pagenum Optional. Page number.
2187  * @return string
2188  */
2189 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2190         global $wp_rewrite;
2191
2192         $pagenum = (int) $pagenum;
2193
2194         $result = get_permalink();
2195
2196         if ( 'newest' == get_option('default_comments_page') ) {
2197                 if ( $pagenum != $max_page ) {
2198                         if ( $wp_rewrite->using_permalinks() )
2199                                 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
2200                         else
2201                                 $result = add_query_arg( 'cpage', $pagenum, $result );
2202                 }
2203         } elseif ( $pagenum > 1 ) {
2204                 if ( $wp_rewrite->using_permalinks() )
2205                         $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
2206                 else
2207                         $result = add_query_arg( 'cpage', $pagenum, $result );
2208         }
2209
2210         $result .= '#comments';
2211
2212         /**
2213          * Filter the comments page number link for the current request.
2214          *
2215          * @since 2.7.0
2216          *
2217          * @param string $result The comments page number link.
2218          */
2219         $result = apply_filters( 'get_comments_pagenum_link', $result );
2220
2221         return $result;
2222 }
2223
2224 /**
2225  * Return the link to next comments page.
2226  *
2227  * @since 2.7.1
2228  *
2229  * @param string $label Optional. Label for link text.
2230  * @param int $max_page Optional. Max page.
2231  * @return string|null
2232  */
2233 function get_next_comments_link( $label = '', $max_page = 0 ) {
2234         global $wp_query;
2235
2236         if ( !is_singular() || !get_option('page_comments') )
2237                 return;
2238
2239         $page = get_query_var('cpage');
2240
2241         $nextpage = intval($page) + 1;
2242
2243         if ( empty($max_page) )
2244                 $max_page = $wp_query->max_num_comment_pages;
2245
2246         if ( empty($max_page) )
2247                 $max_page = get_comment_pages_count();
2248
2249         if ( $nextpage > $max_page )
2250                 return;
2251
2252         if ( empty($label) )
2253                 $label = __('Newer Comments &raquo;');
2254
2255         /**
2256          * Filter the anchor tag attributes for the next comments page link.
2257          *
2258          * @since 2.7.0
2259          *
2260          * @param string $attributes Attributes for the anchor tag.
2261          */
2262         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', '&#038;$1', $label) .'</a>';
2263 }
2264
2265 /**
2266  * Display the link to next comments page.
2267  *
2268  * @since 2.7.0
2269  *
2270  * @param string $label Optional. Label for link text.
2271  * @param int $max_page Optional. Max page.
2272  */
2273 function next_comments_link( $label = '', $max_page = 0 ) {
2274         echo get_next_comments_link( $label, $max_page );
2275 }
2276
2277 /**
2278  * Return the previous comments page link.
2279  *
2280  * @since 2.7.1
2281  *
2282  * @param string $label Optional. Label for comments link text.
2283  * @return string|null
2284  */
2285 function get_previous_comments_link( $label = '' ) {
2286         if ( !is_singular() || !get_option('page_comments') )
2287                 return;
2288
2289         $page = get_query_var('cpage');
2290
2291         if ( intval($page) <= 1 )
2292                 return;
2293
2294         $prevpage = intval($page) - 1;
2295
2296         if ( empty($label) )
2297                 $label = __('&laquo; Older Comments');
2298
2299         /**
2300          * Filter the anchor tag attributes for the previous comments page link.
2301          *
2302          * @since 2.7.0
2303          *
2304          * @param string $attributes Attributes for the anchor tag.
2305          */
2306         return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) .'</a>';
2307 }
2308
2309 /**
2310  * Display the previous comments page link.
2311  *
2312  * @since 2.7.0
2313  *
2314  * @param string $label Optional. Label for comments link text.
2315  */
2316 function previous_comments_link( $label = '' ) {
2317         echo get_previous_comments_link( $label );
2318 }
2319
2320 /**
2321  * Create pagination links for the comments on the current post.
2322  *
2323  * @see paginate_links()
2324  * @since 2.7.0
2325  *
2326  * @param string|array $args Optional args. See paginate_links().
2327  * @return string Markup for pagination links.
2328 */
2329 function paginate_comments_links($args = array()) {
2330         global $wp_rewrite;
2331
2332         if ( !is_singular() || !get_option('page_comments') )
2333                 return;
2334
2335         $page = get_query_var('cpage');
2336         if ( !$page )
2337                 $page = 1;
2338         $max_page = get_comment_pages_count();
2339         $defaults = array(
2340                 'base' => add_query_arg( 'cpage', '%#%' ),
2341                 'format' => '',
2342                 'total' => $max_page,
2343                 'current' => $page,
2344                 'echo' => true,
2345                 'add_fragment' => '#comments'
2346         );
2347         if ( $wp_rewrite->using_permalinks() )
2348                 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
2349
2350         $args = wp_parse_args( $args, $defaults );
2351         $page_links = paginate_links( $args );
2352
2353         if ( $args['echo'] )
2354                 echo $page_links;
2355         else
2356                 return $page_links;
2357 }
2358
2359 /**
2360  * Retrieve the Press This bookmarklet link.
2361  *
2362  * Use this in 'a' element 'href' attribute.
2363  *
2364  * @since 2.6.0
2365  *
2366  * @return string
2367  */
2368 function get_shortcut_link() {
2369         // In case of breaking changes, version this. #WP20071
2370         $link = "javascript:
2371                         var d=document,
2372                         w=window,
2373                         e=w.getSelection,
2374                         k=d.getSelection,
2375                         x=d.selection,
2376                         s=(e?e():(k)?k():(x?x.createRange().text:0)),
2377                         f='" . admin_url('press-this.php') . "',
2378                         l=d.location,
2379                         e=encodeURIComponent,
2380                         u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
2381                         a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
2382                         if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
2383                         void(0)";
2384
2385         $link = str_replace(array("\r", "\n", "\t"),  '', $link);
2386
2387         /**
2388          * Filter the Press This bookmarklet link.
2389          *
2390          * @since 2.6.0
2391          *
2392          * @param string $link The Press This bookmarklet link.
2393          */
2394         return apply_filters( 'shortcut_link', $link );
2395 }
2396
2397 /**
2398  * Retrieve the home url for the current site.
2399  *
2400  * Returns the 'home' option with the appropriate protocol, 'https' if
2401  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2402  * overridden.
2403  *
2404  * @since 3.0.0
2405  *
2406  * @uses get_home_url()
2407  *
2408  * @param  string $path   (optional) Path relative to the home url.
2409  * @param  string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2410  * @return string Home url link with optional path appended.
2411 */
2412 function home_url( $path = '', $scheme = null ) {
2413         return get_home_url( null, $path, $scheme );
2414 }
2415
2416 /**
2417  * Retrieve the home url for a given site.
2418  *
2419  * Returns the 'home' option with the appropriate protocol, 'https' if
2420  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2421  * overridden.
2422  *
2423  * @since 3.0.0
2424  *
2425  * @param  int $blog_id   (optional) Blog ID. Defaults to current blog.
2426  * @param  string $path   (optional) Path relative to the home url.
2427  * @param  string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2428  * @return string Home url link with optional path appended.
2429 */
2430 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2431         $orig_scheme = $scheme;
2432
2433         if ( empty( $blog_id ) || !is_multisite() ) {
2434                 $url = get_option( 'home' );
2435         } else {
2436                 switch_to_blog( $blog_id );
2437                 $url = get_option( 'home' );
2438                 restore_current_blog();
2439         }
2440
2441         if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2442                 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
2443                         $scheme = 'https';
2444                 else
2445                         $scheme = parse_url( $url, PHP_URL_SCHEME );
2446         }
2447
2448         $url = set_url_scheme( $url, $scheme );
2449
2450         if ( $path && is_string( $path ) )
2451                 $url .= '/' . ltrim( $path, '/' );
2452
2453         /**
2454          * Filter the home URL.
2455          *
2456          * @since 3.0.0
2457          *
2458          * @param string      $url         The complete home URL including scheme and path.
2459          * @param string      $path        Path relative to the home URL. Blank string if no path is specified.
2460          * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
2461          * @param int|null    $blog_id     Blog ID, or null for the current blog.
2462          */
2463         return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
2464 }
2465
2466 /**
2467  * Retrieve the site url for the current site.
2468  *
2469  * Returns the 'site_url' option with the appropriate protocol, 'https' if
2470  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2471  * overridden.
2472  *
2473  * @since 3.0.0
2474  *
2475  * @uses get_site_url()
2476  *
2477  * @param string $path Optional. Path relative to the site url.
2478  * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2479  * @return string Site url link with optional path appended.
2480 */
2481 function site_url( $path = '', $scheme = null ) {
2482         return get_site_url( null, $path, $scheme );
2483 }
2484
2485 /**
2486  * Retrieve the site url for a given site.
2487  *
2488  * Returns the 'site_url' option with the appropriate protocol, 'https' if
2489  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2490  * overridden.
2491  *
2492  * @since 3.0.0
2493  *
2494  * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2495  * @param string $path Optional. Path relative to the site url.
2496  * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
2497  * @return string Site url link with optional path appended.
2498 */
2499 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2500         if ( empty( $blog_id ) || !is_multisite() ) {
2501                 $url = get_option( 'siteurl' );
2502         } else {
2503                 switch_to_blog( $blog_id );
2504                 $url = get_option( 'siteurl' );
2505                 restore_current_blog();
2506         }
2507
2508         $url = set_url_scheme( $url, $scheme );
2509
2510         if ( $path && is_string( $path ) )
2511                 $url .= '/' . ltrim( $path, '/' );
2512
2513         /**
2514          * Filter the site URL.
2515          *
2516          * @since 2.7.0
2517          *
2518          * @param string      $url     The complete site URL including scheme and path.
2519          * @param string      $path    Path relative to the site URL. Blank string if no path is specified.
2520          * @param string|null $scheme  Scheme to give the site URL context. Accepts 'http', 'https', 'login',
2521          *                             'login_post', 'admin', 'relative' or null.
2522          * @param int|null    $blog_id Blog ID, or null for the current blog.
2523          */
2524         return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
2525 }
2526
2527 /**
2528  * Retrieve the url to the admin area for the current site.
2529  *
2530  * @since 2.6.0
2531  *
2532  * @param string $path Optional path relative to the admin url.
2533  * @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.
2534  * @return string Admin url link with optional path appended.
2535 */
2536 function admin_url( $path = '', $scheme = 'admin' ) {
2537         return get_admin_url( null, $path, $scheme );
2538 }
2539
2540 /**
2541  * Retrieve the url to the admin area for a given site.
2542  *
2543  * @since 3.0.0
2544  *
2545  * @param int $blog_id (optional) Blog ID. Defaults to current blog.
2546  * @param string $path Optional path relative to the admin url.
2547  * @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.
2548  * @return string Admin url link with optional path appended.
2549 */
2550 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2551         $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2552
2553         if ( $path && is_string( $path ) )
2554                 $url .= ltrim( $path, '/' );
2555
2556         /**
2557          * Filter the admin area URL.
2558          *
2559          * @since 2.8.0
2560          *
2561          * @param string   $url     The complete admin area URL including scheme and path.
2562          * @param string   $path    Path relative to the admin area URL. Blank string if no path is specified.
2563          * @param int|null $blog_id Blog ID, or null for the current blog.
2564          */
2565         return apply_filters( 'admin_url', $url, $path, $blog_id );
2566 }
2567
2568 /**
2569  * Retrieve the url to the includes directory.
2570  *
2571  * @since 2.6.0
2572  *
2573  * @param string $path Optional. Path relative to the includes url.
2574  * @param string $scheme Optional. Scheme to give the includes url context.
2575  * @return string Includes url link with optional path appended.
2576 */
2577 function includes_url( $path = '', $scheme = null ) {
2578         $url = site_url( '/' . WPINC . '/', $scheme );
2579
2580         if ( $path && is_string( $path ) )
2581                 $url .= ltrim($path, '/');
2582
2583         /**
2584          * Filter the URL to the includes directory.
2585          *
2586          * @since 2.8.0
2587          *
2588          * @param string $url  The complete URL to the includes directory including scheme and path.
2589          * @param string $path Path relative to the URL to the wp-includes directory. Blank string
2590          *                     if no path is specified.
2591          */
2592         return apply_filters( 'includes_url', $url, $path );
2593 }
2594
2595 /**
2596  * Retrieve the url to the content directory.
2597  *
2598  * @since 2.6.0
2599  *
2600  * @param string $path Optional. Path relative to the content url.
2601  * @return string Content url link with optional path appended.
2602 */
2603 function content_url($path = '') {
2604         $url = set_url_scheme( WP_CONTENT_URL );
2605
2606         if ( $path && is_string( $path ) )
2607                 $url .= '/' . ltrim($path, '/');
2608
2609         /**
2610          * Filter the URL to the content directory.
2611          *
2612          * @since 2.8.0
2613          *
2614          * @param string $url  The complete URL to the content directory including scheme and path.
2615          * @param string $path Path relative to the URL to the content directory. Blank string
2616          *                     if no path is specified.
2617          */
2618         return apply_filters( 'content_url', $url, $path);
2619 }
2620
2621 /**
2622  * Retrieve the url to the plugins directory or to a specific file within that directory.
2623  * You can hardcode the plugin slug in $path or pass __FILE__ as a second argument to get the correct folder name.
2624  *
2625  * @since 2.6.0
2626  *
2627  * @param string $path Optional. Path relative to the plugins url.
2628  * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
2629  * @return string Plugins url link with optional path appended.
2630 */
2631 function plugins_url($path = '', $plugin = '') {
2632
2633         $mu_plugin_dir = WPMU_PLUGIN_DIR;
2634         foreach ( array('path', 'plugin', 'mu_plugin_dir') as $var ) {
2635                 $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
2636                 $$var = preg_replace('|/+|', '/', $$var);
2637         }
2638
2639         if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2640                 $url = WPMU_PLUGIN_URL;
2641         else
2642                 $url = WP_PLUGIN_URL;
2643
2644
2645         $url = set_url_scheme( $url );
2646
2647         if ( !empty($plugin) && is_string($plugin) ) {
2648                 $folder = dirname(plugin_basename($plugin));
2649                 if ( '.' != $folder )
2650                         $url .= '/' . ltrim($folder, '/');
2651         }
2652
2653         if ( $path && is_string( $path ) )
2654                 $url .= '/' . ltrim($path, '/');
2655
2656         /**
2657          * Filter the URL to the plugins directory.
2658          *
2659          * @since 2.8.0
2660          *
2661          * @param string $url    The complete URL to the plugins directory including scheme and path.
2662          * @param string $path   Path relative to the URL to the plugins directory. Blank string
2663          *                       if no path is specified.
2664          * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
2665          *                       is specified.
2666          */
2667         return apply_filters( 'plugins_url', $url, $path, $plugin );
2668 }
2669
2670 /**
2671  * Retrieve the site url for the current network.
2672  *
2673  * Returns the site url with the appropriate protocol, 'https' if
2674  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2675  * overridden.
2676  *
2677  * @since 3.0.0
2678  *
2679  * @param string $path Optional. Path relative to the site url.
2680  * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2681  * @return string Site url link with optional path appended.
2682 */
2683 function network_site_url( $path = '', $scheme = null ) {
2684         if ( ! is_multisite() )
2685                 return site_url($path, $scheme);
2686
2687         $current_site = get_current_site();
2688
2689         if ( 'relative' == $scheme )
2690                 $url = $current_site->path;
2691         else
2692                 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2693
2694         if ( $path && is_string( $path ) )
2695                 $url .= ltrim( $path, '/' );
2696
2697         /**
2698          * Filter the network site URL.
2699          *
2700          * @since 3.0.0
2701          *
2702          * @param string      $url    The complete network site URL including scheme and path.
2703          * @param string      $path   Path relative to the network site URL. Blank string if
2704          *                            no path is specified.
2705          * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
2706          *                            'relative' or null.
2707          */
2708         return apply_filters( 'network_site_url', $url, $path, $scheme );
2709 }
2710
2711 /**
2712  * Retrieve the home url for the current network.
2713  *
2714  * Returns the home url with the appropriate protocol, 'https' if
2715  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2716  * overridden.
2717  *
2718  * @since 3.0.0
2719  *
2720  * @param  string $path   (optional) Path relative to the home url.
2721  * @param  string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https', or 'relative'.
2722  * @return string Home url link with optional path appended.
2723 */
2724 function network_home_url( $path = '', $scheme = null ) {
2725         if ( ! is_multisite() )
2726                 return home_url($path, $scheme);
2727
2728         $current_site = get_current_site();
2729         $orig_scheme = $scheme;
2730
2731         if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
2732                 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
2733
2734         if ( 'relative' == $scheme )
2735                 $url = $current_site->path;
2736         else
2737                 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2738
2739         if ( $path && is_string( $path ) )
2740                 $url .= ltrim( $path, '/' );
2741
2742         /**
2743          * Filter the network home URL.
2744          *
2745          * @since 3.0.0
2746          *
2747          * @param string      $url         The complete network home URL including scheme and path.
2748          * @param string      $path        Path relative to the network home URL. Blank string
2749          *                                 if no path is specified.
2750          * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
2751          *                                 'relative' or null.
2752          */
2753         return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
2754 }
2755
2756 /**
2757  * Retrieve the url to the admin area for the network.
2758  *
2759  * @since 3.0.0
2760  *
2761  * @param string $path Optional path relative to the admin url.
2762  * @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.
2763  * @return string Admin url link with optional path appended.
2764 */
2765 function network_admin_url( $path = '', $scheme = 'admin' ) {
2766         if ( ! is_multisite() )
2767                 return admin_url( $path, $scheme );
2768
2769         $url = network_site_url('wp-admin/network/', $scheme);
2770
2771         if ( $path && is_string( $path ) )
2772                 $url .= ltrim($path, '/');
2773
2774         /**
2775          * Filter the network admin URL.
2776          *
2777          * @since 3.0.0
2778          *
2779          * @param string $url  The complete network admin URL including scheme and path.
2780          * @param string $path Path relative to the network admin URL. Blank string if
2781          *                     no path is specified.
2782          */
2783         return apply_filters( 'network_admin_url', $url, $path );
2784 }
2785
2786 /**
2787  * Retrieve the url to the admin area for the current user.
2788  *
2789  * @since 3.0.0
2790  *
2791  * @param string $path Optional path relative to the admin url.
2792  * @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.
2793  * @return string Admin url link with optional path appended.
2794 */
2795 function user_admin_url( $path = '', $scheme = 'admin' ) {
2796         $url = network_site_url('wp-admin/user/', $scheme);
2797
2798         if ( $path && is_string( $path ) )
2799                 $url .= ltrim($path, '/');
2800
2801         /**
2802          * Filter the user admin URL for the current user.
2803          *
2804          * @since 3.1.0
2805          *
2806          * @param string $url  The complete URL including scheme and path.
2807          * @param string $path Path relative to the URL. Blank string if
2808          *                     no path is specified.
2809          */
2810         return apply_filters( 'user_admin_url', $url, $path );
2811 }
2812
2813 /**
2814  * Retrieve the url to the admin area for either the current blog or the network depending on context.
2815  *
2816  * @since 3.1.0
2817  *
2818  * @param string $path Optional path relative to the admin url.
2819  * @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.
2820  * @return string Admin url link with optional path appended.
2821 */
2822 function self_admin_url($path = '', $scheme = 'admin') {
2823         if ( is_network_admin() )
2824                 return network_admin_url($path, $scheme);
2825         elseif ( is_user_admin() )
2826                 return user_admin_url($path, $scheme);
2827         else
2828                 return admin_url($path, $scheme);
2829 }
2830
2831 /**
2832  * Set the scheme for a URL
2833  *
2834  * @since 3.4.0
2835  *
2836  * @param string $url Absolute url that includes a scheme
2837  * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
2838  * @return string $url URL with chosen scheme.
2839  */
2840 function set_url_scheme( $url, $scheme = null ) {
2841         $orig_scheme = $scheme;
2842         if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2843                 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
2844                         $scheme = 'https';
2845                 elseif ( ( 'login' == $scheme ) && force_ssl_admin() )
2846                         $scheme = 'https';
2847                 elseif ( ( 'admin' == $scheme ) && force_ssl_admin() )
2848                         $scheme = 'https';
2849                 else
2850                         $scheme = ( is_ssl() ? 'https' : 'http' );
2851         }
2852
2853         $url = trim( $url );
2854         if ( substr( $url, 0, 2 ) === '//' )
2855                 $url = 'http:' . $url;
2856
2857         if ( 'relative' == $scheme ) {
2858                 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
2859                 if ( $url !== '' && $url[0] === '/' )
2860                         $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
2861         } else {
2862                 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
2863         }
2864
2865         /**
2866          * Filter the resulting URL after setting the scheme.
2867          *
2868          * @since 3.4.0
2869          *
2870          * @param string $url         The complete URL including scheme and path.
2871          * @param string $scheme      Scheme applied to the URL. One of 'http', 'https', or 'relative'.
2872          * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
2873          *                            'login_post', 'admin', 'rpc', or 'relative'.
2874          */
2875         return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
2876 }
2877
2878 /**
2879  * Get the URL to the user's dashboard.
2880  *
2881  * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
2882  * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
2883  * primary blog is returned.
2884  *
2885  * @since 3.1.0
2886  *
2887  * @param int $user_id Optional. User ID. Defaults to current user.
2888  * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
2889  * @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.
2890  * @return string Dashboard url link with optional path appended.
2891  */
2892 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
2893         $user_id = $user_id ? (int) $user_id : get_current_user_id();
2894
2895         $blogs = get_blogs_of_user( $user_id );
2896         if ( ! is_super_admin() && empty($blogs) ) {
2897                 $url = user_admin_url( $path, $scheme );
2898         } elseif ( ! is_multisite() ) {
2899                 $url = admin_url( $path, $scheme );
2900         } else {
2901                 $current_blog = get_current_blog_id();
2902                 if ( $current_blog  && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
2903                         $url = admin_url( $path, $scheme );
2904                 } else {
2905                         $active = get_active_blog_for_user( $user_id );
2906                         if ( $active )
2907                                 $url = get_admin_url( $active->blog_id, $path, $scheme );
2908                         else
2909                                 $url = user_admin_url( $path, $scheme );
2910                 }
2911         }
2912
2913         /**
2914          * Filter the dashboard URL for a user.
2915          *
2916          * @since 3.1.0
2917          *
2918          * @param string $url     The complete URL including scheme and path.
2919          * @param int    $user_id The user ID.
2920          * @param string $path    Path relative to the URL. Blank string if no path is specified.
2921          * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
2922          *                        'login_post', 'admin', 'relative' or null.
2923          */
2924         return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
2925 }
2926
2927 /**
2928  * Get the URL to the user's profile editor.
2929  *
2930  * @since 3.1.0
2931  *
2932  * @param int    $user_id Optional. User ID. Defaults to current user.
2933  * @param string $scheme  The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
2934  *                        'http' or 'https' can be passed to force those schemes.
2935  * @return string Dashboard url link with optional path appended.
2936  */
2937 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
2938         $user_id = $user_id ? (int) $user_id : get_current_user_id();
2939
2940         if ( is_user_admin() )
2941                 $url = user_admin_url( 'profile.php', $scheme );
2942         elseif ( is_network_admin() )
2943                 $url = network_admin_url( 'profile.php', $scheme );
2944         else
2945                 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
2946
2947         /**
2948          * Filter the URL for a user's profile editor.
2949          *
2950          * @since 3.1.0
2951          *
2952          * @param string $url     The complete URL including scheme and path.
2953          * @param int    $user_id The user ID.
2954          * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
2955          *                        'login_post', 'admin', 'relative' or null.
2956          */
2957         return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
2958 }
2959
2960 /**
2961  * Output rel=canonical for singular queries.
2962  *
2963  * @since 2.9.0
2964 */
2965 function rel_canonical() {
2966         if ( !is_singular() )
2967                 return;
2968
2969         global $wp_the_query;
2970         if ( !$id = $wp_the_query->get_queried_object_id() )
2971                 return;
2972
2973         $link = get_permalink( $id );
2974
2975         if ( $page = get_query_var('cpage') )
2976                 $link = get_comments_pagenum_link( $page );
2977
2978         echo "<link rel='canonical' href='$link' />\n";
2979 }
2980
2981 /**
2982  * Return a shortlink for a post, page, attachment, or blog.
2983  *
2984  * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
2985  * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
2986  * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
2987  * via the get_shortlink filter.
2988  *
2989  * @since 3.0.0.
2990  *
2991  * @param int $id A post or blog id. Default is 0, which means the current post or blog.
2992  * @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'.
2993  * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
2994  * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
2995  */
2996 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
2997         /**
2998          * Filter whether to preempt generating a shortlink for the given post.
2999          *
3000          * Passing a truthy value to the filter will effectively short-circuit the
3001          * shortlink-generation process, returning that value instead.
3002          *
3003          * @since 3.0.0
3004          *
3005          * @param bool|string $return      Short-circuit return value. Either false or a URL string.
3006          * @param int         $id          Post ID, or 0 for the current post.
3007          * @param string      $context     The context for the link. One of 'post' or 'query',
3008          * @param bool        $allow_slugs Whether to allow post slugs in the shortlink.
3009          */
3010         $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3011
3012         if ( false !== $shortlink )
3013                 return $shortlink;
3014
3015         global $wp_query;
3016         $post_id = 0;
3017         if ( 'query' == $context && is_singular() ) {
3018                 $post_id = $wp_query->get_queried_object_id();
3019                 $post = get_post( $post_id );
3020         } elseif ( 'post' == $context ) {
3021                 $post = get_post( $id );
3022                 if ( ! empty( $post->ID ) )
3023                         $post_id = $post->ID;
3024         }
3025
3026         $shortlink = '';
3027
3028         // Return p= link for all public post types.
3029         if ( ! empty( $post_id ) ) {
3030                 $post_type = get_post_type_object( $post->post_type );
3031
3032                 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3033                         $shortlink = home_url( '/' );
3034                 } elseif ( $post_type->public ) {
3035                         $shortlink = home_url( '?p=' . $post_id );
3036                 }
3037         }
3038
3039         /**
3040          * Filter the shortlink for a post.
3041          *
3042          * @since 3.0.0
3043          *
3044          * @param string $shortlink   Shortlink URL.
3045          * @param int    $id          Post ID, or 0 for the current post.
3046          * @param string $context     The context for the link. One of 'post' or 'query',
3047          * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3048          */
3049         return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3050 }
3051
3052 /**
3053  *  Inject rel=shortlink into head if a shortlink is defined for the current page.
3054  *
3055  *  Attached to the wp_head action.
3056  *
3057  * @since 3.0.0
3058  *
3059  * @uses wp_get_shortlink()
3060  */
3061 function wp_shortlink_wp_head() {
3062         $shortlink = wp_get_shortlink( 0, 'query' );
3063
3064         if ( empty( $shortlink ) )
3065                 return;
3066
3067         echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3068 }
3069
3070 /**
3071  * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3072  *
3073  * Attached to the wp action.
3074  *
3075  * @since 3.0.0
3076  *
3077  * @uses wp_get_shortlink()
3078  */
3079 function wp_shortlink_header() {
3080         if ( headers_sent() )
3081                 return;
3082
3083         $shortlink = wp_get_shortlink(0, 'query');
3084
3085         if ( empty($shortlink) )
3086                 return;
3087
3088         header('Link: <' . $shortlink . '>; rel=shortlink', false);
3089 }
3090
3091 /**
3092  * Display the Short Link for a Post
3093  *
3094  * Must be called from inside "The Loop"
3095  *
3096  * Call like the_shortlink(__('Shortlinkage FTW'))
3097  *
3098  * @since 3.0.0
3099  *
3100  * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3101  * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3102  * @param string $before Optional HTML to display before the link.
3103  * @param string $after Optional HTML to display after the link.
3104  */
3105 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3106         $post = get_post();
3107
3108         if ( empty( $text ) )
3109                 $text = __('This is the short link.');
3110
3111         if ( empty( $title ) )
3112                 $title = the_title_attribute( array( 'echo' => false ) );
3113
3114         $shortlink = wp_get_shortlink( $post->ID );
3115
3116         if ( !empty( $shortlink ) ) {
3117                 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3118
3119                 /**
3120                  * Filter the shortlink anchor tag for a post.
3121                  *
3122                  * @since 3.0.0
3123                  *
3124                  * @param string $link      Shortlink anchor tag.
3125                  * @param string $shortlink Shortlink URL.
3126                  * @param string $text      Shortlink's text.
3127                  * @param string $title     Shortlink's title attribute.
3128                  */
3129                 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3130                 echo $before, $link, $after;
3131         }
3132 }