3 * WordPress Link Template Functions
10 * Display the permalink for the current post.
14 function the_permalink() {
16 * Filter the display of the permalink for the current post.
20 * @param string $permalink The permalink for the current post.
22 echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
26 * Retrieve trailing slash string, if blog set for adding trailing slashes.
28 * Conditionally adds a trailing slash if the permalink structure has a trailing
29 * slash, strips the trailing slash if not. The string is passed through the
30 * 'user_trailingslashit' filter. Will remove trailing slash from string, if
31 * blog is not set to have them.
36 * @param string $string URL with or without a trailing slash.
37 * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
38 * @return string The URL with the trailing slash appended or stripped.
40 function user_trailingslashit($string, $type_of_url = '') {
42 if ( $wp_rewrite->use_trailing_slashes )
43 $string = trailingslashit($string);
45 $string = untrailingslashit($string);
48 * Filter the trailing slashed string, depending on whether the site is set
49 * to use training slashes.
53 * @param string $string URL with or without a trailing slash.
54 * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
55 * 'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',
56 * 'month', 'day', 'paged', 'post_type_archive'.
58 $string = apply_filters( 'user_trailingslashit', $string, $type_of_url );
63 * Display permalink anchor for current post.
65 * The permalink mode title will use the post title for the 'a' element 'id'
66 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
70 * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
72 function permalink_anchor( $mode = 'id' ) {
74 switch ( strtolower( $mode ) ) {
76 $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
77 echo '<a id="'.$title.'"></a>';
81 echo '<a id="post-' . $post->ID . '"></a>';
87 * Retrieve full permalink for current post or post ID.
89 * This function is an alias for get_permalink().
93 * @see get_permalink()
95 * @param int|WP_Post $id Optional. Post ID or post object. Default is the current post.
96 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
97 * @return string|bool The permalink URL or false if post does not exist.
99 function get_the_permalink( $id = 0, $leavename = false ) {
100 return get_permalink( $id, $leavename );
104 * Retrieve full permalink for current post or post ID.
108 * @param int|WP_Post $id Optional. Post ID or post object. Default current post.
109 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
110 * @return string|bool The permalink URL or false if post does not exist.
112 function get_permalink( $id = 0, $leavename = false ) {
113 $rewritecode = array(
120 $leavename? '' : '%postname%',
124 $leavename? '' : '%pagename%',
127 if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
131 $post = get_post($id);
135 if ( empty($post->ID) )
138 if ( $post->post_type == 'page' )
139 return get_page_link($post, $leavename, $sample);
140 elseif ( $post->post_type == 'attachment' )
141 return get_attachment_link( $post, $leavename );
142 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
143 return get_post_permalink($post, $leavename, $sample);
145 $permalink = get_option('permalink_structure');
148 * Filter the permalink structure for a post before token replacement occurs.
150 * Only applies to posts with post_type of 'post'.
154 * @param string $permalink The site's permalink structure.
155 * @param WP_Post $post The post in question.
156 * @param bool $leavename Whether to keep the post name.
158 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
160 if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
161 $unixtime = strtotime($post->post_date);
164 if ( strpos($permalink, '%category%') !== false ) {
165 $cats = get_the_category($post->ID);
167 usort($cats, '_usort_terms_by_ID'); // order by ID
170 * Filter the category that gets used in the %category% permalink token.
174 * @param stdClass $cat The category to use in the permalink.
175 * @param array $cats Array of all categories associated with the post.
176 * @param WP_Post $post The post in question.
178 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
180 $category_object = get_term( $category_object, 'category' );
181 $category = $category_object->slug;
182 if ( $parent = $category_object->parent )
183 $category = get_category_parents($parent, false, '/', true) . $category;
185 // show default category in permalinks, without
186 // having to assign it explicitly
187 if ( empty($category) ) {
188 $default_category = get_term( get_option( 'default_category' ), 'category' );
189 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
194 if ( strpos($permalink, '%author%') !== false ) {
195 $authordata = get_userdata($post->post_author);
196 $author = $authordata->user_nicename;
199 $date = explode(" ",date('Y m d H i s', $unixtime));
214 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
215 $permalink = user_trailingslashit($permalink, 'single');
216 } else { // if they're not using the fancy permalink option
217 $permalink = home_url('?p=' . $post->ID);
221 * Filter the permalink for a post.
223 * Only applies to posts with post_type of 'post'.
227 * @param string $permalink The post's permalink.
228 * @param WP_Post $post The post in question.
229 * @param bool $leavename Whether to keep the post name.
231 return apply_filters( 'post_link', $permalink, $post, $leavename );
235 * Retrieve the permalink for a post with a custom post type.
239 * @param int $id Optional. Post ID.
240 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
241 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
242 * @return string The post permalink.
244 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
247 $post = get_post($id);
249 if ( is_wp_error( $post ) )
252 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
254 $slug = $post->post_name;
256 $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) );
258 $post_type = get_post_type_object($post->post_type);
260 if ( $post_type->hierarchical ) {
261 $slug = get_page_uri( $id );
264 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
265 if ( ! $leavename ) {
266 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
268 $post_link = home_url( user_trailingslashit($post_link) );
270 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
271 $post_link = add_query_arg($post_type->query_var, $slug, '');
273 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
274 $post_link = home_url($post_link);
278 * Filter the permalink for a post with a custom post type.
282 * @param string $post_link The post's permalink.
283 * @param WP_Post $post The post in question.
284 * @param bool $leavename Whether to keep the post name.
285 * @param bool $sample Is it a sample permalink.
287 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
291 * Retrieve permalink from post ID.
295 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
296 * @param mixed $deprecated Not used.
299 function post_permalink( $post_id = 0, $deprecated = '' ) {
300 if ( !empty( $deprecated ) )
301 _deprecated_argument( __FUNCTION__, '1.3' );
303 return get_permalink($post_id);
307 * Retrieve the permalink for current page or page ID.
309 * Respects page_on_front. Use this one.
313 * @param int|object $post Optional. Post ID or object.
314 * @param bool $leavename Optional, defaults to false. Whether to keep page name.
315 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
316 * @return string The page permalink.
318 function get_page_link( $post = false, $leavename = false, $sample = false ) {
319 $post = get_post( $post );
321 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
322 $link = home_url('/');
324 $link = _get_page_link( $post, $leavename, $sample );
327 * Filter the permalink for a page.
331 * @param string $link The page's permalink.
332 * @param int $post_id The ID of the page.
333 * @param bool $sample Is it a sample permalink.
335 return apply_filters( 'page_link', $link, $post->ID, $sample );
339 * Retrieve the page permalink.
341 * Ignores page_on_front. Internal use only.
346 * @param int|object $post Optional. Post ID or object.
347 * @param bool $leavename Optional. Leave name.
348 * @param bool $sample Optional. Sample permalink.
349 * @return string The page permalink.
351 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
354 $post = get_post( $post );
356 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
358 $link = $wp_rewrite->get_page_permastruct();
360 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
361 if ( ! $leavename ) {
362 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
365 $link = home_url($link);
366 $link = user_trailingslashit($link, 'page');
368 $link = home_url( '?page_id=' . $post->ID );
372 * Filter the permalink for a non-page_on_front page.
376 * @param string $link The page's permalink.
377 * @param int $post_id The ID of the page.
379 return apply_filters( '_get_page_link', $link, $post->ID );
383 * Retrieve permalink for attachment.
385 * This can be used in the WordPress Loop or outside of it.
389 * @param int|object $post Optional. Post ID or object.
390 * @param bool $leavename Optional. Leave name.
391 * @return string The attachment permalink.
393 function get_attachment_link( $post = null, $leavename = false ) {
398 $post = get_post( $post );
399 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
401 if ( $wp_rewrite->using_permalinks() && $parent ) {
402 if ( 'page' == $parent->post_type )
403 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
405 $parentlink = get_permalink( $post->post_parent );
407 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
408 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
410 $name = $post->post_name;
412 if ( strpos($parentlink, '?') === false )
413 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
416 $link = str_replace( '%postname%', $name, $link );
420 $link = home_url( '/?attachment_id=' . $post->ID );
423 * Filter the permalink for an attachment.
427 * @param string $link The attachment's permalink.
428 * @param int $post_id Attachment ID.
430 return apply_filters( 'attachment_link', $link, $post->ID );
434 * Retrieve the permalink for the year archives.
438 * @param int|bool $year False for current year or year for permalink.
439 * @return string The permalink for the specified year archive.
441 function get_year_link($year) {
444 $year = gmdate('Y', current_time('timestamp'));
445 $yearlink = $wp_rewrite->get_year_permastruct();
446 if ( !empty($yearlink) ) {
447 $yearlink = str_replace('%year%', $year, $yearlink);
448 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
450 $yearlink = home_url( '?m=' . $year );
454 * Filter the year archive permalink.
458 * @param string $yearlink Permalink for the year archive.
459 * @param int $year Year for the archive.
461 return apply_filters( 'year_link', $yearlink, $year );
465 * Retrieve the permalink for the month archives with year.
469 * @param bool|int $year False for current year. Integer of year.
470 * @param bool|int $month False for current month. Integer of month.
471 * @return string The permalink for the specified month and year archive.
473 function get_month_link($year, $month) {
476 $year = gmdate('Y', current_time('timestamp'));
478 $month = gmdate('m', current_time('timestamp'));
479 $monthlink = $wp_rewrite->get_month_permastruct();
480 if ( !empty($monthlink) ) {
481 $monthlink = str_replace('%year%', $year, $monthlink);
482 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
483 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
485 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
489 * Filter the month archive permalink.
493 * @param string $monthlink Permalink for the month archive.
494 * @param int $year Year for the archive.
495 * @param int $month The month for the archive.
497 return apply_filters( 'month_link', $monthlink, $year, $month );
501 * Retrieve the permalink for the day archives with year and month.
505 * @param bool|int $year False for current year. Integer of year.
506 * @param bool|int $month False for current month. Integer of month.
507 * @param bool|int $day False for current day. Integer of day.
508 * @return string The permalink for the specified day, month, and year archive.
510 function get_day_link($year, $month, $day) {
513 $year = gmdate('Y', current_time('timestamp'));
515 $month = gmdate('m', current_time('timestamp'));
517 $day = gmdate('j', current_time('timestamp'));
519 $daylink = $wp_rewrite->get_day_permastruct();
520 if ( !empty($daylink) ) {
521 $daylink = str_replace('%year%', $year, $daylink);
522 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
523 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
524 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
526 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
530 * Filter the day archive permalink.
534 * @param string $daylink Permalink for the day archive.
535 * @param int $year Year for the archive.
536 * @param int $month Month for the archive.
537 * @param int $day The day for the archive.
539 return apply_filters( 'day_link', $daylink, $year, $month, $day );
543 * Display the permalink for the feed type.
547 * @param string $anchor The link's anchor text.
548 * @param string $feed Optional, defaults to default feed. Feed type.
550 function the_feed_link( $anchor, $feed = '' ) {
551 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
554 * Filter the feed link anchor tag.
558 * @param string $link The complete anchor tag for a feed link.
559 * @param string $feed The feed type, or an empty string for the
562 echo apply_filters( 'the_feed_link', $link, $feed );
566 * Retrieve the permalink for the feed type.
570 * @param string $feed Optional, defaults to default feed. Feed type.
571 * @return string The feed permalink.
573 function get_feed_link($feed = '') {
576 $permalink = $wp_rewrite->get_feed_permastruct();
577 if ( '' != $permalink ) {
578 if ( false !== strpos($feed, 'comments_') ) {
579 $feed = str_replace('comments_', '', $feed);
580 $permalink = $wp_rewrite->get_comment_feed_permastruct();
583 if ( get_default_feed() == $feed )
586 $permalink = str_replace('%feed%', $feed, $permalink);
587 $permalink = preg_replace('#/+#', '/', "/$permalink");
588 $output = home_url( user_trailingslashit($permalink, 'feed') );
591 $feed = get_default_feed();
593 if ( false !== strpos($feed, 'comments_') )
594 $feed = str_replace('comments_', 'comments-', $feed);
596 $output = home_url("?feed={$feed}");
600 * Filter the feed type permalink.
604 * @param string $output The feed permalink.
605 * @param string $feed Feed type.
607 return apply_filters( 'feed_link', $output, $feed );
611 * Retrieve the permalink for the post comments feed.
615 * @param int $post_id Optional. Post ID.
616 * @param string $feed Optional. Feed type.
617 * @return string The permalink for the comments feed for the given post.
619 function get_post_comments_feed_link($post_id = 0, $feed = '') {
620 $post_id = absint( $post_id );
623 $post_id = get_the_ID();
625 if ( empty( $feed ) )
626 $feed = get_default_feed();
628 if ( '' != get_option('permalink_structure') ) {
629 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
630 $url = _get_page_link( $post_id );
632 $url = get_permalink($post_id);
634 $url = trailingslashit($url) . 'feed';
635 if ( $feed != get_default_feed() )
637 $url = user_trailingslashit($url, 'single_feed');
639 $type = get_post_field('post_type', $post_id);
640 if ( 'page' == $type )
641 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
643 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
647 * Filter the post comments feed permalink.
651 * @param string $url Post comments feed permalink.
653 return apply_filters( 'post_comments_feed_link', $url );
657 * Display the comment feed link for a post.
659 * Prints out the comment feed link for a post. Link text is placed in the
660 * anchor. If no link text is specified, default text is used. If no post ID is
661 * specified, the current post is used.
665 * @param string $link_text Descriptive text.
666 * @param int $post_id Optional post ID. Default to current post.
667 * @param string $feed Optional. Feed format.
668 * @return string Link to the comment feed for the current post.
670 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
671 $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
672 if ( empty($link_text) )
673 $link_text = __('Comments Feed');
676 * Filter the post comment feed link anchor tag.
680 * @param string $link The complete anchor tag for the comment feed link.
681 * @param int $post_id Post ID.
682 * @param string $feed The feed type, or an empty string for the default feed type.
684 echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
688 * Retrieve the feed link for a given author.
690 * Returns a link to the feed for all posts by a given author. A specific feed
691 * can be requested or left blank to get the default feed.
695 * @param int $author_id ID of an author.
696 * @param string $feed Optional. Feed type.
697 * @return string Link to the feed for the author specified by $author_id.
699 function get_author_feed_link( $author_id, $feed = '' ) {
700 $author_id = (int) $author_id;
701 $permalink_structure = get_option('permalink_structure');
704 $feed = get_default_feed();
706 if ( '' == $permalink_structure ) {
707 $link = home_url("?feed=$feed&author=" . $author_id);
709 $link = get_author_posts_url($author_id);
710 if ( $feed == get_default_feed() )
713 $feed_link = "feed/$feed";
715 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
719 * Filter the feed link for a given author.
723 * @param string $link The author feed link.
724 * @param string $feed Feed type.
726 $link = apply_filters( 'author_feed_link', $link, $feed );
732 * Retrieve the feed link for a category.
734 * Returns a link to the feed for all posts in a given category. A specific feed
735 * can be requested or left blank to get the default feed.
739 * @param int $cat_id ID of a category.
740 * @param string $feed Optional. Feed type.
741 * @return string Link to the feed for the category specified by $cat_id.
743 function get_category_feed_link($cat_id, $feed = '') {
744 return get_term_feed_link($cat_id, 'category', $feed);
748 * Retrieve the feed link for a term.
750 * Returns a link to the feed for all posts in a given term. A specific feed
751 * can be requested or left blank to get the default feed.
755 * @param int $term_id ID of a category.
756 * @param string $taxonomy Optional. Taxonomy of $term_id
757 * @param string $feed Optional. Feed type.
758 * @return string Link to the feed for the term specified by $term_id and $taxonomy.
760 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
761 $term_id = ( int ) $term_id;
763 $term = get_term( $term_id, $taxonomy );
765 if ( empty( $term ) || is_wp_error( $term ) )
768 if ( empty( $feed ) )
769 $feed = get_default_feed();
771 $permalink_structure = get_option( 'permalink_structure' );
773 if ( '' == $permalink_structure ) {
774 if ( 'category' == $taxonomy ) {
775 $link = home_url("?feed=$feed&cat=$term_id");
777 elseif ( 'post_tag' == $taxonomy ) {
778 $link = home_url("?feed=$feed&tag=$term->slug");
780 $t = get_taxonomy( $taxonomy );
781 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
784 $link = get_term_link( $term_id, $term->taxonomy );
785 if ( $feed == get_default_feed() )
788 $feed_link = "feed/$feed";
790 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
793 if ( 'category' == $taxonomy ) {
795 * Filter the category feed link.
799 * @param string $link The category feed link.
800 * @param string $feed Feed type.
802 $link = apply_filters( 'category_feed_link', $link, $feed );
803 } elseif ( 'post_tag' == $taxonomy ) {
805 * Filter the post tag feed link.
809 * @param string $link The tag feed link.
810 * @param string $feed Feed type.
812 $link = apply_filters( 'tag_feed_link', $link, $feed );
815 * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
819 * @param string $link The taxonomy feed link.
820 * @param string $feed Feed type.
821 * @param string $feed The taxonomy name.
823 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
830 * Retrieve permalink for feed of tag.
834 * @param int $tag_id Tag ID.
835 * @param string $feed Optional. Feed type.
836 * @return string The feed permalink for the given tag.
838 function get_tag_feed_link($tag_id, $feed = '') {
839 return get_term_feed_link($tag_id, 'post_tag', $feed);
843 * Retrieve edit tag link.
847 * @param int $tag_id Tag ID
848 * @param string $taxonomy Taxonomy
849 * @return string The edit tag link URL for the given tag.
851 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
853 * Filter the edit link for a tag (or term in another taxonomy).
857 * @param string $link The term edit link.
859 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
863 * Display or retrieve edit tag link with formatting.
867 * @param string $link Optional. Anchor text.
868 * @param string $before Optional. Display before edit link.
869 * @param string $after Optional. Display after edit link.
870 * @param object $tag Tag object.
871 * @return string HTML content.
873 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
874 $link = edit_term_link( $link, '', '', $tag, false );
877 * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
881 * @param string $link The anchor tag for the edit link.
883 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
887 * Retrieve edit term url.
891 * @param int $term_id Term ID.
892 * @param string $taxonomy Taxonomy.
893 * @param string $object_type The object type. Used to highlight the proper post type menu on the linked page.
894 * Defaults to the first object_type associated with the taxonomy.
895 * @return string The edit term link URL for the given term.
897 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
898 $tax = get_taxonomy( $taxonomy );
899 if ( !current_user_can( $tax->cap->edit_terms ) )
902 $term = get_term( $term_id, $taxonomy );
906 'taxonomy' => $taxonomy,
907 'tag_ID' => $term->term_id,
910 if ( $object_type ) {
911 $args['post_type'] = $object_type;
912 } else if ( ! empty( $tax->object_type ) ) {
913 $args['post_type'] = reset( $tax->object_type );
916 $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
919 * Filter the edit link for a term.
923 * @param string $location The edit link.
924 * @param int $term_id Term ID.
925 * @param string $taxonomy Taxonomy name.
926 * @param string $object_type The object type (eg. the post type).
928 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
932 * Display or retrieve edit term link with formatting.
936 * @param string $link Optional. Anchor text.
937 * @param string $before Optional. Display before edit link.
938 * @param string $after Optional. Display after edit link.
939 * @param object $term Term object.
940 * @return string HTML content.
942 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
943 if ( is_null( $term ) )
944 $term = get_queried_object();
949 $tax = get_taxonomy( $term->taxonomy );
950 if ( ! current_user_can( $tax->cap->edit_terms ) )
953 if ( empty( $link ) )
954 $link = __('Edit This');
956 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
959 * Filter the anchor tag for the edit link of a term.
963 * @param string $link The anchor tag for the edit link.
964 * @param int $term_id Term ID.
966 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
975 * Retrieve permalink for search.
979 * @param string $query Optional. The query string to use. If empty the current query is used.
980 * @return string The search permalink.
982 function get_search_link( $query = '' ) {
986 $search = get_search_query( false );
988 $search = stripslashes($query);
990 $permastruct = $wp_rewrite->get_search_permastruct();
992 if ( empty( $permastruct ) ) {
993 $link = home_url('?s=' . urlencode($search) );
995 $search = urlencode($search);
996 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
997 $link = str_replace( '%search%', $search, $permastruct );
998 $link = home_url( user_trailingslashit( $link, 'search' ) );
1002 * Filter the search permalink.
1006 * @param string $link Search permalink.
1007 * @param string $search The URL-encoded search term.
1009 return apply_filters( 'search_link', $link, $search );
1013 * Retrieve the permalink for the feed of the search results.
1017 * @param string $search_query Optional. Search query.
1018 * @param string $feed Optional. Feed type.
1019 * @return string The search results feed permalink.
1021 function get_search_feed_link($search_query = '', $feed = '') {
1023 $link = get_search_link($search_query);
1026 $feed = get_default_feed();
1028 $permastruct = $wp_rewrite->get_search_permastruct();
1030 if ( empty($permastruct) ) {
1031 $link = add_query_arg('feed', $feed, $link);
1033 $link = trailingslashit($link);
1034 $link .= "feed/$feed/";
1038 * Filter the search feed link.
1042 * @param string $link Search feed link.
1043 * @param string $feed Feed type.
1044 * @param string $type The search type. One of 'posts' or 'comments'.
1046 $link = apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1052 * Retrieve the permalink for the comments feed of the search results.
1056 * @param string $search_query Optional. Search query.
1057 * @param string $feed Optional. Feed type.
1058 * @return string The comments feed search results permalink.
1060 function get_search_comments_feed_link($search_query = '', $feed = '') {
1064 $feed = get_default_feed();
1066 $link = get_search_feed_link($search_query, $feed);
1068 $permastruct = $wp_rewrite->get_search_permastruct();
1070 if ( empty($permastruct) )
1071 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1073 $link = add_query_arg('withcomments', 1, $link);
1075 /** This filter is documented in wp-includes/link-template.php */
1076 $link = apply_filters('search_feed_link', $link, $feed, 'comments');
1082 * Retrieve the permalink for a post type archive.
1086 * @param string $post_type Post type
1087 * @return string The post type archive permalink.
1089 function get_post_type_archive_link( $post_type ) {
1091 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1094 if ( ! $post_type_obj->has_archive )
1097 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1098 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1099 if ( $post_type_obj->rewrite['with_front'] )
1100 $struct = $wp_rewrite->front . $struct;
1102 $struct = $wp_rewrite->root . $struct;
1103 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1105 $link = home_url( '?post_type=' . $post_type );
1109 * Filter the post type archive permalink.
1113 * @param string $link The post type archive permalink.
1114 * @param string $post_type Post type name.
1116 return apply_filters( 'post_type_archive_link', $link, $post_type );
1120 * Retrieve the permalink for a post type archive feed.
1124 * @param string $post_type Post type
1125 * @param string $feed Optional. Feed type
1126 * @return string The post type feed permalink.
1128 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1129 $default_feed = get_default_feed();
1130 if ( empty( $feed ) )
1131 $feed = $default_feed;
1133 if ( ! $link = get_post_type_archive_link( $post_type ) )
1136 $post_type_obj = get_post_type_object( $post_type );
1137 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1138 $link = trailingslashit( $link );
1140 if ( $feed != $default_feed )
1143 $link = add_query_arg( 'feed', $feed, $link );
1147 * Filter the post type archive feed link.
1151 * @param string $link The post type archive feed link.
1152 * @param string $feed Feed type.
1154 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1158 * Retrieve edit posts link for post.
1160 * Can be used within the WordPress loop or outside of it. Can be used with
1161 * pages, posts, attachments, and revisions.
1165 * @param int $id Optional. Post ID.
1166 * @param string $context Optional, defaults to display. How to write the '&', defaults to '&'.
1167 * @return string The edit post link for the given post.
1169 function get_edit_post_link( $id = 0, $context = 'display' ) {
1170 if ( ! $post = get_post( $id ) )
1173 if ( 'revision' === $post->post_type )
1175 elseif ( 'display' == $context )
1176 $action = '&action=edit';
1178 $action = '&action=edit';
1180 $post_type_object = get_post_type_object( $post->post_type );
1181 if ( !$post_type_object )
1184 if ( !current_user_can( 'edit_post', $post->ID ) )
1188 * Filter the post edit link.
1192 * @param string $link The edit link.
1193 * @param int $post_id Post ID.
1194 * @param string $context The link context. If set to 'display' then ampersands
1197 return apply_filters( 'get_edit_post_link', admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ), $post->ID, $context );
1201 * Display edit post link for post.
1205 * @param string $text Optional. Anchor text.
1206 * @param string $before Optional. Display before edit link.
1207 * @param string $after Optional. Display after edit link.
1208 * @param int $id Optional. Post ID.
1210 function edit_post_link( $text = null, $before = '', $after = '', $id = 0 ) {
1211 if ( ! $post = get_post( $id ) ) {
1215 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1219 if ( null === $text ) {
1220 $text = __( 'Edit This' );
1223 $link = '<a class="post-edit-link" href="' . $url . '">' . $text . '</a>';
1226 * Filter the post edit link anchor tag.
1230 * @param string $link Anchor tag for the edit link.
1231 * @param int $post_id Post ID.
1232 * @param string $text Anchor text.
1234 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1238 * Retrieve delete posts link for post.
1240 * Can be used within the WordPress loop or outside of it, with any post type.
1244 * @param int $id Optional. Post ID.
1245 * @param string $deprecated Not used.
1246 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1247 * @return string The delete post link URL for the given post.
1249 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1250 if ( ! empty( $deprecated ) )
1251 _deprecated_argument( __FUNCTION__, '3.0' );
1253 if ( !$post = get_post( $id ) )
1256 $post_type_object = get_post_type_object( $post->post_type );
1257 if ( !$post_type_object )
1260 if ( !current_user_can( 'delete_post', $post->ID ) )
1263 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1265 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1268 * Filter the post delete link.
1272 * @param string $link The delete link.
1273 * @param int $post_id Post ID.
1274 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1276 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1280 * Retrieve edit comment link.
1284 * @param int $comment_id Optional. Comment ID.
1285 * @return string The edit comment link URL for the given comment.
1287 function get_edit_comment_link( $comment_id = 0 ) {
1288 $comment = get_comment( $comment_id );
1290 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1293 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1296 * Filter the comment edit link.
1300 * @param string $location The edit link.
1302 return apply_filters( 'get_edit_comment_link', $location );
1306 * Display edit comment link with formatting.
1310 * @param string $text Optional. Anchor text.
1311 * @param string $before Optional. Display before edit link.
1312 * @param string $after Optional. Display after edit link.
1314 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1317 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1321 if ( null === $text ) {
1322 $text = __( 'Edit This' );
1325 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $text . '</a>';
1328 * Filter the comment edit link anchor tag.
1332 * @param string $link Anchor tag for the edit link.
1333 * @param int $comment_id Comment ID.
1334 * @param string $text Anchor text.
1336 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1340 * Display edit bookmark (literally a URL external to blog) link.
1344 * @param int|stdClass $link Optional. Bookmark ID.
1345 * @return string The edit bookmark link URL.
1347 function get_edit_bookmark_link( $link = 0 ) {
1348 $link = get_bookmark( $link );
1350 if ( !current_user_can('manage_links') )
1353 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1356 * Filter the bookmark (link) edit link.
1360 * @param string $location The edit link.
1361 * @param int $link_id Bookmark ID.
1363 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1367 * Display edit bookmark (literally a URL external to blog) link anchor content.
1371 * @param string $link Optional. Anchor text.
1372 * @param string $before Optional. Display before edit link.
1373 * @param string $after Optional. Display after edit link.
1374 * @param int $bookmark Optional. Bookmark ID.
1376 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1377 $bookmark = get_bookmark($bookmark);
1379 if ( !current_user_can('manage_links') )
1383 $link = __('Edit This');
1385 $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
1388 * Filter the bookmark edit link anchor tag.
1392 * @param string $link Anchor tag for the edit link.
1393 * @param int $link_id Bookmark ID.
1395 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1399 * Retrieve edit user link
1403 * @param int $user_id Optional. User ID. Defaults to the current user.
1404 * @return string URL to edit user page or empty string.
1406 function get_edit_user_link( $user_id = null ) {
1408 $user_id = get_current_user_id();
1410 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1413 $user = get_userdata( $user_id );
1418 if ( get_current_user_id() == $user->ID )
1419 $link = get_edit_profile_url( $user->ID );
1421 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1424 * Filter the user edit link.
1428 * @param string $link The edit link.
1429 * @param int $user_id User ID.
1431 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1437 * Retrieve previous post that is adjacent to current post.
1441 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1442 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1443 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1444 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1446 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1447 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1451 * Retrieve next post that is adjacent to current post.
1455 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1456 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1457 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1458 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1460 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1461 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1465 * Retrieve adjacent post.
1467 * Can either be next or previous post.
1471 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
1472 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1473 * @param bool $previous Optional. Whether to retrieve previous post.
1474 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1475 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1477 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1480 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1483 $current_post_date = $post->post_date;
1488 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1489 $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";
1490 $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1492 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1493 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1494 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1495 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1496 $excluded_terms = explode( ' and ', $excluded_terms );
1498 $excluded_terms = explode( ',', $excluded_terms );
1501 $excluded_terms = array_map( 'intval', $excluded_terms );
1504 if ( $in_same_term ) {
1505 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1507 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1509 // Remove any exclusions from the term array to include.
1510 $term_array = array_diff( $term_array, (array) $excluded_terms );
1511 $term_array = array_map( 'intval', $term_array );
1513 if ( ! $term_array || is_wp_error( $term_array ) )
1516 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1519 if ( ! empty( $excluded_terms ) ) {
1520 $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( $excluded_terms, ',' ) . ') )';
1524 // 'post_status' clause depends on the current user.
1525 if ( is_user_logged_in() ) {
1526 $user_id = get_current_user_id();
1528 $post_type_object = get_post_type_object( $post->post_type );
1529 if ( empty( $post_type_object ) ) {
1530 $post_type_cap = $post->post_type;
1531 $read_private_cap = 'read_private_' . $post_type_cap . 's';
1533 $read_private_cap = $post_type_object->cap->read_private_posts;
1537 * Results should include private posts belonging to the current user, or private posts where the
1538 * current user has the 'read_private_posts' cap.
1540 $private_states = get_post_stati( array( 'private' => true ) );
1541 $where .= " AND ( p.post_status = 'publish'";
1542 foreach ( (array) $private_states as $state ) {
1543 if ( current_user_can( $read_private_cap ) ) {
1544 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1546 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1551 $where .= " AND p.post_status = 'publish'";
1554 $adjacent = $previous ? 'previous' : 'next';
1555 $op = $previous ? '<' : '>';
1556 $order = $previous ? 'DESC' : 'ASC';
1559 * Filter the JOIN clause in the SQL for an adjacent post query.
1561 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1562 * of adjacency, 'next' or 'previous'.
1566 * @param string $join The JOIN clause in the SQL.
1567 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1568 * @param array $excluded_terms Array of excluded term IDs.
1570 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
1573 * Filter the WHERE clause in the SQL for an adjacent post query.
1575 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1576 * of adjacency, 'next' or 'previous'.
1580 * @param string $where The `WHERE` clause in the SQL.
1581 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1582 * @param array $excluded_terms Array of excluded term IDs.
1584 $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
1587 * Filter the ORDER BY clause in the SQL for an adjacent post query.
1589 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1590 * of adjacency, 'next' or 'previous'.
1594 * @param string $order_by The `ORDER BY` clause in the SQL.
1596 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1598 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1599 $query_key = 'adjacent_post_' . md5( $query );
1600 $result = wp_cache_get( $query_key, 'counts' );
1601 if ( false !== $result ) {
1603 $result = get_post( $result );
1607 $result = $wpdb->get_var( $query );
1608 if ( null === $result )
1611 wp_cache_set( $query_key, $result, 'counts' );
1614 $result = get_post( $result );
1620 * Get adjacent post relational link.
1622 * Can either be next or previous post relational link.
1626 * @param string $title Optional. Link title format.
1627 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1628 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1629 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1630 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1631 * @return string The adjacent post relational link URL.
1633 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1634 if ( $previous && is_attachment() && $post = get_post() )
1635 $post = get_post( $post->post_parent );
1637 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1639 if ( empty( $post ) )
1642 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1644 if ( empty( $post_title ) )
1645 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1647 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1649 $title = str_replace( '%title', $post_title, $title );
1650 $title = str_replace( '%date', $date, $title );
1652 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1653 $link .= esc_attr( $title );
1654 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1656 $adjacent = $previous ? 'previous' : 'next';
1659 * Filter the adjacent post relational link.
1661 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1662 * of adjacency, 'next' or 'previous'.
1666 * @param string $link The relational link.
1668 return apply_filters( "{$adjacent}_post_rel_link", $link );
1672 * Display relational links for the posts adjacent to the current post.
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.
1679 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1681 function adjacent_posts_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 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1687 * Display relational links for the posts adjacent to the current post for single post pages.
1689 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1693 function adjacent_posts_rel_link_wp_head() {
1694 if ( ! is_single() || is_attachment() ) {
1697 adjacent_posts_rel_link();
1701 * Display relational link for the next post adjacent to the current post.
1705 * @param string $title Optional. Link title format.
1706 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1707 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1708 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1710 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1711 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1715 * Display relational link for the previous post adjacent to the current post.
1719 * @param string $title Optional. Link title format.
1720 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1721 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1722 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1724 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1725 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1729 * Retrieve boundary post.
1731 * Boundary being either the first or last post by publish date within the constraints specified
1732 * by $in_same_term or $excluded_terms.
1736 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1737 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1738 * @param bool $start Optional. Whether to retrieve first or last post.
1739 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1740 * @return mixed Array containing the boundary post object if successful, null otherwise.
1742 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1744 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1747 $query_args = array(
1748 'posts_per_page' => 1,
1749 'order' => $start ? 'ASC' : 'DESC',
1750 'update_post_term_cache' => false,
1751 'update_post_meta_cache' => false
1754 $term_array = array();
1756 if ( ! is_array( $excluded_terms ) ) {
1757 if ( ! empty( $excluded_terms ) )
1758 $excluded_terms = explode( ',', $excluded_terms );
1760 $excluded_terms = array();
1763 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1764 if ( $in_same_term )
1765 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1767 if ( ! empty( $excluded_terms ) ) {
1768 $excluded_terms = array_map( 'intval', $excluded_terms );
1769 $excluded_terms = array_diff( $excluded_terms, $term_array );
1771 $inverse_terms = array();
1772 foreach ( $excluded_terms as $excluded_term )
1773 $inverse_terms[] = $excluded_term * -1;
1774 $excluded_terms = $inverse_terms;
1777 $query_args[ 'tax_query' ] = array( array(
1778 'taxonomy' => $taxonomy,
1779 'terms' => array_merge( $term_array, $excluded_terms )
1783 return get_posts( $query_args );
1787 * Get previous post link that is adjacent to the current post.
1791 * @param string $format Optional. Link anchor format.
1792 * @param string $link Optional. Link permalink format.
1793 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1794 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1795 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1796 * @return string The link URL of the previous post in relation to the current post.
1798 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1799 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1803 * Display previous post link that is adjacent to the current post.
1806 * @see get_previous_post_link()
1808 * @param string $format Optional. Link anchor format.
1809 * @param string $link Optional. Link permalink format.
1810 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1811 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1812 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1814 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1815 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1819 * Get next post link that is adjacent to the current post.
1823 * @param string $format Optional. Link anchor format.
1824 * @param string $link Optional. Link permalink format.
1825 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1826 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1827 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1828 * @return string The link URL of the next post in relation to the current post.
1830 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1831 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1835 * Display next post link that is adjacent to the current post.
1838 * @see get_next_post_link()
1840 * @param string $format Optional. Link anchor format.
1841 * @param string $link Optional. Link permalink format.
1842 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1843 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1844 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1846 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1847 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1851 * Get adjacent post link.
1853 * Can be either next post link or previous.
1857 * @param string $format Link anchor format.
1858 * @param string $link Link permalink format.
1859 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1860 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1861 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1862 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1863 * @return string The link URL of the previous or next post in relation to the current post.
1865 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1866 if ( $previous && is_attachment() )
1867 $post = get_post( get_post()->post_parent );
1869 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1874 $title = $post->post_title;
1876 if ( empty( $post->post_title ) )
1877 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1879 /** This filter is documented in wp-includes/post-template.php */
1880 $title = apply_filters( 'the_title', $title, $post->ID );
1882 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1883 $rel = $previous ? 'prev' : 'next';
1885 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1886 $inlink = str_replace( '%title', $title, $link );
1887 $inlink = str_replace( '%date', $date, $inlink );
1888 $inlink = $string . $inlink . '</a>';
1890 $output = str_replace( '%link', $inlink, $format );
1893 $adjacent = $previous ? 'previous' : 'next';
1896 * Filter the adjacent post link.
1898 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1899 * of adjacency, 'next' or 'previous'.
1902 * @since 4.2.0 Added the `$adjacent` parameter.
1904 * @param string $output The adjacent post link.
1905 * @param string $format Link anchor format.
1906 * @param string $link Link permalink format.
1907 * @param WP_Post $post The adjacent post.
1908 * @param string $adjacent Whether the post is previous or next.
1910 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
1914 * Display adjacent post link.
1916 * Can be either next post link or previous.
1920 * @param string $format Link anchor format.
1921 * @param string $link Link permalink format.
1922 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
1923 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
1924 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1925 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1927 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1928 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
1932 * Retrieve links for page numbers.
1936 * @param int $pagenum Optional. Page ID.
1937 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
1938 * Otherwise, prepares the URL with esc_url_raw().
1939 * @return string The link URL for the given page number.
1941 function get_pagenum_link($pagenum = 1, $escape = true ) {
1944 $pagenum = (int) $pagenum;
1946 $request = remove_query_arg( 'paged' );
1948 $home_root = parse_url(home_url());
1949 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1950 $home_root = preg_quote( $home_root, '|' );
1952 $request = preg_replace('|^'. $home_root . '|i', '', $request);
1953 $request = preg_replace('|^/+|', '', $request);
1955 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1956 $base = trailingslashit( get_bloginfo( 'url' ) );
1958 if ( $pagenum > 1 ) {
1959 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1961 $result = $base . $request;
1964 $qs_regex = '|\?.*?$|';
1965 preg_match( $qs_regex, $request, $qs_match );
1967 if ( !empty( $qs_match[0] ) ) {
1968 $query_string = $qs_match[0];
1969 $request = preg_replace( $qs_regex, '', $request );
1974 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
1975 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
1976 $request = ltrim($request, '/');
1978 $base = trailingslashit( get_bloginfo( 'url' ) );
1980 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1981 $base .= $wp_rewrite->index . '/';
1983 if ( $pagenum > 1 ) {
1984 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
1987 $result = $base . $request . $query_string;
1991 * Filter the page number link for the current request.
1995 * @param string $result The page number link.
1997 $result = apply_filters( 'get_pagenum_link', $result );
2000 return esc_url( $result );
2002 return esc_url_raw( $result );
2006 * Retrieve next posts page link.
2008 * Backported from 2.1.3 to 2.0.10.
2012 * @param int $max_page Optional. Max pages.
2013 * @return string The link URL for next posts page.
2015 function get_next_posts_page_link($max_page = 0) {
2018 if ( !is_single() ) {
2021 $nextpage = intval($paged) + 1;
2022 if ( !$max_page || $max_page >= $nextpage )
2023 return get_pagenum_link($nextpage);
2028 * Display or return the next posts page link.
2032 * @param int $max_page Optional. Max pages.
2033 * @param boolean $echo Optional. Echo or return;
2034 * @return string The link URL for next posts page if `$echo = false`.
2036 function next_posts( $max_page = 0, $echo = true ) {
2037 $output = esc_url( get_next_posts_page_link( $max_page ) );
2046 * Return the next posts page link.
2050 * @param string $label Content for link text.
2051 * @param int $max_page Optional. Max pages.
2052 * @return string|null HTML-formatted next posts page link.
2054 function get_next_posts_link( $label = null, $max_page = 0 ) {
2055 global $paged, $wp_query;
2058 $max_page = $wp_query->max_num_pages;
2063 $nextpage = intval($paged) + 1;
2065 if ( null === $label )
2066 $label = __( 'Next Page »' );
2068 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2070 * Filter the anchor tag attributes for the next posts page link.
2074 * @param string $attributes Attributes for the anchor tag.
2076 $attr = apply_filters( 'next_posts_link_attributes', '' );
2078 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2083 * Display the next posts page link.
2087 * @param string $label Content for link text.
2088 * @param int $max_page Optional. Max pages.
2090 function next_posts_link( $label = null, $max_page = 0 ) {
2091 echo get_next_posts_link( $label, $max_page );
2095 * Retrieve previous posts page link.
2097 * Will only return string, if not on a single page or post.
2099 * Backported to 2.0.10 from 2.1.3.
2103 * @return string|null The link for the previous posts page.
2105 function get_previous_posts_page_link() {
2108 if ( !is_single() ) {
2109 $nextpage = intval($paged) - 1;
2110 if ( $nextpage < 1 )
2112 return get_pagenum_link($nextpage);
2117 * Display or return the previous posts page link.
2121 * @param boolean $echo Optional. Echo or return;
2122 * @return string The previous posts page link if `$echo = false`.
2124 function previous_posts( $echo = true ) {
2125 $output = esc_url( get_previous_posts_page_link() );
2134 * Return the previous posts page link.
2138 * @param string $label Optional. Previous page link text.
2139 * @return string|null HTML-formatted previous page link.
2141 function get_previous_posts_link( $label = null ) {
2144 if ( null === $label )
2145 $label = __( '« Previous Page' );
2147 if ( !is_single() && $paged > 1 ) {
2149 * Filter the anchor tag attributes for the previous posts page link.
2153 * @param string $attributes Attributes for the anchor tag.
2155 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2156 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2161 * Display the previous posts page link.
2165 * @param string $label Optional. Previous page link text.
2167 function previous_posts_link( $label = null ) {
2168 echo get_previous_posts_link( $label );
2172 * Return post pages link navigation for previous and next pages.
2176 * @param string|array $args Optional args.
2177 * @return string The posts link navigation.
2179 function get_posts_nav_link( $args = array() ) {
2184 if ( !is_singular() ) {
2186 'sep' => ' — ',
2187 'prelabel' => __('« Previous Page'),
2188 'nxtlabel' => __('Next Page »'),
2190 $args = wp_parse_args( $args, $defaults );
2192 $max_num_pages = $wp_query->max_num_pages;
2193 $paged = get_query_var('paged');
2195 //only have sep if there's both prev and next results
2196 if ($paged < 2 || $paged >= $max_num_pages) {
2200 if ( $max_num_pages > 1 ) {
2201 $return = get_previous_posts_link($args['prelabel']);
2202 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2203 $return .= get_next_posts_link($args['nxtlabel']);
2211 * Display post pages link navigation for previous and next pages.
2215 * @param string $sep Optional. Separator for posts navigation links.
2216 * @param string $prelabel Optional. Label for previous pages.
2217 * @param string $nxtlabel Optional Label for next pages.
2219 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2220 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2221 echo get_posts_nav_link($args);
2225 * Return navigation to next/previous post when applicable.
2229 * @param array $args {
2230 * Optional. Default post navigation arguments. Default empty array.
2232 * @type string $prev_text Anchor text to display in the previous post link. Default `%title`.
2233 * @type string $next_text Anchor text to display in the next post link. Default `%title`.
2234 * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2236 * @return string Markup for post links.
2238 function get_the_post_navigation( $args = array() ) {
2239 $args = wp_parse_args( $args, array(
2240 'prev_text' => '%title',
2241 'next_text' => '%title',
2242 'screen_reader_text' => __( 'Post navigation' ),
2246 $previous = get_previous_post_link( '<div class="nav-previous">%link</div>', $args['prev_text'] );
2247 $next = get_next_post_link( '<div class="nav-next">%link</div>', $args['next_text'] );
2249 // Only add markup if there's somewhere to navigate to.
2250 if ( $previous || $next ) {
2251 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2258 * Display navigation to next/previous post when applicable.
2262 * @param array $args Optional. See {@see get_the_post_navigation()} for available
2263 * arguments. Default empty array.
2265 function the_post_navigation( $args = array() ) {
2266 echo get_the_post_navigation( $args );
2270 * Return navigation to next/previous set of posts when applicable.
2274 * @global WP_Query $wp_query WordPress Query object.
2276 * @param array $args {
2277 * Optional. Default posts navigation arguments. Default empty array.
2279 * @type string $prev_text Anchor text to display in the previous posts link.
2280 * Default 'Older posts'.
2281 * @type string $next_text Anchor text to display in the next posts link.
2282 * Default 'Newer posts'.
2283 * @type string $screen_reader_text Screen reader text for nav element.
2284 * Default 'Posts navigation'.
2286 * @return string Markup for posts links.
2288 function get_the_posts_navigation( $args = array() ) {
2291 // Don't print empty markup if there's only one page.
2292 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2293 $args = wp_parse_args( $args, array(
2294 'prev_text' => __( 'Older posts' ),
2295 'next_text' => __( 'Newer posts' ),
2296 'screen_reader_text' => __( 'Posts navigation' ),
2299 $next_link = get_previous_posts_link( $args['next_text'] );
2300 $prev_link = get_next_posts_link( $args['prev_text'] );
2303 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2307 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2310 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2317 * Display navigation to next/previous set of posts when applicable.
2321 * @param array $args Optional. See {@see get_the_posts_navigation()} for available
2322 * arguments. Default empty array.
2324 function the_posts_navigation( $args = array() ) {
2325 echo get_the_posts_navigation( $args );
2329 * Return a paginated navigation to next/previous set of posts,
2334 * @param array $args {
2335 * Optional. Default pagination arguments, {@see paginate_links()}.
2337 * @type string $screen_reader_text Screen reader text for navigation element.
2338 * Default 'Posts navigation'.
2340 * @return string Markup for pagination links.
2342 function get_the_posts_pagination( $args = array() ) {
2345 // Don't print empty markup if there's only one page.
2346 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2347 $args = wp_parse_args( $args, array(
2349 'prev_text' => _x( 'Previous', 'previous post' ),
2350 'next_text' => _x( 'Next', 'next post' ),
2351 'screen_reader_text' => __( 'Posts navigation' ),
2354 // Make sure we get a string back. Plain is the next best thing.
2355 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2356 $args['type'] = 'plain';
2359 // Set up paginated links.
2360 $links = paginate_links( $args );
2363 $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2371 * Display a paginated navigation to next/previous set of posts,
2376 * @param array $args Optional. See {@see get_the_posts_pagination()} for available arguments.
2377 * Default empty array.
2379 function the_posts_pagination( $args = array() ) {
2380 echo get_the_posts_pagination( $args );
2384 * Wraps passed links in navigational markup.
2389 * @param string $links Navigational links.
2390 * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'.
2391 * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2392 * @return string Navigation template tag.
2394 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2395 if ( empty( $screen_reader_text ) ) {
2396 $screen_reader_text = __( 'Posts navigation' );
2400 <nav class="navigation %1$s" role="navigation">
2401 <h2 class="screen-reader-text">%2$s</h2>
2402 <div class="nav-links">%3$s</div>
2405 return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2409 * Retrieve comments page number link.
2413 * @param int $pagenum Optional. Page number.
2414 * @param int $max_page Optional. The maximum number of comment pages.
2415 * @return string The comments page number link URL.
2417 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2420 $pagenum = (int) $pagenum;
2422 $result = get_permalink();
2424 if ( 'newest' == get_option('default_comments_page') ) {
2425 if ( $pagenum != $max_page ) {
2426 if ( $wp_rewrite->using_permalinks() )
2427 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2429 $result = add_query_arg( 'cpage', $pagenum, $result );
2431 } elseif ( $pagenum > 1 ) {
2432 if ( $wp_rewrite->using_permalinks() )
2433 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2435 $result = add_query_arg( 'cpage', $pagenum, $result );
2438 $result .= '#comments';
2441 * Filter the comments page number link for the current request.
2445 * @param string $result The comments page number link.
2447 $result = apply_filters( 'get_comments_pagenum_link', $result );
2453 * Return the link to next comments page.
2457 * @param string $label Optional. Label for link text.
2458 * @param int $max_page Optional. Max page.
2459 * @return string|null HTML-formatted link for the next page of comments.
2461 function get_next_comments_link( $label = '', $max_page = 0 ) {
2464 if ( !is_singular() || !get_option('page_comments') )
2467 $page = get_query_var('cpage');
2473 $nextpage = intval($page) + 1;
2475 if ( empty($max_page) )
2476 $max_page = $wp_query->max_num_comment_pages;
2478 if ( empty($max_page) )
2479 $max_page = get_comment_pages_count();
2481 if ( $nextpage > $max_page )
2484 if ( empty($label) )
2485 $label = __('Newer Comments »');
2488 * Filter the anchor tag attributes for the next comments page link.
2492 * @param string $attributes Attributes for the anchor tag.
2494 return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
2498 * Display the link to next comments page.
2502 * @param string $label Optional. Label for link text.
2503 * @param int $max_page Optional. Max page.
2505 function next_comments_link( $label = '', $max_page = 0 ) {
2506 echo get_next_comments_link( $label, $max_page );
2510 * Return the previous comments page link.
2514 * @param string $label Optional. Label for comments link text.
2515 * @return string|null HTML-formatted link for the previous page of comments.
2517 function get_previous_comments_link( $label = '' ) {
2518 if ( !is_singular() || !get_option('page_comments') )
2521 $page = get_query_var('cpage');
2523 if ( intval($page) <= 1 )
2526 $prevpage = intval($page) - 1;
2528 if ( empty($label) )
2529 $label = __('« Older Comments');
2532 * Filter the anchor tag attributes for the previous comments page link.
2536 * @param string $attributes Attributes for the anchor tag.
2538 return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
2542 * Display the previous comments page link.
2546 * @param string $label Optional. Label for comments link text.
2548 function previous_comments_link( $label = '' ) {
2549 echo get_previous_comments_link( $label );
2553 * Create pagination links for the comments on the current post.
2555 * @see paginate_links()
2558 * @param string|array $args Optional args. See paginate_links().
2559 * @return string Markup for pagination links.
2561 function paginate_comments_links($args = array()) {
2564 if ( !is_singular() || !get_option('page_comments') )
2567 $page = get_query_var('cpage');
2570 $max_page = get_comment_pages_count();
2572 'base' => add_query_arg( 'cpage', '%#%' ),
2574 'total' => $max_page,
2577 'add_fragment' => '#comments'
2579 if ( $wp_rewrite->using_permalinks() )
2580 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2582 $args = wp_parse_args( $args, $defaults );
2583 $page_links = paginate_links( $args );
2585 if ( $args['echo'] )
2592 * Retrieve the Press This bookmarklet link.
2594 * Use this in 'a' element 'href' attribute.
2598 * @return string The Press This bookmarklet link URL.
2600 function get_shortcut_link() {
2601 global $is_IE, $wp_version;
2603 include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2604 $bookmarklet_version = $GLOBALS['wp_press_this']->version;
2609 * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2610 * since they only support a max length of ~2000 characters for
2611 * bookmark[let] URLs, which is way to small for our smarter one.
2612 * Do update the version number so users do not get the "upgrade your
2613 * bookmarklet" notice when using PT in those browsers.
2615 $ua = $_SERVER['HTTP_USER_AGENT'];
2617 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2618 $url = wp_json_encode( admin_url( 'press-this.php' ) );
2620 $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2621 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2622 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' .
2623 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2624 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2628 if ( empty( $link ) ) {
2629 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2632 $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version );
2633 $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2637 $link = str_replace( array( "\r", "\n", "\t" ), '', $link );
2640 * Filter the Press This bookmarklet link.
2644 * @param string $link The Press This bookmarklet link.
2646 return apply_filters( 'shortcut_link', $link );
2650 * Retrieve the home url for the current site.
2652 * Returns the 'home' option with the appropriate protocol, 'https' if
2653 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2654 * `is_ssl()` is overridden.
2658 * @param string $path Optional. Path relative to the home url. Default empty.
2659 * @param string $scheme Optional. Scheme to give the home url context. Accepts
2660 * 'http', 'https', or 'relative'. Default null.
2661 * @return string Home url link with optional path appended.
2663 function home_url( $path = '', $scheme = null ) {
2664 return get_home_url( null, $path, $scheme );
2668 * Retrieve the home url for a given site.
2670 * Returns the 'home' option with the appropriate protocol, 'https' if
2671 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2677 * @param int $blog_id Optional. Blog ID. Default null (current blog).
2678 * @param string $path Optional. Path relative to the home URL. Default empty.
2679 * @param string|null $orig_scheme Optional. Scheme to give the home URL context. Accepts
2680 * 'http', 'https', 'relative', or null. Default null.
2681 * @return string Home URL link with optional path appended.
2683 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2684 $orig_scheme = $scheme;
2686 if ( empty( $blog_id ) || !is_multisite() ) {
2687 $url = get_option( 'home' );
2689 switch_to_blog( $blog_id );
2690 $url = get_option( 'home' );
2691 restore_current_blog();
2694 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2695 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
2698 $scheme = parse_url( $url, PHP_URL_SCHEME );
2701 $url = set_url_scheme( $url, $scheme );
2703 if ( $path && is_string( $path ) )
2704 $url .= '/' . ltrim( $path, '/' );
2707 * Filter the home URL.
2711 * @param string $url The complete home URL including scheme and path.
2712 * @param string $path Path relative to the home URL. Blank string if no path is specified.
2713 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
2714 * @param int|null $blog_id Blog ID, or null for the current blog.
2716 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
2720 * Retrieve the site url for the current site.
2722 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2723 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2728 * @param string $path Optional. Path relative to the site url.
2729 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2730 * @return string Site url link with optional path appended.
2732 function site_url( $path = '', $scheme = null ) {
2733 return get_site_url( null, $path, $scheme );
2737 * Retrieve the site url for a given site.
2739 * Returns the 'site_url' option with the appropriate protocol, 'https' if
2740 * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2741 * `is_ssl()` is overridden.
2745 * @param int $blog_id Optional. Blog ID. Default null (current site).
2746 * @param string $path Optional. Path relative to the site url. Default empty.
2747 * @param string $scheme Optional. Scheme to give the site url context. Accepts
2748 * 'http', 'https', 'login', 'login_post', 'admin', or
2749 * 'relative'. Default null.
2750 * @return string Site url link with optional path appended.
2752 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2753 if ( empty( $blog_id ) || !is_multisite() ) {
2754 $url = get_option( 'siteurl' );
2756 switch_to_blog( $blog_id );
2757 $url = get_option( 'siteurl' );
2758 restore_current_blog();
2761 $url = set_url_scheme( $url, $scheme );
2763 if ( $path && is_string( $path ) )
2764 $url .= '/' . ltrim( $path, '/' );
2767 * Filter the site URL.
2771 * @param string $url The complete site URL including scheme and path.
2772 * @param string $path Path relative to the site URL. Blank string if no path is specified.
2773 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
2774 * 'login_post', 'admin', 'relative' or null.
2775 * @param int|null $blog_id Blog ID, or null for the current blog.
2777 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
2781 * Retrieve the url to the admin area for the current site.
2785 * @param string $path Optional path relative to the admin url.
2786 * @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.
2787 * @return string Admin url link with optional path appended.
2789 function admin_url( $path = '', $scheme = 'admin' ) {
2790 return get_admin_url( null, $path, $scheme );
2794 * Retrieves the url to the admin area for a given site.
2798 * @param int $blog_id Optional. Blog ID. Default null (current site).
2799 * @param string $path Optional. Path relative to the admin url. Default empty.
2800 * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
2801 * to force those schemes. Default 'admin', which obeys
2802 * {@see force_ssl_admin()} and {@see is_ssl()}.
2803 * @return string Admin url link with optional path appended.
2805 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2806 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2808 if ( $path && is_string( $path ) )
2809 $url .= ltrim( $path, '/' );
2812 * Filter the admin area URL.
2816 * @param string $url The complete admin area URL including scheme and path.
2817 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
2818 * @param int|null $blog_id Blog ID, or null for the current blog.
2820 return apply_filters( 'admin_url', $url, $path, $blog_id );
2824 * Retrieve the url to the includes directory.
2828 * @param string $path Optional. Path relative to the includes url.
2829 * @param string $scheme Optional. Scheme to give the includes url context.
2830 * @return string Includes url link with optional path appended.
2832 function includes_url( $path = '', $scheme = null ) {
2833 $url = site_url( '/' . WPINC . '/', $scheme );
2835 if ( $path && is_string( $path ) )
2836 $url .= ltrim($path, '/');
2839 * Filter the URL to the includes directory.
2843 * @param string $url The complete URL to the includes directory including scheme and path.
2844 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
2845 * if no path is specified.
2847 return apply_filters( 'includes_url', $url, $path );
2851 * Retrieve the url to the content directory.
2855 * @param string $path Optional. Path relative to the content url.
2856 * @return string Content url link with optional path appended.
2858 function content_url($path = '') {
2859 $url = set_url_scheme( WP_CONTENT_URL );
2861 if ( $path && is_string( $path ) )
2862 $url .= '/' . ltrim($path, '/');
2865 * Filter the URL to the content directory.
2869 * @param string $url The complete URL to the content directory including scheme and path.
2870 * @param string $path Path relative to the URL to the content directory. Blank string
2871 * if no path is specified.
2873 return apply_filters( 'content_url', $url, $path);
2877 * Retrieve a URL within the plugins or mu-plugins directory.
2879 * Defaults to the plugins directory URL if no arguments are supplied.
2883 * @param string $path Optional. Extra path appended to the end of the URL, including
2884 * the relative directory if $plugin is supplied. Default empty.
2885 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
2886 * The URL will be relative to its directory. Default empty.
2887 * Typically this is done by passing `__FILE__` as the argument.
2888 * @return string Plugins URL link with optional paths appended.
2890 function plugins_url( $path = '', $plugin = '' ) {
2892 $path = wp_normalize_path( $path );
2893 $plugin = wp_normalize_path( $plugin );
2894 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
2896 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2897 $url = WPMU_PLUGIN_URL;
2899 $url = WP_PLUGIN_URL;
2902 $url = set_url_scheme( $url );
2904 if ( !empty($plugin) && is_string($plugin) ) {
2905 $folder = dirname(plugin_basename($plugin));
2906 if ( '.' != $folder )
2907 $url .= '/' . ltrim($folder, '/');
2910 if ( $path && is_string( $path ) )
2911 $url .= '/' . ltrim($path, '/');
2914 * Filter the URL to the plugins directory.
2918 * @param string $url The complete URL to the plugins directory including scheme and path.
2919 * @param string $path Path relative to the URL to the plugins directory. Blank string
2920 * if no path is specified.
2921 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
2924 return apply_filters( 'plugins_url', $url, $path, $plugin );
2928 * Retrieve the site url for the current network.
2930 * Returns the site url with the appropriate protocol, 'https' if
2931 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2936 * @param string $path Optional. Path relative to the site url.
2937 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2938 * @return string Site url link with optional path appended.
2940 function network_site_url( $path = '', $scheme = null ) {
2941 if ( ! is_multisite() )
2942 return site_url($path, $scheme);
2944 $current_site = get_current_site();
2946 if ( 'relative' == $scheme )
2947 $url = $current_site->path;
2949 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2951 if ( $path && is_string( $path ) )
2952 $url .= ltrim( $path, '/' );
2955 * Filter the network site URL.
2959 * @param string $url The complete network site URL including scheme and path.
2960 * @param string $path Path relative to the network site URL. Blank string if
2961 * no path is specified.
2962 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
2963 * 'relative' or null.
2965 return apply_filters( 'network_site_url', $url, $path, $scheme );
2969 * Retrieves the home url for the current network.
2971 * Returns the home url with the appropriate protocol, 'https' {@see is_ssl()}
2972 * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
2977 * @param string $path Optional. Path relative to the home url. Default empty.
2978 * @param string $scheme Optional. Scheme to give the home url context. Accepts
2979 * 'http', 'https', or 'relative'. Default null.
2980 * @return string Home url link with optional path appended.
2982 function network_home_url( $path = '', $scheme = null ) {
2983 if ( ! is_multisite() )
2984 return home_url($path, $scheme);
2986 $current_site = get_current_site();
2987 $orig_scheme = $scheme;
2989 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
2990 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
2992 if ( 'relative' == $scheme )
2993 $url = $current_site->path;
2995 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2997 if ( $path && is_string( $path ) )
2998 $url .= ltrim( $path, '/' );
3001 * Filter the network home URL.
3005 * @param string $url The complete network home URL including scheme and path.
3006 * @param string $path Path relative to the network home URL. Blank string
3007 * if no path is specified.
3008 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3009 * 'relative' or null.
3011 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3015 * Retrieve the url to the admin area for the network.
3019 * @param string $path Optional path relative to the admin url.
3020 * @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.
3021 * @return string Admin url link with optional path appended.
3023 function network_admin_url( $path = '', $scheme = 'admin' ) {
3024 if ( ! is_multisite() )
3025 return admin_url( $path, $scheme );
3027 $url = network_site_url('wp-admin/network/', $scheme);
3029 if ( $path && is_string( $path ) )
3030 $url .= ltrim($path, '/');
3033 * Filter the network admin URL.
3037 * @param string $url The complete network admin URL including scheme and path.
3038 * @param string $path Path relative to the network admin URL. Blank string if
3039 * no path is specified.
3041 return apply_filters( 'network_admin_url', $url, $path );
3045 * Retrieve the url to the admin area for the current user.
3049 * @param string $path Optional path relative to the admin url.
3050 * @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.
3051 * @return string Admin url link with optional path appended.
3053 function user_admin_url( $path = '', $scheme = 'admin' ) {
3054 $url = network_site_url('wp-admin/user/', $scheme);
3056 if ( $path && is_string( $path ) )
3057 $url .= ltrim($path, '/');
3060 * Filter the user admin URL for the current user.
3064 * @param string $url The complete URL including scheme and path.
3065 * @param string $path Path relative to the URL. Blank string if
3066 * no path is specified.
3068 return apply_filters( 'user_admin_url', $url, $path );
3072 * Retrieve the url to the admin area for either the current blog or the network depending on context.
3076 * @param string $path Optional path relative to the admin url.
3077 * @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.
3078 * @return string Admin url link with optional path appended.
3080 function self_admin_url($path = '', $scheme = 'admin') {
3081 if ( is_network_admin() )
3082 return network_admin_url($path, $scheme);
3083 elseif ( is_user_admin() )
3084 return user_admin_url($path, $scheme);
3086 return admin_url($path, $scheme);
3090 * Set the scheme for a URL
3094 * @param string $url Absolute url that includes a scheme
3095 * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
3096 * @return string $url URL with chosen scheme.
3098 function set_url_scheme( $url, $scheme = null ) {
3099 $orig_scheme = $scheme;
3102 $scheme = is_ssl() ? 'https' : 'http';
3103 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3104 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3105 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3106 $scheme = is_ssl() ? 'https' : 'http';
3109 $url = trim( $url );
3110 if ( substr( $url, 0, 2 ) === '//' )
3111 $url = 'http:' . $url;
3113 if ( 'relative' == $scheme ) {
3114 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3115 if ( $url !== '' && $url[0] === '/' )
3116 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3118 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3122 * Filter the resulting URL after setting the scheme.
3126 * @param string $url The complete URL including scheme and path.
3127 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3128 * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3129 * 'login_post', 'admin', 'rpc', or 'relative'.
3131 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3135 * Get the URL to the user's dashboard.
3137 * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
3138 * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
3139 * primary blog is returned.
3143 * @param int $user_id Optional. User ID. Defaults to current user.
3144 * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
3145 * @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.
3146 * @return string Dashboard url link with optional path appended.
3148 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3149 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3151 $blogs = get_blogs_of_user( $user_id );
3152 if ( ! is_super_admin() && empty($blogs) ) {
3153 $url = user_admin_url( $path, $scheme );
3154 } elseif ( ! is_multisite() ) {
3155 $url = admin_url( $path, $scheme );
3157 $current_blog = get_current_blog_id();
3158 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3159 $url = admin_url( $path, $scheme );
3161 $active = get_active_blog_for_user( $user_id );
3163 $url = get_admin_url( $active->blog_id, $path, $scheme );
3165 $url = user_admin_url( $path, $scheme );
3170 * Filter the dashboard URL for a user.
3174 * @param string $url The complete URL including scheme and path.
3175 * @param int $user_id The user ID.
3176 * @param string $path Path relative to the URL. Blank string if no path is specified.
3177 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3178 * 'login_post', 'admin', 'relative' or null.
3180 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3184 * Get the URL to the user's profile editor.
3188 * @param int $user_id Optional. User ID. Defaults to current user.
3189 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3190 * 'http' or 'https' can be passed to force those schemes.
3191 * @return string Dashboard url link with optional path appended.
3193 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3194 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3196 if ( is_user_admin() )
3197 $url = user_admin_url( 'profile.php', $scheme );
3198 elseif ( is_network_admin() )
3199 $url = network_admin_url( 'profile.php', $scheme );
3201 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3204 * Filter the URL for a user's profile editor.
3208 * @param string $url The complete URL including scheme and path.
3209 * @param int $user_id The user ID.
3210 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3211 * 'login_post', 'admin', 'relative' or null.
3213 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3217 * Output rel=canonical for singular queries.
3221 function rel_canonical() {
3222 if ( !is_singular() )
3225 global $wp_the_query;
3226 if ( !$id = $wp_the_query->get_queried_object_id() )
3229 $link = get_permalink( $id );
3231 if ( $page = get_query_var('cpage') )
3232 $link = get_comments_pagenum_link( $page );
3234 echo "<link rel='canonical' href='$link' />\n";
3238 * Return a shortlink for a post, page, attachment, or blog.
3240 * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
3241 * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
3242 * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
3243 * via the get_shortlink filter.
3247 * @param int $id A post or blog id. Default is 0, which means the current post or blog.
3248 * @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'.
3249 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3250 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3252 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3254 * Filter whether to preempt generating a shortlink for the given post.
3256 * Passing a truthy value to the filter will effectively short-circuit the
3257 * shortlink-generation process, returning that value instead.
3261 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3262 * @param int $id Post ID, or 0 for the current post.
3263 * @param string $context The context for the link. One of 'post' or 'query',
3264 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3266 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3268 if ( false !== $shortlink )
3273 if ( 'query' == $context && is_singular() ) {
3274 $post_id = $wp_query->get_queried_object_id();
3275 $post = get_post( $post_id );
3276 } elseif ( 'post' == $context ) {
3277 $post = get_post( $id );
3278 if ( ! empty( $post->ID ) )
3279 $post_id = $post->ID;
3284 // Return p= link for all public post types.
3285 if ( ! empty( $post_id ) ) {
3286 $post_type = get_post_type_object( $post->post_type );
3288 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3289 $shortlink = home_url( '/' );
3290 } elseif ( $post_type->public ) {
3291 $shortlink = home_url( '?p=' . $post_id );
3296 * Filter the shortlink for a post.
3300 * @param string $shortlink Shortlink URL.
3301 * @param int $id Post ID, or 0 for the current post.
3302 * @param string $context The context for the link. One of 'post' or 'query',
3303 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3305 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3309 * Inject rel=shortlink into head if a shortlink is defined for the current page.
3311 * Attached to the wp_head action.
3315 function wp_shortlink_wp_head() {
3316 $shortlink = wp_get_shortlink( 0, 'query' );
3318 if ( empty( $shortlink ) )
3321 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3325 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3327 * Attached to the wp action.
3331 function wp_shortlink_header() {
3332 if ( headers_sent() )
3335 $shortlink = wp_get_shortlink(0, 'query');
3337 if ( empty($shortlink) )
3340 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3344 * Display the Short Link for a Post
3346 * Must be called from inside "The Loop"
3348 * Call like the_shortlink(__('Shortlinkage FTW'))
3352 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3353 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3354 * @param string $before Optional HTML to display before the link.
3355 * @param string $after Optional HTML to display after the link.
3357 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3360 if ( empty( $text ) )
3361 $text = __('This is the short link.');
3363 if ( empty( $title ) )
3364 $title = the_title_attribute( array( 'echo' => false ) );
3366 $shortlink = wp_get_shortlink( $post->ID );
3368 if ( !empty( $shortlink ) ) {
3369 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3372 * Filter the shortlink anchor tag for a post.
3376 * @param string $link Shortlink anchor tag.
3377 * @param string $shortlink Shortlink URL.
3378 * @param string $text Shortlink's text.
3379 * @param string $title Shortlink's title attribute.
3381 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3382 echo $before, $link, $after;
3388 * Retrieve the avatar URL.
3392 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3393 * user email, WP_User object, WP_Post object, or comment object.
3394 * @param array $args {
3395 * Optional. Arguments to return instead of the default arguments.
3397 * @type int $size Height and width of the avatar in pixels. Default 96.
3398 * @type string $default URL for the default image or a default type. Accepts '404' (return
3399 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3400 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3401 * or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
3402 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3403 * 'avatar_default' option, with a fallback of 'mystery'.
3404 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3405 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3406 * judged in that order. Default is the value of the 'avatar_rating' option.
3407 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3409 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3410 * plus a "found_avatar" guess. Pass as a reference. Default null.
3412 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3414 function get_avatar_url( $id_or_email, $args = null ) {
3415 $args = get_avatar_data( $id_or_email, $args );
3416 return $args['url'];
3420 * Retrieve default data about the avatar.
3424 * @param mixed $id_or_email The Gravatar to check the data against. Accepts a user_id, gravatar md5 hash,
3425 * user email, WP_User object, WP_Post object, or comment object.
3426 * @param array $args {
3427 * Optional. Arguments to return instead of the default arguments.
3429 * @type int $size Height and width of the avatar image file in pixels. Default 96.
3430 * @type int $height Display height of the avatar in pixels. Defaults to $size.
3431 * @type int $width Display width of the avatar in pixels. Defaults to $size.
3432 * @type string $default URL for the default image or a default type. Accepts '404' (return
3433 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3434 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3435 * or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
3436 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3437 * 'avatar_default' option, with a fallback of 'mystery'.
3438 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3439 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3440 * judged in that order. Default is the value of the 'avatar_rating' option.
3441 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3443 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3444 * plus a "found_avatar" guess. Pass as a reference. Default null.
3445 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3447 * @return array $processed_args {
3448 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3450 * @type bool $found_avatar True if we were able to find an avatar for this user,
3451 * false or not set if we couldn't.
3452 * @type string $url The URL of the avatar we found.
3455 function get_avatar_data( $id_or_email, $args = null ) {
3456 $args = wp_parse_args( $args, array(
3460 'default' => get_option( 'avatar_default', 'mystery' ),
3461 'force_default' => false,
3462 'rating' => get_option( 'avatar_rating' ),
3464 'processed_args' => null, // if used, should be a reference
3468 if ( is_numeric( $args['size'] ) ) {
3469 $args['size'] = absint( $args['size'] );
3470 if ( ! $args['size'] ) {
3477 if ( is_numeric( $args['height'] ) ) {
3478 $args['height'] = absint( $args['height'] );
3479 if ( ! $args['height'] ) {
3480 $args['height'] = $args['size'];
3483 $args['height'] = $args['size'];
3486 if ( is_numeric( $args['width'] ) ) {
3487 $args['width'] = absint( $args['width'] );
3488 if ( ! $args['width'] ) {
3489 $args['width'] = $args['size'];
3492 $args['width'] = $args['size'];
3495 if ( empty( $args['default'] ) ) {
3496 $args['default'] = get_option( 'avatar_default', 'mystery' );
3499 switch ( $args['default'] ) {
3503 $args['default'] = 'mm';
3505 case 'gravatar_default' :
3506 $args['default'] = false;
3510 $args['force_default'] = (bool) $args['force_default'];
3512 $args['rating'] = strtolower( $args['rating'] );
3514 $args['found_avatar'] = false;
3517 * Filter whether to retrieve the avatar URL early.
3519 * Passing a non-null value in the 'url' member of the return array will
3520 * effectively short circuit get_avatar_data(), passing the value through
3521 * the {@see 'get_avatar_data'} filter and returning early.
3525 * @param array $args Arguments passed to get_avatar_data(), after processing.
3526 * @param int|object|string $id_or_email A user ID, email address, or comment object.
3528 $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3530 if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3531 /** This filter is documented in wp-includes/link-template.php */
3532 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3536 $user = $email = false;
3538 // Process the user identifier.
3539 if ( is_numeric( $id_or_email ) ) {
3540 $user = get_user_by( 'id', absint( $id_or_email ) );
3541 } elseif ( is_string( $id_or_email ) ) {
3542 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3544 list( $email_hash ) = explode( '@', $id_or_email );
3547 $email = $id_or_email;
3549 } elseif ( $id_or_email instanceof WP_User ) {
3551 $user = $id_or_email;
3552 } elseif ( $id_or_email instanceof WP_Post ) {
3554 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3555 } elseif ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3559 * Filter the list of allowed comment types for retrieving avatars.
3563 * @param array $types An array of content types. Default only contains 'comment'.
3565 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3566 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3567 $args['url'] = false;
3568 /** This filter is documented in wp-includes/link-template.php */
3569 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3572 if ( ! empty( $id_or_email->user_id ) ) {
3573 $user = get_user_by( 'id', (int) $id_or_email->user_id );
3575 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3576 $email = $id_or_email->comment_author_email;
3580 if ( ! $email_hash ) {
3582 $email = $user->user_email;
3586 $email_hash = md5( strtolower( trim( $email ) ) );
3590 if ( $email_hash ) {
3591 $args['found_avatar'] = true;
3592 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3594 $gravatar_server = rand( 0, 2 );
3598 's' => $args['size'],
3599 'd' => $args['default'],
3600 'f' => $args['force_default'] ? 'y' : false,
3601 'r' => $args['rating'],
3604 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
3606 $url = add_query_arg(
3607 rawurlencode_deep( array_filter( $url_args ) ),
3608 set_url_scheme( $url, $args['scheme'] )
3612 * Filter the avatar URL.
3616 * @param string $url The URL of the avatar.
3617 * @param int|object|string $id_or_email A user ID, email address, or comment object.
3618 * @param array $args Arguments passed to get_avatar_data(), after processing.
3620 $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
3623 * Filter the avatar data.
3627 * @param array $args Arguments passed to get_avatar_data(), after processing.
3628 * @param int|object|string $id_or_email A user ID, email address, or comment object.
3630 return apply_filters( 'get_avatar_data', $args, $id_or_email );