3 * WordPress Link Template Functions
10 * Displays the permalink for the current post.
13 * @since 4.4.0 Added the `$post` parameter.
15 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
17 function the_permalink( $post = 0 ) {
19 * Filters the display of the permalink for the current post.
22 * @since 4.4.0 Added the `$post` parameter.
24 * @param string $permalink The permalink for the current post.
25 * @param int|WP_Post $post Post ID, WP_Post object, or 0. Default 0.
27 echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
31 * Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
33 * Conditionally adds a trailing slash if the permalink structure has a trailing
34 * slash, strips the trailing slash if not. The string is passed through the
35 * {@see 'user_trailingslashit'} filter. Will remove trailing slash from string, if
36 * site is not set to have them.
40 * @global WP_Rewrite $wp_rewrite
42 * @param string $string URL with or without a trailing slash.
43 * @param string $type_of_url Optional. The type of URL being considered (e.g. single, category, etc)
44 * for use in the filter. Default empty string.
45 * @return string The URL with the trailing slash appended or stripped.
47 function user_trailingslashit($string, $type_of_url = '') {
49 if ( $wp_rewrite->use_trailing_slashes )
50 $string = trailingslashit($string);
52 $string = untrailingslashit($string);
55 * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes.
59 * @param string $string URL with or without a trailing slash.
60 * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
61 * 'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed',
62 * 'category', 'page', 'year', 'month', 'day', 'post_type_archive'.
64 return apply_filters( 'user_trailingslashit', $string, $type_of_url );
68 * Displays the permalink anchor for the current post.
70 * The permalink mode title will use the post title for the 'a' element 'id'
71 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
75 * @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'.
77 function permalink_anchor( $mode = 'id' ) {
79 switch ( strtolower( $mode ) ) {
81 $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
82 echo '<a id="'.$title.'"></a>';
86 echo '<a id="post-' . $post->ID . '"></a>';
92 * Retrieves the full permalink for the current post or post ID.
94 * This function is an alias for get_permalink().
98 * @see get_permalink()
100 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
101 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
103 * @return string|false The permalink URL or false if post does not exist.
105 function get_the_permalink( $post = 0, $leavename = false ) {
106 return get_permalink( $post, $leavename );
110 * Retrieves the full permalink for the current post or post ID.
114 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
115 * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
116 * @return string|false The permalink URL or false if post does not exist.
118 function get_permalink( $post = 0, $leavename = false ) {
119 $rewritecode = array(
126 $leavename? '' : '%postname%',
130 $leavename? '' : '%pagename%',
133 if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) {
136 $post = get_post( $post );
140 if ( empty($post->ID) )
143 if ( $post->post_type == 'page' )
144 return get_page_link($post, $leavename, $sample);
145 elseif ( $post->post_type == 'attachment' )
146 return get_attachment_link( $post, $leavename );
147 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
148 return get_post_permalink($post, $leavename, $sample);
150 $permalink = get_option('permalink_structure');
153 * Filters the permalink structure for a post before token replacement occurs.
155 * Only applies to posts with post_type of 'post'.
159 * @param string $permalink The site's permalink structure.
160 * @param WP_Post $post The post in question.
161 * @param bool $leavename Whether to keep the post name.
163 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
165 if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
166 $unixtime = strtotime($post->post_date);
169 if ( strpos($permalink, '%category%') !== false ) {
170 $cats = get_the_category($post->ID);
172 $cats = wp_list_sort( $cats, array(
177 * Filters the category that gets used in the %category% permalink token.
181 * @param WP_Term $cat The category to use in the permalink.
182 * @param array $cats Array of all categories (WP_Term objects) associated with the post.
183 * @param WP_Post $post The post in question.
185 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
187 $category_object = get_term( $category_object, 'category' );
188 $category = $category_object->slug;
189 if ( $parent = $category_object->parent )
190 $category = get_category_parents($parent, false, '/', true) . $category;
192 // show default category in permalinks, without
193 // having to assign it explicitly
194 if ( empty($category) ) {
195 $default_category = get_term( get_option( 'default_category' ), 'category' );
196 if ( $default_category && ! is_wp_error( $default_category ) ) {
197 $category = $default_category->slug;
203 if ( strpos($permalink, '%author%') !== false ) {
204 $authordata = get_userdata($post->post_author);
205 $author = $authordata->user_nicename;
208 $date = explode(" ",date('Y m d H i s', $unixtime));
223 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
224 $permalink = user_trailingslashit($permalink, 'single');
225 } else { // if they're not using the fancy permalink option
226 $permalink = home_url('?p=' . $post->ID);
230 * Filters the permalink for a post.
232 * Only applies to posts with post_type of 'post'.
236 * @param string $permalink The post's permalink.
237 * @param WP_Post $post The post in question.
238 * @param bool $leavename Whether to keep the post name.
240 return apply_filters( 'post_link', $permalink, $post, $leavename );
244 * Retrieves the permalink for a post of a custom post type.
248 * @global WP_Rewrite $wp_rewrite
250 * @param int $id Optional. Post ID. Default uses the global `$post`.
251 * @param bool $leavename Optional, defaults to false. Whether to keep post name. Default false.
252 * @param bool $sample Optional, defaults to false. Is it a sample permalink. Default false.
253 * @return string|WP_Error The post permalink.
255 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
258 $post = get_post($id);
260 if ( is_wp_error( $post ) )
263 $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
265 $slug = $post->post_name;
267 $draft_or_pending = get_post_status( $id ) && in_array( get_post_status( $id ), array( 'draft', 'pending', 'auto-draft', 'future' ) );
269 $post_type = get_post_type_object($post->post_type);
271 if ( $post_type->hierarchical ) {
272 $slug = get_page_uri( $id );
275 if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
276 if ( ! $leavename ) {
277 $post_link = str_replace("%$post->post_type%", $slug, $post_link);
279 $post_link = home_url( user_trailingslashit($post_link) );
281 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
282 $post_link = add_query_arg($post_type->query_var, $slug, '');
284 $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
285 $post_link = home_url($post_link);
289 * Filters the permalink for a post of a custom post type.
293 * @param string $post_link The post's permalink.
294 * @param WP_Post $post The post in question.
295 * @param bool $leavename Whether to keep the post name.
296 * @param bool $sample Is it a sample permalink.
298 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
302 * Retrieves the permalink for the current page or page ID.
304 * Respects page_on_front. Use this one.
308 * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
309 * @param bool $leavename Optional. Whether to keep the page name. Default false.
310 * @param bool $sample Optional. Whether it should be treated as a sample permalink.
312 * @return string The page permalink.
314 function get_page_link( $post = false, $leavename = false, $sample = false ) {
315 $post = get_post( $post );
317 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
318 $link = home_url('/');
320 $link = _get_page_link( $post, $leavename, $sample );
323 * Filters the permalink for a page.
327 * @param string $link The page's permalink.
328 * @param int $post_id The ID of the page.
329 * @param bool $sample Is it a sample permalink.
331 return apply_filters( 'page_link', $link, $post->ID, $sample );
335 * Retrieves the page permalink.
337 * Ignores page_on_front. Internal use only.
342 * @global WP_Rewrite $wp_rewrite
344 * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
345 * @param bool $leavename Optional. Whether to keep the page name. Default false.
346 * @param bool $sample Optional. Whether it should be treated as a sample permalink.
348 * @return string The page permalink.
350 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
353 $post = get_post( $post );
355 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
357 $link = $wp_rewrite->get_page_permastruct();
359 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
360 if ( ! $leavename ) {
361 $link = str_replace('%pagename%', get_page_uri( $post ), $link);
364 $link = home_url($link);
365 $link = user_trailingslashit($link, 'page');
367 $link = home_url( '?page_id=' . $post->ID );
371 * Filters the permalink for a non-page_on_front page.
375 * @param string $link The page's permalink.
376 * @param int $post_id The ID of the page.
378 return apply_filters( '_get_page_link', $link, $post->ID );
382 * Retrieves the permalink for an attachment.
384 * This can be used in the WordPress Loop or outside of it.
388 * @global WP_Rewrite $wp_rewrite
390 * @param int|object $post Optional. Post ID or object. Default uses the global `$post`.
391 * @param bool $leavename Optional. Whether to keep the page name. Default false.
392 * @return string The attachment permalink.
394 function get_attachment_link( $post = null, $leavename = false ) {
399 $post = get_post( $post );
400 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
401 if ( $parent && ! in_array( $parent->post_type, get_post_types() ) ) {
405 if ( $wp_rewrite->using_permalinks() && $parent ) {
406 if ( 'page' == $parent->post_type )
407 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
409 $parentlink = get_permalink( $post->post_parent );
411 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
412 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
414 $name = $post->post_name;
416 if ( strpos($parentlink, '?') === false )
417 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
420 $link = str_replace( '%postname%', $name, $link );
421 } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
422 $link = home_url( user_trailingslashit( $post->post_name ) );
426 $link = home_url( '/?attachment_id=' . $post->ID );
429 * Filters the permalink for an attachment.
433 * @param string $link The attachment's permalink.
434 * @param int $post_id Attachment ID.
436 return apply_filters( 'attachment_link', $link, $post->ID );
440 * Retrieves the permalink for the year archives.
444 * @global WP_Rewrite $wp_rewrite
446 * @param int|bool $year False for current year or year for permalink.
447 * @return string The permalink for the specified year archive.
449 function get_year_link( $year ) {
452 $year = gmdate('Y', current_time('timestamp'));
453 $yearlink = $wp_rewrite->get_year_permastruct();
454 if ( !empty($yearlink) ) {
455 $yearlink = str_replace('%year%', $year, $yearlink);
456 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
458 $yearlink = home_url( '?m=' . $year );
462 * Filters the year archive permalink.
466 * @param string $yearlink Permalink for the year archive.
467 * @param int $year Year for the archive.
469 return apply_filters( 'year_link', $yearlink, $year );
473 * Retrieves the permalink for the month archives with year.
477 * @global WP_Rewrite $wp_rewrite
479 * @param bool|int $year False for current year. Integer of year.
480 * @param bool|int $month False for current month. Integer of month.
481 * @return string The permalink for the specified month and year archive.
483 function get_month_link($year, $month) {
486 $year = gmdate('Y', current_time('timestamp'));
488 $month = gmdate('m', current_time('timestamp'));
489 $monthlink = $wp_rewrite->get_month_permastruct();
490 if ( !empty($monthlink) ) {
491 $monthlink = str_replace('%year%', $year, $monthlink);
492 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
493 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
495 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
499 * Filters the month archive permalink.
503 * @param string $monthlink Permalink for the month archive.
504 * @param int $year Year for the archive.
505 * @param int $month The month for the archive.
507 return apply_filters( 'month_link', $monthlink, $year, $month );
511 * Retrieves the permalink for the day archives with year and month.
515 * @global WP_Rewrite $wp_rewrite
517 * @param bool|int $year False for current year. Integer of year.
518 * @param bool|int $month False for current month. Integer of month.
519 * @param bool|int $day False for current day. Integer of day.
520 * @return string The permalink for the specified day, month, and year archive.
522 function get_day_link($year, $month, $day) {
525 $year = gmdate('Y', current_time('timestamp'));
527 $month = gmdate('m', current_time('timestamp'));
529 $day = gmdate('j', current_time('timestamp'));
531 $daylink = $wp_rewrite->get_day_permastruct();
532 if ( !empty($daylink) ) {
533 $daylink = str_replace('%year%', $year, $daylink);
534 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
535 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
536 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
538 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
542 * Filters the day archive permalink.
546 * @param string $daylink Permalink for the day archive.
547 * @param int $year Year for the archive.
548 * @param int $month Month for the archive.
549 * @param int $day The day for the archive.
551 return apply_filters( 'day_link', $daylink, $year, $month, $day );
555 * Displays the permalink for the feed type.
559 * @param string $anchor The link's anchor text.
560 * @param string $feed Optional. Feed type. Default empty.
562 function the_feed_link( $anchor, $feed = '' ) {
563 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
566 * Filters the feed link anchor tag.
570 * @param string $link The complete anchor tag for a feed link.
571 * @param string $feed The feed type, or an empty string for the
574 echo apply_filters( 'the_feed_link', $link, $feed );
578 * Retrieves the permalink for the feed type.
582 * @global WP_Rewrite $wp_rewrite
584 * @param string $feed Optional. Feed type. Default empty.
585 * @return string The feed permalink.
587 function get_feed_link( $feed = '' ) {
590 $permalink = $wp_rewrite->get_feed_permastruct();
591 if ( '' != $permalink ) {
592 if ( false !== strpos($feed, 'comments_') ) {
593 $feed = str_replace('comments_', '', $feed);
594 $permalink = $wp_rewrite->get_comment_feed_permastruct();
597 if ( get_default_feed() == $feed )
600 $permalink = str_replace('%feed%', $feed, $permalink);
601 $permalink = preg_replace('#/+#', '/', "/$permalink");
602 $output = home_url( user_trailingslashit($permalink, 'feed') );
605 $feed = get_default_feed();
607 if ( false !== strpos($feed, 'comments_') )
608 $feed = str_replace('comments_', 'comments-', $feed);
610 $output = home_url("?feed={$feed}");
614 * Filters the feed type permalink.
618 * @param string $output The feed permalink.
619 * @param string $feed Feed type.
621 return apply_filters( 'feed_link', $output, $feed );
625 * Retrieves the permalink for the post comments feed.
629 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
630 * @param string $feed Optional. Feed type. Default empty.
631 * @return string The permalink for the comments feed for the given post.
633 function get_post_comments_feed_link( $post_id = 0, $feed = '' ) {
634 $post_id = absint( $post_id );
637 $post_id = get_the_ID();
639 if ( empty( $feed ) )
640 $feed = get_default_feed();
642 $post = get_post( $post_id );
643 $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent;
645 if ( '' != get_option('permalink_structure') ) {
646 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
647 $url = _get_page_link( $post_id );
649 $url = get_permalink($post_id);
652 $url = home_url( '/feed/' );
653 if ( $feed !== get_default_feed() ) {
656 $url = add_query_arg( 'attachment_id', $post_id, $url );
658 $url = trailingslashit($url) . 'feed';
659 if ( $feed != get_default_feed() )
661 $url = user_trailingslashit($url, 'single_feed');
665 $url = add_query_arg( array( 'feed' => $feed, 'attachment_id' => $post_id ), home_url( '/' ) );
666 } elseif ( 'page' == $post->post_type ) {
667 $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
669 $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
674 * Filters the post comments feed permalink.
678 * @param string $url Post comments feed permalink.
680 return apply_filters( 'post_comments_feed_link', $url );
684 * Displays the comment feed link for a post.
686 * Prints out the comment feed link for a post. Link text is placed in the
687 * anchor. If no link text is specified, default text is used. If no post ID is
688 * specified, the current post is used.
692 * @param string $link_text Optional. Descriptive link text. Default 'Comments Feed'.
693 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
694 * @param string $feed Optional. Feed format. Default empty.
696 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
697 $url = get_post_comments_feed_link( $post_id, $feed );
698 if ( empty( $link_text ) ) {
699 $link_text = __('Comments Feed');
702 $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>';
704 * Filters the post comment feed link anchor tag.
708 * @param string $link The complete anchor tag for the comment feed link.
709 * @param int $post_id Post ID.
710 * @param string $feed The feed type, or an empty string for the default feed type.
712 echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed );
716 * Retrieves the feed link for a given author.
718 * Returns a link to the feed for all posts by a given author. A specific feed
719 * can be requested or left blank to get the default feed.
723 * @param int $author_id Author ID.
724 * @param string $feed Optional. Feed type. Default empty.
725 * @return string Link to the feed for the author specified by $author_id.
727 function get_author_feed_link( $author_id, $feed = '' ) {
728 $author_id = (int) $author_id;
729 $permalink_structure = get_option('permalink_structure');
732 $feed = get_default_feed();
734 if ( '' == $permalink_structure ) {
735 $link = home_url("?feed=$feed&author=" . $author_id);
737 $link = get_author_posts_url($author_id);
738 if ( $feed == get_default_feed() )
741 $feed_link = "feed/$feed";
743 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
747 * Filters the feed link for a given author.
751 * @param string $link The author feed link.
752 * @param string $feed Feed type.
754 $link = apply_filters( 'author_feed_link', $link, $feed );
760 * Retrieves the feed link for a category.
762 * Returns a link to the feed for all posts in a given category. A specific feed
763 * can be requested or left blank to get the default feed.
767 * @param int $cat_id Category ID.
768 * @param string $feed Optional. Feed type. Default empty.
769 * @return string Link to the feed for the category specified by $cat_id.
771 function get_category_feed_link( $cat_id, $feed = '' ) {
772 return get_term_feed_link( $cat_id, 'category', $feed );
776 * Retrieves the feed link for a term.
778 * Returns a link to the feed for all posts in a given term. A specific feed
779 * can be requested or left blank to get the default feed.
783 * @param int $term_id Term ID.
784 * @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'.
785 * @param string $feed Optional. Feed type. Default empty.
786 * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
788 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
789 $term_id = ( int ) $term_id;
791 $term = get_term( $term_id, $taxonomy );
793 if ( empty( $term ) || is_wp_error( $term ) )
796 if ( empty( $feed ) )
797 $feed = get_default_feed();
799 $permalink_structure = get_option( 'permalink_structure' );
801 if ( '' == $permalink_structure ) {
802 if ( 'category' == $taxonomy ) {
803 $link = home_url("?feed=$feed&cat=$term_id");
805 elseif ( 'post_tag' == $taxonomy ) {
806 $link = home_url("?feed=$feed&tag=$term->slug");
808 $t = get_taxonomy( $taxonomy );
809 $link = home_url("?feed=$feed&$t->query_var=$term->slug");
812 $link = get_term_link( $term_id, $term->taxonomy );
813 if ( $feed == get_default_feed() )
816 $feed_link = "feed/$feed";
818 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
821 if ( 'category' == $taxonomy ) {
823 * Filters the category feed link.
827 * @param string $link The category feed link.
828 * @param string $feed Feed type.
830 $link = apply_filters( 'category_feed_link', $link, $feed );
831 } elseif ( 'post_tag' == $taxonomy ) {
833 * Filters the post tag feed link.
837 * @param string $link The tag feed link.
838 * @param string $feed Feed type.
840 $link = apply_filters( 'tag_feed_link', $link, $feed );
843 * Filters the feed link for a taxonomy other than 'category' or 'post_tag'.
847 * @param string $link The taxonomy feed link.
848 * @param string $feed Feed type.
849 * @param string $feed The taxonomy name.
851 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
858 * Retrieves the permalink for a tag feed.
862 * @param int $tag_id Tag ID.
863 * @param string $feed Optional. Feed type. Default empty.
864 * @return string The feed permalink for the given tag.
866 function get_tag_feed_link( $tag_id, $feed = '' ) {
867 return get_term_feed_link( $tag_id, 'post_tag', $feed );
871 * Retrieves the edit link for a tag.
875 * @param int $tag_id Tag ID.
876 * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'.
877 * @return string The edit tag link URL for the given tag.
879 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
881 * Filters the edit link for a tag (or term in another taxonomy).
885 * @param string $link The term edit link.
887 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
891 * Displays or retrieves the edit link for a tag with formatting.
895 * @param string $link Optional. Anchor text. Default empty.
896 * @param string $before Optional. Display before edit link. Default empty.
897 * @param string $after Optional. Display after edit link. Default empty.
898 * @param WP_Term $tag Optional. Term object. If null, the queried object will be inspected.
901 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
902 $link = edit_term_link( $link, '', '', $tag, false );
905 * Filters the anchor tag for the edit link for a tag (or term in another taxonomy).
909 * @param string $link The anchor tag for the edit link.
911 echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
915 * Retrieves the URL for editing a given term.
918 * @since 4.5.0 The `$taxonomy` argument was made optional.
920 * @param int $term_id Term ID.
921 * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified
923 * @param string $object_type Optional. The object type. Used to highlight the proper post type
924 * menu on the linked page. Defaults to the first object_type associated
926 * @return string|null The edit term link URL for the given term, or null on failure.
928 function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
929 $term = get_term( $term_id, $taxonomy );
930 if ( ! $term || is_wp_error( $term ) ) {
934 $tax = get_taxonomy( $term->taxonomy );
935 if ( ! $tax || ! current_user_can( 'edit_term', $term->term_id ) ) {
940 'taxonomy' => $taxonomy,
941 'tag_ID' => $term->term_id,
944 if ( $object_type ) {
945 $args['post_type'] = $object_type;
946 } elseif ( ! empty( $tax->object_type ) ) {
947 $args['post_type'] = reset( $tax->object_type );
950 if ( $tax->show_ui ) {
951 $location = add_query_arg( $args, admin_url( 'term.php' ) );
957 * Filters the edit link for a term.
961 * @param string $location The edit link.
962 * @param int $term_id Term ID.
963 * @param string $taxonomy Taxonomy name.
964 * @param string $object_type The object type (eg. the post type).
966 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
970 * Displays or retrieves the edit term link with formatting.
974 * @param string $link Optional. Anchor text. Default empty.
975 * @param string $before Optional. Display before edit link. Default empty.
976 * @param string $after Optional. Display after edit link. Default empty.
977 * @param object $term Optional. Term object. If null, the queried object will be inspected. Default null.
978 * @param bool $echo Optional. Whether or not to echo the return. Default true.
979 * @return string|void HTML content.
981 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
982 if ( is_null( $term ) )
983 $term = get_queried_object();
988 $tax = get_taxonomy( $term->taxonomy );
989 if ( ! current_user_can( 'edit_term', $term->term_id ) ) {
993 if ( empty( $link ) )
994 $link = __('Edit This');
996 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
999 * Filters the anchor tag for the edit link of a term.
1003 * @param string $link The anchor tag for the edit link.
1004 * @param int $term_id Term ID.
1006 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
1015 * Retrieves the permalink for a search.
1019 * @global WP_Rewrite $wp_rewrite
1021 * @param string $query Optional. The query string to use. If empty the current query is used. Default empty.
1022 * @return string The search permalink.
1024 function get_search_link( $query = '' ) {
1027 if ( empty($query) )
1028 $search = get_search_query( false );
1030 $search = stripslashes($query);
1032 $permastruct = $wp_rewrite->get_search_permastruct();
1034 if ( empty( $permastruct ) ) {
1035 $link = home_url('?s=' . urlencode($search) );
1037 $search = urlencode($search);
1038 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it un-encoded.
1039 $link = str_replace( '%search%', $search, $permastruct );
1040 $link = home_url( user_trailingslashit( $link, 'search' ) );
1044 * Filters the search permalink.
1048 * @param string $link Search permalink.
1049 * @param string $search The URL-encoded search term.
1051 return apply_filters( 'search_link', $link, $search );
1055 * Retrieves the permalink for the search results feed.
1059 * @global WP_Rewrite $wp_rewrite
1061 * @param string $search_query Optional. Search query. Default empty.
1062 * @param string $feed Optional. Feed type. Default empty.
1063 * @return string The search results feed permalink.
1065 function get_search_feed_link($search_query = '', $feed = '') {
1067 $link = get_search_link($search_query);
1070 $feed = get_default_feed();
1072 $permastruct = $wp_rewrite->get_search_permastruct();
1074 if ( empty($permastruct) ) {
1075 $link = add_query_arg('feed', $feed, $link);
1077 $link = trailingslashit($link);
1078 $link .= "feed/$feed/";
1082 * Filters the search feed link.
1086 * @param string $link Search feed link.
1087 * @param string $feed Feed type.
1088 * @param string $type The search type. One of 'posts' or 'comments'.
1090 return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1094 * Retrieves the permalink for the search results comments feed.
1098 * @global WP_Rewrite $wp_rewrite
1100 * @param string $search_query Optional. Search query. Default empty.
1101 * @param string $feed Optional. Feed type. Default empty.
1102 * @return string The comments feed search results permalink.
1104 function get_search_comments_feed_link($search_query = '', $feed = '') {
1108 $feed = get_default_feed();
1110 $link = get_search_feed_link($search_query, $feed);
1112 $permastruct = $wp_rewrite->get_search_permastruct();
1114 if ( empty($permastruct) )
1115 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1117 $link = add_query_arg('withcomments', 1, $link);
1119 /** This filter is documented in wp-includes/link-template.php */
1120 return apply_filters( 'search_feed_link', $link, $feed, 'comments' );
1124 * Retrieves the permalink for a post type archive.
1127 * @since 4.5.0 Support for posts was added.
1129 * @global WP_Rewrite $wp_rewrite
1131 * @param string $post_type Post type.
1132 * @return string|false The post type archive permalink.
1134 function get_post_type_archive_link( $post_type ) {
1136 if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1139 if ( 'post' === $post_type ) {
1140 $show_on_front = get_option( 'show_on_front' );
1141 $page_for_posts = get_option( 'page_for_posts' );
1143 if ( 'page' == $show_on_front && $page_for_posts ) {
1144 $link = get_permalink( $page_for_posts );
1146 $link = get_home_url();
1148 /** This filter is documented in wp-includes/link-template.php */
1149 return apply_filters( 'post_type_archive_link', $link, $post_type );
1152 if ( ! $post_type_obj->has_archive )
1155 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1156 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1157 if ( $post_type_obj->rewrite['with_front'] )
1158 $struct = $wp_rewrite->front . $struct;
1160 $struct = $wp_rewrite->root . $struct;
1161 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1163 $link = home_url( '?post_type=' . $post_type );
1167 * Filters the post type archive permalink.
1171 * @param string $link The post type archive permalink.
1172 * @param string $post_type Post type name.
1174 return apply_filters( 'post_type_archive_link', $link, $post_type );
1178 * Retrieves the permalink for a post type archive feed.
1182 * @param string $post_type Post type
1183 * @param string $feed Optional. Feed type. Default empty.
1184 * @return string|false The post type feed permalink.
1186 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1187 $default_feed = get_default_feed();
1188 if ( empty( $feed ) )
1189 $feed = $default_feed;
1191 if ( ! $link = get_post_type_archive_link( $post_type ) )
1194 $post_type_obj = get_post_type_object( $post_type );
1195 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1196 $link = trailingslashit( $link );
1198 if ( $feed != $default_feed )
1201 $link = add_query_arg( 'feed', $feed, $link );
1205 * Filters the post type archive feed link.
1209 * @param string $link The post type archive feed link.
1210 * @param string $feed Feed type.
1212 return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1216 * Retrieves the URL used for the post preview.
1218 * Allows additional query args to be appended.
1222 * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global `$post`.
1223 * @param array $query_args Optional. Array of additional query args to be appended to the link.
1224 * Default empty array.
1225 * @param string $preview_link Optional. Base preview link to be used if it should differ from the
1226 * post permalink. Default empty.
1227 * @return string|null URL used for the post preview, or null if the post does not exist.
1229 function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
1230 $post = get_post( $post );
1235 $post_type_object = get_post_type_object( $post->post_type );
1236 if ( is_post_type_viewable( $post_type_object ) ) {
1237 if ( ! $preview_link ) {
1238 $preview_link = set_url_scheme( get_permalink( $post ) );
1241 $query_args['preview'] = 'true';
1242 $preview_link = add_query_arg( $query_args, $preview_link );
1246 * Filters the URL used for a post preview.
1249 * @since 4.0.0 Added the `$post` parameter.
1251 * @param string $preview_link URL used for the post preview.
1252 * @param WP_Post $post Post object.
1254 return apply_filters( 'preview_post_link', $preview_link, $post );
1258 * Retrieves the edit post link for post.
1260 * Can be used within the WordPress loop or outside of it. Can be used with
1261 * pages, posts, attachments, and revisions.
1265 * @param int $id Optional. Post ID. Default is the ID of the global `$post`.
1266 * @param string $context Optional. How to output the '&' character. Default '&'.
1267 * @return string|null The edit post link for the given post. null if the post type is invalid or does
1268 * not allow an editing UI.
1270 function get_edit_post_link( $id = 0, $context = 'display' ) {
1271 if ( ! $post = get_post( $id ) )
1274 if ( 'revision' === $post->post_type )
1276 elseif ( 'display' == $context )
1277 $action = '&action=edit';
1279 $action = '&action=edit';
1281 $post_type_object = get_post_type_object( $post->post_type );
1282 if ( !$post_type_object )
1285 if ( !current_user_can( 'edit_post', $post->ID ) )
1288 if ( $post_type_object->_edit_link ) {
1289 $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
1295 * Filters the post edit link.
1299 * @param string $link The edit link.
1300 * @param int $post_id Post ID.
1301 * @param string $context The link context. If set to 'display' then ampersands
1304 return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
1308 * Displays the edit post link for post.
1311 * @since 4.4.0 The `$class` argument was added.
1313 * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null.
1314 * @param string $before Optional. Display before edit link. Default empty.
1315 * @param string $after Optional. Display after edit link. Default empty.
1316 * @param int $id Optional. Post ID. Default is the ID of the global `$post`.
1317 * @param string $class Optional. Add custom class to link. Default 'post-edit-link'.
1319 function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
1320 if ( ! $post = get_post( $id ) ) {
1324 if ( ! $url = get_edit_post_link( $post->ID ) ) {
1328 if ( null === $text ) {
1329 $text = __( 'Edit This' );
1332 $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
1335 * Filters the post edit link anchor tag.
1339 * @param string $link Anchor tag for the edit link.
1340 * @param int $post_id Post ID.
1341 * @param string $text Anchor text.
1343 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1347 * Retrieves the delete posts link for post.
1349 * Can be used within the WordPress loop or outside of it, with any post type.
1353 * @param int $id Optional. Post ID. Default is the ID of the global `$post`.
1354 * @param string $deprecated Not used.
1355 * @param bool $force_delete Optional. Whether to bypass trash and force deletion. Default false.
1356 * @return string|void The delete post link URL for the given post.
1358 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1359 if ( ! empty( $deprecated ) )
1360 _deprecated_argument( __FUNCTION__, '3.0.0' );
1362 if ( !$post = get_post( $id ) )
1365 $post_type_object = get_post_type_object( $post->post_type );
1366 if ( !$post_type_object )
1369 if ( !current_user_can( 'delete_post', $post->ID ) )
1372 $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1374 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1377 * Filters the post delete link.
1381 * @param string $link The delete link.
1382 * @param int $post_id Post ID.
1383 * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
1385 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1389 * Retrieves the edit comment link.
1393 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
1394 * @return string|void The edit comment link URL for the given comment.
1396 function get_edit_comment_link( $comment_id = 0 ) {
1397 $comment = get_comment( $comment_id );
1399 if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1402 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
1405 * Filters the comment edit link.
1409 * @param string $location The edit link.
1411 return apply_filters( 'get_edit_comment_link', $location );
1415 * Displays the edit comment link with formatting.
1419 * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null.
1420 * @param string $before Optional. Display before edit link. Default empty.
1421 * @param string $after Optional. Display after edit link. Default empty.
1423 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1424 $comment = get_comment();
1426 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1430 if ( null === $text ) {
1431 $text = __( 'Edit This' );
1434 $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
1437 * Filters the comment edit link anchor tag.
1441 * @param string $link Anchor tag for the edit link.
1442 * @param int $comment_id Comment ID.
1443 * @param string $text Anchor text.
1445 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1449 * Displays the edit bookmark link.
1453 * @param int|stdClass $link Optional. Bookmark ID. Default is the id of the current bookmark.
1454 * @return string|void The edit bookmark link URL.
1456 function get_edit_bookmark_link( $link = 0 ) {
1457 $link = get_bookmark( $link );
1459 if ( !current_user_can('manage_links') )
1462 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
1465 * Filters the bookmark edit link.
1469 * @param string $location The edit link.
1470 * @param int $link_id Bookmark ID.
1472 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1476 * Displays the edit bookmark link anchor content.
1480 * @param string $link Optional. Anchor text. Default empty.
1481 * @param string $before Optional. Display before edit link. Default empty.
1482 * @param string $after Optional. Display after edit link. Default empty.
1483 * @param int $bookmark Optional. Bookmark ID. Default is the current bookmark.
1485 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1486 $bookmark = get_bookmark($bookmark);
1488 if ( !current_user_can('manage_links') )
1492 $link = __('Edit This');
1494 $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>';
1497 * Filters the bookmark edit link anchor tag.
1501 * @param string $link Anchor tag for the edit link.
1502 * @param int $link_id Bookmark ID.
1504 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1508 * Retrieves the edit user link.
1512 * @param int $user_id Optional. User ID. Defaults to the current user.
1513 * @return string URL to edit user page or empty string.
1515 function get_edit_user_link( $user_id = null ) {
1517 $user_id = get_current_user_id();
1519 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1522 $user = get_userdata( $user_id );
1527 if ( get_current_user_id() == $user->ID )
1528 $link = get_edit_profile_url( $user->ID );
1530 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1533 * Filters the user edit link.
1537 * @param string $link The edit link.
1538 * @param int $user_id User ID.
1540 return apply_filters( 'get_edit_user_link', $link, $user->ID );
1546 * Retrieves the previous post that is adjacent to the current post.
1550 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
1551 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1552 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1553 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
1554 * corresponding post exists.
1556 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1557 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1561 * Retrieves the next post that is adjacent to the current post.
1565 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
1566 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1567 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1568 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
1569 * corresponding post exists.
1571 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1572 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1576 * Retrieves the adjacent post.
1578 * Can either be next or previous post.
1582 * @global wpdb $wpdb WordPress database abstraction object.
1584 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
1585 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1586 * @param bool $previous Optional. Whether to retrieve previous post. Default true
1587 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1588 * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
1589 * corresponding post exists.
1591 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1594 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1597 $current_post_date = $post->post_date;
1601 $adjacent = $previous ? 'previous' : 'next';
1603 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1604 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1605 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1606 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1607 _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1608 $excluded_terms = explode( ' and ', $excluded_terms );
1610 $excluded_terms = explode( ',', $excluded_terms );
1613 $excluded_terms = array_map( 'intval', $excluded_terms );
1616 if ( $in_same_term ) {
1617 $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";
1618 $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1620 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1622 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1624 // Remove any exclusions from the term array to include.
1625 $term_array = array_diff( $term_array, (array) $excluded_terms );
1626 $term_array = array_map( 'intval', $term_array );
1628 if ( ! $term_array || is_wp_error( $term_array ) )
1631 $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1635 * Filters the IDs of terms excluded from adjacent post queries.
1637 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1638 * of adjacency, 'next' or 'previous'.
1642 * @param string $excluded_terms Array of excluded term IDs.
1644 $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
1646 if ( ! empty( $excluded_terms ) ) {
1647 $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( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
1651 // 'post_status' clause depends on the current user.
1652 if ( is_user_logged_in() ) {
1653 $user_id = get_current_user_id();
1655 $post_type_object = get_post_type_object( $post->post_type );
1656 if ( empty( $post_type_object ) ) {
1657 $post_type_cap = $post->post_type;
1658 $read_private_cap = 'read_private_' . $post_type_cap . 's';
1660 $read_private_cap = $post_type_object->cap->read_private_posts;
1664 * Results should include private posts belonging to the current user, or private posts where the
1665 * current user has the 'read_private_posts' cap.
1667 $private_states = get_post_stati( array( 'private' => true ) );
1668 $where .= " AND ( p.post_status = 'publish'";
1669 foreach ( (array) $private_states as $state ) {
1670 if ( current_user_can( $read_private_cap ) ) {
1671 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1673 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1678 $where .= " AND p.post_status = 'publish'";
1681 $op = $previous ? '<' : '>';
1682 $order = $previous ? 'DESC' : 'ASC';
1685 * Filters the JOIN clause in the SQL for an adjacent post query.
1687 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1688 * of adjacency, 'next' or 'previous'.
1691 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1693 * @param string $join The JOIN clause in the SQL.
1694 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1695 * @param array $excluded_terms Array of excluded term IDs.
1696 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1697 * @param WP_Post $post WP_Post object.
1699 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
1702 * Filters the WHERE clause in the SQL for an adjacent post query.
1704 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1705 * of adjacency, 'next' or 'previous'.
1708 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1710 * @param string $where The `WHERE` clause in the SQL.
1711 * @param bool $in_same_term Whether post should be in a same taxonomy term.
1712 * @param array $excluded_terms Array of excluded term IDs.
1713 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
1714 * @param WP_Post $post WP_Post object.
1716 $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, $taxonomy, $post );
1719 * Filters the ORDER BY clause in the SQL for an adjacent post query.
1721 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1722 * of adjacency, 'next' or 'previous'.
1725 * @since 4.4.0 Added the `$post` parameter.
1727 * @param string $order_by The `ORDER BY` clause in the SQL.
1728 * @param WP_Post $post WP_Post object.
1730 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post );
1732 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1733 $query_key = 'adjacent_post_' . md5( $query );
1734 $result = wp_cache_get( $query_key, 'counts' );
1735 if ( false !== $result ) {
1737 $result = get_post( $result );
1741 $result = $wpdb->get_var( $query );
1742 if ( null === $result )
1745 wp_cache_set( $query_key, $result, 'counts' );
1748 $result = get_post( $result );
1754 * Retrieves the adjacent post relational link.
1756 * Can either be next or previous post relational link.
1760 * @param string $title Optional. Link title format. Default '%title'.
1761 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1762 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1763 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
1764 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1765 * @return string|void The adjacent post relational link URL.
1767 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1768 if ( $previous && is_attachment() && $post = get_post() )
1769 $post = get_post( $post->post_parent );
1771 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1773 if ( empty( $post ) )
1776 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1778 if ( empty( $post_title ) )
1779 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1781 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1783 $title = str_replace( '%title', $post_title, $title );
1784 $title = str_replace( '%date', $date, $title );
1786 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1787 $link .= esc_attr( $title );
1788 $link .= "' href='" . get_permalink( $post ) . "' />\n";
1790 $adjacent = $previous ? 'previous' : 'next';
1793 * Filters the adjacent post relational link.
1795 * The dynamic portion of the hook name, `$adjacent`, refers to the type
1796 * of adjacency, 'next' or 'previous'.
1800 * @param string $link The relational link.
1802 return apply_filters( "{$adjacent}_post_rel_link", $link );
1806 * Displays the relational links for the posts adjacent to the current post.
1810 * @param string $title Optional. Link title format. Default '%title'.
1811 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1812 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1813 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1815 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1816 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1817 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1821 * Displays relational links for the posts adjacent to the current post for single post pages.
1823 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins
1824 * or theme templates.
1828 * @see adjacent_posts_rel_link()
1830 function adjacent_posts_rel_link_wp_head() {
1831 if ( ! is_single() || is_attachment() ) {
1834 adjacent_posts_rel_link();
1838 * Displays the relational link for the next post adjacent to the current post.
1842 * @see get_adjacent_post_rel_link()
1844 * @param string $title Optional. Link title format. Default '%title'.
1845 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1846 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1847 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1849 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1850 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1854 * Displays the relational link for the previous post adjacent to the current post.
1858 * @see get_adjacent_post_rel_link()
1860 * @param string $title Optional. Link title format. Default '%title'.
1861 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1862 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1863 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1865 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1866 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1870 * Retrieves the boundary post.
1872 * Boundary being either the first or last post by publish date within the constraints specified
1873 * by $in_same_term or $excluded_terms.
1877 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
1879 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1881 * @param bool $start Optional. Whether to retrieve first or last post. Default true
1882 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1883 * @return null|array Array containing the boundary post object if successful, null otherwise.
1885 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1887 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1890 $query_args = array(
1891 'posts_per_page' => 1,
1892 'order' => $start ? 'ASC' : 'DESC',
1893 'update_post_term_cache' => false,
1894 'update_post_meta_cache' => false
1897 $term_array = array();
1899 if ( ! is_array( $excluded_terms ) ) {
1900 if ( ! empty( $excluded_terms ) )
1901 $excluded_terms = explode( ',', $excluded_terms );
1903 $excluded_terms = array();
1906 if ( $in_same_term || ! empty( $excluded_terms ) ) {
1907 if ( $in_same_term )
1908 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1910 if ( ! empty( $excluded_terms ) ) {
1911 $excluded_terms = array_map( 'intval', $excluded_terms );
1912 $excluded_terms = array_diff( $excluded_terms, $term_array );
1914 $inverse_terms = array();
1915 foreach ( $excluded_terms as $excluded_term )
1916 $inverse_terms[] = $excluded_term * -1;
1917 $excluded_terms = $inverse_terms;
1920 $query_args[ 'tax_query' ] = array( array(
1921 'taxonomy' => $taxonomy,
1922 'terms' => array_merge( $term_array, $excluded_terms )
1926 return get_posts( $query_args );
1930 * Retrieves the previous post link that is adjacent to the current post.
1934 * @param string $format Optional. Link anchor format. Default '« %link'.
1935 * @param string $link Optional. Link permalink format. Default '%title%'.
1936 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1937 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1938 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1939 * @return string The link URL of the previous post in relation to the current post.
1941 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1942 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1946 * Displays the previous post link that is adjacent to the current post.
1950 * @see get_previous_post_link()
1952 * @param string $format Optional. Link anchor format. Default '« %link'.
1953 * @param string $link Optional. Link permalink format. Default '%title'.
1954 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1955 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1956 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1958 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1959 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1963 * Retrieves the next post link that is adjacent to the current post.
1967 * @param string $format Optional. Link anchor format. Default '« %link'.
1968 * @param string $link Optional. Link permalink format. Default '%title'.
1969 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1970 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1971 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1972 * @return string The link URL of the next post in relation to the current post.
1974 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1975 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1979 * Displays the next post link that is adjacent to the current post.
1982 * @see get_next_post_link()
1984 * @param string $format Optional. Link anchor format. Default '« %link'.
1985 * @param string $link Optional. Link permalink format. Default '%title'
1986 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
1987 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
1988 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1990 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1991 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1995 * Retrieves the adjacent post link.
1997 * Can be either next post link or previous.
2001 * @param string $format Link anchor format.
2002 * @param string $link Link permalink format.
2003 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
2004 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. Default empty.
2005 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
2006 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
2007 * @return string The link URL of the previous or next post in relation to the current post.
2009 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
2010 if ( $previous && is_attachment() )
2011 $post = get_post( get_post()->post_parent );
2013 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
2018 $title = $post->post_title;
2020 if ( empty( $post->post_title ) )
2021 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
2023 /** This filter is documented in wp-includes/post-template.php */
2024 $title = apply_filters( 'the_title', $title, $post->ID );
2026 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
2027 $rel = $previous ? 'prev' : 'next';
2029 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
2030 $inlink = str_replace( '%title', $title, $link );
2031 $inlink = str_replace( '%date', $date, $inlink );
2032 $inlink = $string . $inlink . '</a>';
2034 $output = str_replace( '%link', $inlink, $format );
2037 $adjacent = $previous ? 'previous' : 'next';
2040 * Filters the adjacent post link.
2042 * The dynamic portion of the hook name, `$adjacent`, refers to the type
2043 * of adjacency, 'next' or 'previous'.
2046 * @since 4.2.0 Added the `$adjacent` parameter.
2048 * @param string $output The adjacent post link.
2049 * @param string $format Link anchor format.
2050 * @param string $link Link permalink format.
2051 * @param WP_Post $post The adjacent post.
2052 * @param string $adjacent Whether the post is previous or next.
2054 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
2058 * Displays the adjacent post link.
2060 * Can be either next post link or previous.
2064 * @param string $format Link anchor format.
2065 * @param string $link Link permalink format.
2066 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
2067 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs. Default empty.
2068 * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
2069 * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
2071 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
2072 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
2076 * Retrieves the link for a page number.
2080 * @global WP_Rewrite $wp_rewrite
2082 * @param int $pagenum Optional. Page ID. Default 1.
2083 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
2084 * Otherwise, prepares the URL with esc_url_raw().
2085 * @return string The link URL for the given page number.
2087 function get_pagenum_link($pagenum = 1, $escape = true ) {
2090 $pagenum = (int) $pagenum;
2092 $request = remove_query_arg( 'paged' );
2094 $home_root = parse_url(home_url());
2095 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
2096 $home_root = preg_quote( $home_root, '|' );
2098 $request = preg_replace('|^'. $home_root . '|i', '', $request);
2099 $request = preg_replace('|^/+|', '', $request);
2101 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
2102 $base = trailingslashit( get_bloginfo( 'url' ) );
2104 if ( $pagenum > 1 ) {
2105 $result = add_query_arg( 'paged', $pagenum, $base . $request );
2107 $result = $base . $request;
2110 $qs_regex = '|\?.*?$|';
2111 preg_match( $qs_regex, $request, $qs_match );
2113 if ( !empty( $qs_match[0] ) ) {
2114 $query_string = $qs_match[0];
2115 $request = preg_replace( $qs_regex, '', $request );
2120 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
2121 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
2122 $request = ltrim($request, '/');
2124 $base = trailingslashit( get_bloginfo( 'url' ) );
2126 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
2127 $base .= $wp_rewrite->index . '/';
2129 if ( $pagenum > 1 ) {
2130 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
2133 $result = $base . $request . $query_string;
2137 * Filters the page number link for the current request.
2141 * @param string $result The page number link.
2143 $result = apply_filters( 'get_pagenum_link', $result );
2146 return esc_url( $result );
2148 return esc_url_raw( $result );
2152 * Retrieves the next posts page link.
2154 * Backported from 2.1.3 to 2.0.10.
2158 * @global int $paged
2160 * @param int $max_page Optional. Max pages. Default 0.
2161 * @return string|void The link URL for next posts page.
2163 function get_next_posts_page_link($max_page = 0) {
2166 if ( !is_single() ) {
2169 $nextpage = intval($paged) + 1;
2170 if ( !$max_page || $max_page >= $nextpage )
2171 return get_pagenum_link($nextpage);
2176 * Displays or retrieves the next posts page link.
2180 * @param int $max_page Optional. Max pages. Default 0.
2181 * @param bool $echo Optional. Whether to echo the link. Default true.
2182 * @return string|void The link URL for next posts page if `$echo = false`.
2184 function next_posts( $max_page = 0, $echo = true ) {
2185 $output = esc_url( get_next_posts_page_link( $max_page ) );
2194 * Retrieves the next posts page link.
2198 * @global int $paged
2199 * @global WP_Query $wp_query
2201 * @param string $label Content for link text.
2202 * @param int $max_page Optional. Max pages. Default 0.
2203 * @return string|void HTML-formatted next posts page link.
2205 function get_next_posts_link( $label = null, $max_page = 0 ) {
2206 global $paged, $wp_query;
2209 $max_page = $wp_query->max_num_pages;
2214 $nextpage = intval($paged) + 1;
2216 if ( null === $label )
2217 $label = __( 'Next Page »' );
2219 if ( !is_single() && ( $nextpage <= $max_page ) ) {
2221 * Filters the anchor tag attributes for the next posts page link.
2225 * @param string $attributes Attributes for the anchor tag.
2227 $attr = apply_filters( 'next_posts_link_attributes', '' );
2229 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
2234 * Displays the next posts page link.
2238 * @param string $label Content for link text.
2239 * @param int $max_page Optional. Max pages. Default 0.
2241 function next_posts_link( $label = null, $max_page = 0 ) {
2242 echo get_next_posts_link( $label, $max_page );
2246 * Retrieves the previous posts page link.
2248 * Will only return string, if not on a single page or post.
2250 * Backported to 2.0.10 from 2.1.3.
2254 * @global int $paged
2256 * @return string|void The link for the previous posts page.
2258 function get_previous_posts_page_link() {
2261 if ( !is_single() ) {
2262 $nextpage = intval($paged) - 1;
2263 if ( $nextpage < 1 )
2265 return get_pagenum_link($nextpage);
2270 * Displays or retrieves the previous posts page link.
2274 * @param bool $echo Optional. Whether to echo the link. Default true.
2275 * @return string|void The previous posts page link if `$echo = false`.
2277 function previous_posts( $echo = true ) {
2278 $output = esc_url( get_previous_posts_page_link() );
2287 * Retrieves the previous posts page link.
2291 * @global int $paged
2293 * @param string $label Optional. Previous page link text.
2294 * @return string|void HTML-formatted previous page link.
2296 function get_previous_posts_link( $label = null ) {
2299 if ( null === $label )
2300 $label = __( '« Previous Page' );
2302 if ( !is_single() && $paged > 1 ) {
2304 * Filters the anchor tag attributes for the previous posts page link.
2308 * @param string $attributes Attributes for the anchor tag.
2310 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2311 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) .'</a>';
2316 * Displays the previous posts page link.
2320 * @param string $label Optional. Previous page link text.
2322 function previous_posts_link( $label = null ) {
2323 echo get_previous_posts_link( $label );
2327 * Retrieves the post pages link navigation for previous and next pages.
2331 * @global WP_Query $wp_query
2333 * @param string|array $args {
2334 * Optional. Arguments to build the post pages link navigation.
2336 * @type string $sep Separator character. Default '—'.
2337 * @type string $prelabel Link text to display for the previous page link.
2338 * Default '« Previous Page'.
2339 * @type string $nxtlabel Link text to display for the next page link.
2340 * Default 'Next Page »'.
2342 * @return string The posts link navigation.
2344 function get_posts_nav_link( $args = array() ) {
2349 if ( !is_singular() ) {
2351 'sep' => ' — ',
2352 'prelabel' => __('« Previous Page'),
2353 'nxtlabel' => __('Next Page »'),
2355 $args = wp_parse_args( $args, $defaults );
2357 $max_num_pages = $wp_query->max_num_pages;
2358 $paged = get_query_var('paged');
2360 //only have sep if there's both prev and next results
2361 if ($paged < 2 || $paged >= $max_num_pages) {
2365 if ( $max_num_pages > 1 ) {
2366 $return = get_previous_posts_link($args['prelabel']);
2367 $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep']);
2368 $return .= get_next_posts_link($args['nxtlabel']);
2376 * Displays the post pages link navigation for previous and next pages.
2380 * @param string $sep Optional. Separator for posts navigation links. Default empty.
2381 * @param string $prelabel Optional. Label for previous pages. Default empty.
2382 * @param string $nxtlabel Optional Label for next pages. Default empty.
2384 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2385 $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2386 echo get_posts_nav_link($args);
2390 * Retrieves the navigation to next/previous post, when applicable.
2393 * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments.
2395 * @param array $args {
2396 * Optional. Default post navigation arguments. Default empty array.
2398 * @type string $prev_text Anchor text to display in the previous post link. Default '%title'.
2399 * @type string $next_text Anchor text to display in the next post link. Default '%title'.
2400 * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false.
2401 * @type array|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty.
2402 * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'.
2403 * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2405 * @return string Markup for post links.
2407 function get_the_post_navigation( $args = array() ) {
2408 $args = wp_parse_args( $args, array(
2409 'prev_text' => '%title',
2410 'next_text' => '%title',
2411 'in_same_term' => false,
2412 'excluded_terms' => '',
2413 'taxonomy' => 'category',
2414 'screen_reader_text' => __( 'Post navigation' ),
2419 $previous = get_previous_post_link(
2420 '<div class="nav-previous">%link</div>',
2422 $args['in_same_term'],
2423 $args['excluded_terms'],
2427 $next = get_next_post_link(
2428 '<div class="nav-next">%link</div>',
2430 $args['in_same_term'],
2431 $args['excluded_terms'],
2435 // Only add markup if there's somewhere to navigate to.
2436 if ( $previous || $next ) {
2437 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2444 * Displays the navigation to next/previous post, when applicable.
2448 * @param array $args Optional. See get_the_post_navigation() for available arguments.
2449 * Default empty array.
2451 function the_post_navigation( $args = array() ) {
2452 echo get_the_post_navigation( $args );
2456 * Returns the navigation to next/previous set of posts, when applicable.
2460 * @global WP_Query $wp_query WordPress Query object.
2462 * @param array $args {
2463 * Optional. Default posts navigation arguments. Default empty array.
2465 * @type string $prev_text Anchor text to display in the previous posts link.
2466 * Default 'Older posts'.
2467 * @type string $next_text Anchor text to display in the next posts link.
2468 * Default 'Newer posts'.
2469 * @type string $screen_reader_text Screen reader text for nav element.
2470 * Default 'Posts navigation'.
2472 * @return string Markup for posts links.
2474 function get_the_posts_navigation( $args = array() ) {
2477 // Don't print empty markup if there's only one page.
2478 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2479 $args = wp_parse_args( $args, array(
2480 'prev_text' => __( 'Older posts' ),
2481 'next_text' => __( 'Newer posts' ),
2482 'screen_reader_text' => __( 'Posts navigation' ),
2485 $next_link = get_previous_posts_link( $args['next_text'] );
2486 $prev_link = get_next_posts_link( $args['prev_text'] );
2489 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2493 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2496 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2503 * Displays the navigation to next/previous set of posts, when applicable.
2507 * @param array $args Optional. See get_the_posts_navigation() for available arguments.
2508 * Default empty array.
2510 function the_posts_navigation( $args = array() ) {
2511 echo get_the_posts_navigation( $args );
2515 * Retrieves a paginated navigation to next/previous set of posts, when applicable.
2519 * @param array $args {
2520 * Optional. Default pagination arguments, see paginate_links().
2522 * @type string $screen_reader_text Screen reader text for navigation element.
2523 * Default 'Posts navigation'.
2525 * @return string Markup for pagination links.
2527 function get_the_posts_pagination( $args = array() ) {
2530 // Don't print empty markup if there's only one page.
2531 if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2532 $args = wp_parse_args( $args, array(
2534 'prev_text' => _x( 'Previous', 'previous set of posts' ),
2535 'next_text' => _x( 'Next', 'next set of posts' ),
2536 'screen_reader_text' => __( 'Posts navigation' ),
2539 // Make sure we get a string back. Plain is the next best thing.
2540 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2541 $args['type'] = 'plain';
2544 // Set up paginated links.
2545 $links = paginate_links( $args );
2548 $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2556 * Displays a paginated navigation to next/previous set of posts, when applicable.
2560 * @param array $args Optional. See get_the_posts_pagination() for available arguments.
2561 * Default empty array.
2563 function the_posts_pagination( $args = array() ) {
2564 echo get_the_posts_pagination( $args );
2568 * Wraps passed links in navigational markup.
2573 * @param string $links Navigational links.
2574 * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'.
2575 * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2576 * @return string Navigation template tag.
2578 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2579 if ( empty( $screen_reader_text ) ) {
2580 $screen_reader_text = __( 'Posts navigation' );
2584 <nav class="navigation %1$s" role="navigation">
2585 <h2 class="screen-reader-text">%2$s</h2>
2586 <div class="nav-links">%3$s</div>
2590 * Filters the navigation markup template.
2592 * Note: The filtered template HTML must contain specifiers for the navigation
2593 * class (%1$s), the screen-reader-text value (%2$s), and placement of the
2594 * navigation links (%3$s):
2596 * <nav class="navigation %1$s" role="navigation">
2597 * <h2 class="screen-reader-text">%2$s</h2>
2598 * <div class="nav-links">%3$s</div>
2603 * @param string $template The default template.
2604 * @param string $class The class passed by the calling function.
2605 * @return string Navigation template.
2607 $template = apply_filters( 'navigation_markup_template', $template, $class );
2609 return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2613 * Retrieves the comments page number link.
2617 * @global WP_Rewrite $wp_rewrite
2619 * @param int $pagenum Optional. Page number. Default 1.
2620 * @param int $max_page Optional. The maximum number of comment pages. Default 0.
2621 * @return string The comments page number link URL.
2623 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2626 $pagenum = (int) $pagenum;
2628 $result = get_permalink();
2630 if ( 'newest' == get_option('default_comments_page') ) {
2631 if ( $pagenum != $max_page ) {
2632 if ( $wp_rewrite->using_permalinks() )
2633 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2635 $result = add_query_arg( 'cpage', $pagenum, $result );
2637 } elseif ( $pagenum > 1 ) {
2638 if ( $wp_rewrite->using_permalinks() )
2639 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2641 $result = add_query_arg( 'cpage', $pagenum, $result );
2644 $result .= '#comments';
2647 * Filters the comments page number link for the current request.
2651 * @param string $result The comments page number link.
2653 return apply_filters( 'get_comments_pagenum_link', $result );
2657 * Retrieves the link to the next comments page.
2661 * @global WP_Query $wp_query
2663 * @param string $label Optional. Label for link text. Default empty.
2664 * @param int $max_page Optional. Max page. Default 0.
2665 * @return string|void HTML-formatted link for the next page of comments.
2667 function get_next_comments_link( $label = '', $max_page = 0 ) {
2670 if ( ! is_singular() )
2673 $page = get_query_var('cpage');
2679 $nextpage = intval($page) + 1;
2681 if ( empty($max_page) )
2682 $max_page = $wp_query->max_num_comment_pages;
2684 if ( empty($max_page) )
2685 $max_page = get_comment_pages_count();
2687 if ( $nextpage > $max_page )
2690 if ( empty($label) )
2691 $label = __('Newer Comments »');
2694 * Filters the anchor tag attributes for the next comments page link.
2698 * @param string $attributes Attributes for the anchor tag.
2700 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>';
2704 * Displays the link to the next comments page.
2708 * @param string $label Optional. Label for link text. Default empty.
2709 * @param int $max_page Optional. Max page. Default 0.
2711 function next_comments_link( $label = '', $max_page = 0 ) {
2712 echo get_next_comments_link( $label, $max_page );
2716 * Retrieves the link to the previous comments page.
2720 * @param string $label Optional. Label for comments link text. Default empty.
2721 * @return string|void HTML-formatted link for the previous page of comments.
2723 function get_previous_comments_link( $label = '' ) {
2724 if ( ! is_singular() )
2727 $page = get_query_var('cpage');
2729 if ( intval($page) <= 1 )
2732 $prevpage = intval($page) - 1;
2734 if ( empty($label) )
2735 $label = __('« Older Comments');
2738 * Filters the anchor tag attributes for the previous comments page link.
2742 * @param string $attributes Attributes for the anchor tag.
2744 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>';
2748 * Displays the link to the previous comments page.
2752 * @param string $label Optional. Label for comments link text. Default empty.
2754 function previous_comments_link( $label = '' ) {
2755 echo get_previous_comments_link( $label );
2759 * Displays or retrieves pagination links for the comments on the current post.
2761 * @see paginate_links()
2764 * @global WP_Rewrite $wp_rewrite
2766 * @param string|array $args Optional args. See paginate_links(). Default empty array.
2767 * @return string|void Markup for pagination links.
2769 function paginate_comments_links( $args = array() ) {
2772 if ( ! is_singular() )
2775 $page = get_query_var('cpage');
2778 $max_page = get_comment_pages_count();
2780 'base' => add_query_arg( 'cpage', '%#%' ),
2782 'total' => $max_page,
2785 'add_fragment' => '#comments'
2787 if ( $wp_rewrite->using_permalinks() )
2788 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2790 $args = wp_parse_args( $args, $defaults );
2791 $page_links = paginate_links( $args );
2793 if ( $args['echo'] )
2800 * Retrieves navigation to next/previous set of comments, when applicable.
2804 * @param array $args {
2805 * Optional. Default comments navigation arguments.
2807 * @type string $prev_text Anchor text to display in the previous comments link.
2808 * Default 'Older comments'.
2809 * @type string $next_text Anchor text to display in the next comments link.
2810 * Default 'Newer comments'.
2811 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2813 * @return string Markup for comments links.
2815 function get_the_comments_navigation( $args = array() ) {
2818 // Are there comments to navigate through?
2819 if ( get_comment_pages_count() > 1 ) {
2820 $args = wp_parse_args( $args, array(
2821 'prev_text' => __( 'Older comments' ),
2822 'next_text' => __( 'Newer comments' ),
2823 'screen_reader_text' => __( 'Comments navigation' ),
2826 $prev_link = get_previous_comments_link( $args['prev_text'] );
2827 $next_link = get_next_comments_link( $args['next_text'] );
2830 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2834 $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2837 $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] );
2844 * Displays navigation to next/previous set of comments, when applicable.
2848 * @param array $args See get_the_comments_navigation() for available arguments. Default empty array.
2850 function the_comments_navigation( $args = array() ) {
2851 echo get_the_comments_navigation( $args );
2855 * Retrieves a paginated navigation to next/previous set of comments, when applicable.
2859 * @see paginate_comments_links()
2861 * @param array $args {
2862 * Optional. Default pagination arguments.
2864 * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2866 * @return string Markup for pagination links.
2868 function get_the_comments_pagination( $args = array() ) {
2870 $args = wp_parse_args( $args, array(
2871 'screen_reader_text' => __( 'Comments navigation' ),
2873 $args['echo'] = false;
2875 // Make sure we get plain links, so we get a string we can work with.
2876 $args['type'] = 'plain';
2878 $links = paginate_comments_links( $args );
2881 $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
2888 * Displays a paginated navigation to next/previous set of comments, when applicable.
2892 * @param array $args See get_the_comments_pagination() for available arguments. Default empty array.
2894 function the_comments_pagination( $args = array() ) {
2895 echo get_the_comments_pagination( $args );
2899 * Retrieves the Press This bookmarklet link.
2903 * @global bool $is_IE Whether the browser matches an Internet Explorer user agent.
2905 function get_shortcut_link() {
2908 include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2914 * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2915 * since they only support a max length of ~2000 characters for
2916 * bookmark[let] URLs, which is way to small for our smarter one.
2917 * Do update the version number so users do not get the "upgrade your
2918 * bookmarklet" notice when using PT in those browsers.
2920 $ua = $_SERVER['HTTP_USER_AGENT'];
2922 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2923 $url = wp_json_encode( admin_url( 'press-this.php' ) );
2925 $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2926 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2927 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . WP_Press_This::VERSION . '";' .
2928 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2929 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2933 if ( empty( $link ) ) {
2934 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2937 $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . WP_Press_This::VERSION );
2938 $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2942 $link = str_replace( array( "\r", "\n", "\t" ), '', $link );
2945 * Filters the Press This bookmarklet link.
2949 * @param string $link The Press This bookmarklet link.
2951 return apply_filters( 'shortcut_link', $link );
2955 * Retrieves the URL for the current site where the front end is accessible.
2957 * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
2958 * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
2959 * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
2963 * @param string $path Optional. Path relative to the home URL. Default empty.
2964 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
2965 * 'http', 'https', 'relative', 'rest', or null. Default null.
2966 * @return string Home URL link with optional path appended.
2968 function home_url( $path = '', $scheme = null ) {
2969 return get_home_url( null, $path, $scheme );
2973 * Retrieves the URL for a given site where the front end is accessible.
2975 * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
2976 * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
2977 * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
2981 * @global string $pagenow
2983 * @param int $blog_id Optional. Site ID. Default null (current site).
2984 * @param string $path Optional. Path relative to the home URL. Default empty.
2985 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
2986 * 'http', 'https', 'relative', 'rest', or null. Default null.
2987 * @return string Home URL link with optional path appended.
2989 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2992 $orig_scheme = $scheme;
2994 if ( empty( $blog_id ) || !is_multisite() ) {
2995 $url = get_option( 'home' );
2997 switch_to_blog( $blog_id );
2998 $url = get_option( 'home' );
2999 restore_current_blog();
3002 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
3003 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow )
3006 $scheme = parse_url( $url, PHP_URL_SCHEME );
3009 $url = set_url_scheme( $url, $scheme );
3011 if ( $path && is_string( $path ) )
3012 $url .= '/' . ltrim( $path, '/' );
3015 * Filters the home URL.
3019 * @param string $url The complete home URL including scheme and path.
3020 * @param string $path Path relative to the home URL. Blank string if no path is specified.
3021 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
3022 * 'relative', 'rest', or null.
3023 * @param int|null $blog_id Site ID, or null for the current site.
3025 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
3029 * Retrieves the URL for the current site where WordPress application files
3030 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3032 * Returns the 'site_url' option with the appropriate protocol, 'https' if
3033 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3038 * @param string $path Optional. Path relative to the site URL. Default empty.
3039 * @param string $scheme Optional. Scheme to give the site URL context. See set_url_scheme().
3040 * @return string Site URL link with optional path appended.
3042 function site_url( $path = '', $scheme = null ) {
3043 return get_site_url( null, $path, $scheme );
3047 * Retrieves the URL for a given site where WordPress application files
3048 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3050 * Returns the 'site_url' option with the appropriate protocol, 'https' if
3051 * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',
3052 * `is_ssl()` is overridden.
3056 * @param int $blog_id Optional. Site ID. Default null (current site).
3057 * @param string $path Optional. Path relative to the site URL. Default empty.
3058 * @param string $scheme Optional. Scheme to give the site URL context. Accepts
3059 * 'http', 'https', 'login', 'login_post', 'admin', or
3060 * 'relative'. Default null.
3061 * @return string Site URL link with optional path appended.
3063 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
3064 if ( empty( $blog_id ) || !is_multisite() ) {
3065 $url = get_option( 'siteurl' );
3067 switch_to_blog( $blog_id );
3068 $url = get_option( 'siteurl' );
3069 restore_current_blog();
3072 $url = set_url_scheme( $url, $scheme );
3074 if ( $path && is_string( $path ) )
3075 $url .= '/' . ltrim( $path, '/' );
3078 * Filters the site URL.
3082 * @param string $url The complete site URL including scheme and path.
3083 * @param string $path Path relative to the site URL. Blank string if no path is specified.
3084 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
3085 * 'login_post', 'admin', 'relative' or null.
3086 * @param int|null $blog_id Site ID, or null for the current site.
3088 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
3092 * Retrieves the URL to the admin area for the current site.
3096 * @param string $path Optional path relative to the admin URL.
3097 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3098 * 'http' or 'https' can be passed to force those schemes.
3099 * @return string Admin URL link with optional path appended.
3101 function admin_url( $path = '', $scheme = 'admin' ) {
3102 return get_admin_url( null, $path, $scheme );
3106 * Retrieves the URL to the admin area for a given site.
3110 * @param int $blog_id Optional. Site ID. Default null (current site).
3111 * @param string $path Optional. Path relative to the admin URL. Default empty.
3112 * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
3113 * to force those schemes. Default 'admin', which obeys
3114 * force_ssl_admin() and is_ssl().
3115 * @return string Admin URL link with optional path appended.
3117 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
3118 $url = get_site_url($blog_id, 'wp-admin/', $scheme);
3120 if ( $path && is_string( $path ) )
3121 $url .= ltrim( $path, '/' );
3124 * Filters the admin area URL.
3128 * @param string $url The complete admin area URL including scheme and path.
3129 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
3130 * @param int|null $blog_id Site ID, or null for the current site.
3132 return apply_filters( 'admin_url', $url, $path, $blog_id );
3136 * Retrieves the URL to the includes directory.
3140 * @param string $path Optional. Path relative to the includes URL. Default empty.
3141 * @param string $scheme Optional. Scheme to give the includes URL context. Accepts
3142 * 'http', 'https', or 'relative'. Default null.
3143 * @return string Includes URL link with optional path appended.
3145 function includes_url( $path = '', $scheme = null ) {
3146 $url = site_url( '/' . WPINC . '/', $scheme );
3148 if ( $path && is_string( $path ) )
3149 $url .= ltrim($path, '/');
3152 * Filters the URL to the includes directory.
3156 * @param string $url The complete URL to the includes directory including scheme and path.
3157 * @param string $path Path relative to the URL to the wp-includes directory. Blank string
3158 * if no path is specified.
3160 return apply_filters( 'includes_url', $url, $path );
3164 * Retrieves the URL to the content directory.
3168 * @param string $path Optional. Path relative to the content URL. Default empty.
3169 * @return string Content URL link with optional path appended.
3171 function content_url( $path = '' ) {
3172 $url = set_url_scheme( WP_CONTENT_URL );
3174 if ( $path && is_string( $path ) )
3175 $url .= '/' . ltrim($path, '/');
3178 * Filters the URL to the content directory.
3182 * @param string $url The complete URL to the content directory including scheme and path.
3183 * @param string $path Path relative to the URL to the content directory. Blank string
3184 * if no path is specified.
3186 return apply_filters( 'content_url', $url, $path);
3190 * Retrieves a URL within the plugins or mu-plugins directory.
3192 * Defaults to the plugins directory URL if no arguments are supplied.
3196 * @param string $path Optional. Extra path appended to the end of the URL, including
3197 * the relative directory if $plugin is supplied. Default empty.
3198 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
3199 * The URL will be relative to its directory. Default empty.
3200 * Typically this is done by passing `__FILE__` as the argument.
3201 * @return string Plugins URL link with optional paths appended.
3203 function plugins_url( $path = '', $plugin = '' ) {
3205 $path = wp_normalize_path( $path );
3206 $plugin = wp_normalize_path( $plugin );
3207 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
3209 if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
3210 $url = WPMU_PLUGIN_URL;
3212 $url = WP_PLUGIN_URL;
3215 $url = set_url_scheme( $url );
3217 if ( !empty($plugin) && is_string($plugin) ) {
3218 $folder = dirname(plugin_basename($plugin));
3219 if ( '.' != $folder )
3220 $url .= '/' . ltrim($folder, '/');
3223 if ( $path && is_string( $path ) )
3224 $url .= '/' . ltrim($path, '/');
3227 * Filters the URL to the plugins directory.
3231 * @param string $url The complete URL to the plugins directory including scheme and path.
3232 * @param string $path Path relative to the URL to the plugins directory. Blank string
3233 * if no path is specified.
3234 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
3237 return apply_filters( 'plugins_url', $url, $path, $plugin );
3241 * Retrieves the site URL for the current network.
3243 * Returns the site URL with the appropriate protocol, 'https' if
3244 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3249 * @see set_url_scheme()
3251 * @param string $path Optional. Path relative to the site URL. Default empty.
3252 * @param string $scheme Optional. Scheme to give the site URL context. Accepts
3253 * 'http', 'https', or 'relative'. Default null.
3254 * @return string Site URL link with optional path appended.
3256 function network_site_url( $path = '', $scheme = null ) {
3257 if ( ! is_multisite() )
3258 return site_url($path, $scheme);
3260 $current_network = get_network();
3262 if ( 'relative' == $scheme )
3263 $url = $current_network->path;
3265 $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
3267 if ( $path && is_string( $path ) )
3268 $url .= ltrim( $path, '/' );
3271 * Filters the network site URL.
3275 * @param string $url The complete network site URL including scheme and path.
3276 * @param string $path Path relative to the network site URL. Blank string if
3277 * no path is specified.
3278 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3279 * 'relative' or null.
3281 return apply_filters( 'network_site_url', $url, $path, $scheme );
3285 * Retrieves the home URL for the current network.
3287 * Returns the home URL with the appropriate protocol, 'https' is_ssl()
3288 * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3293 * @param string $path Optional. Path relative to the home URL. Default empty.
3294 * @param string $scheme Optional. Scheme to give the home URL context. Accepts
3295 * 'http', 'https', or 'relative'. Default null.
3296 * @return string Home URL link with optional path appended.
3298 function network_home_url( $path = '', $scheme = null ) {
3299 if ( ! is_multisite() )
3300 return home_url($path, $scheme);
3302 $current_network = get_network();
3303 $orig_scheme = $scheme;
3305 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
3306 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3308 if ( 'relative' == $scheme )
3309 $url = $current_network->path;
3311 $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
3313 if ( $path && is_string( $path ) )
3314 $url .= ltrim( $path, '/' );
3317 * Filters the network home URL.
3321 * @param string $url The complete network home URL including scheme and path.
3322 * @param string $path Path relative to the network home URL. Blank string
3323 * if no path is specified.
3324 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3325 * 'relative' or null.
3327 return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3331 * Retrieves the URL to the admin area for the network.
3335 * @param string $path Optional path relative to the admin URL. Default empty.
3336 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3337 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3338 * @return string Admin URL link with optional path appended.
3340 function network_admin_url( $path = '', $scheme = 'admin' ) {
3341 if ( ! is_multisite() )
3342 return admin_url( $path, $scheme );
3344 $url = network_site_url('wp-admin/network/', $scheme);
3346 if ( $path && is_string( $path ) )
3347 $url .= ltrim($path, '/');
3350 * Filters the network admin URL.
3354 * @param string $url The complete network admin URL including scheme and path.
3355 * @param string $path Path relative to the network admin URL. Blank string if
3356 * no path is specified.
3358 return apply_filters( 'network_admin_url', $url, $path );
3362 * Retrieves the URL to the admin area for the current user.
3366 * @param string $path Optional. Path relative to the admin URL. Default empty.
3367 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3368 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3369 * @return string Admin URL link with optional path appended.
3371 function user_admin_url( $path = '', $scheme = 'admin' ) {
3372 $url = network_site_url('wp-admin/user/', $scheme);
3374 if ( $path && is_string( $path ) )
3375 $url .= ltrim($path, '/');
3378 * Filters the user admin URL for the current user.
3382 * @param string $url The complete URL including scheme and path.
3383 * @param string $path Path relative to the URL. Blank string if
3384 * no path is specified.
3386 return apply_filters( 'user_admin_url', $url, $path );
3390 * Retrieves the URL to the admin area for either the current site or the network depending on context.
3394 * @param string $path Optional. Path relative to the admin URL. Default empty.
3395 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3396 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3397 * @return string Admin URL link with optional path appended.
3399 function self_admin_url( $path = '', $scheme = 'admin' ) {
3400 if ( is_network_admin() )
3401 return network_admin_url($path, $scheme);
3402 elseif ( is_user_admin() )
3403 return user_admin_url($path, $scheme);
3405 return admin_url($path, $scheme);
3409 * Sets the scheme for a URL.
3412 * @since 4.4.0 The 'rest' scheme was added.
3414 * @param string $url Absolute URL that includes a scheme
3415 * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
3416 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
3417 * @return string $url URL with chosen scheme.
3419 function set_url_scheme( $url, $scheme = null ) {
3420 $orig_scheme = $scheme;
3423 $scheme = is_ssl() ? 'https' : 'http';
3424 } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3425 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3426 } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3427 $scheme = is_ssl() ? 'https' : 'http';
3430 $url = trim( $url );
3431 if ( substr( $url, 0, 2 ) === '//' )
3432 $url = 'http:' . $url;
3434 if ( 'relative' == $scheme ) {
3435 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3436 if ( $url !== '' && $url[0] === '/' )
3437 $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3439 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3443 * Filters the resulting URL after setting the scheme.
3447 * @param string $url The complete URL including scheme and path.
3448 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3449 * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3450 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
3452 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3456 * Retrieves the URL to the user's dashboard.
3458 * If a user does not belong to any site, the global user dashboard is used. If the user
3459 * belongs to the current site, the dashboard for the current site is returned. If the user
3460 * cannot edit the current site, the dashboard to the user's primary site is returned.
3464 * @param int $user_id Optional. User ID. Defaults to current user.
3465 * @param string $path Optional path relative to the dashboard. Use only paths known to
3466 * both site and user admins. Default empty.
3467 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3468 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3469 * @return string Dashboard URL link with optional path appended.
3471 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3472 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3474 $blogs = get_blogs_of_user( $user_id );
3475 if ( ! is_super_admin() && empty($blogs) ) {
3476 $url = user_admin_url( $path, $scheme );
3477 } elseif ( ! is_multisite() ) {
3478 $url = admin_url( $path, $scheme );
3480 $current_blog = get_current_blog_id();
3481 if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3482 $url = admin_url( $path, $scheme );
3484 $active = get_active_blog_for_user( $user_id );
3486 $url = get_admin_url( $active->blog_id, $path, $scheme );
3488 $url = user_admin_url( $path, $scheme );
3493 * Filters the dashboard URL for a user.
3497 * @param string $url The complete URL including scheme and path.
3498 * @param int $user_id The user ID.
3499 * @param string $path Path relative to the URL. Blank string if no path is specified.
3500 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3501 * 'login_post', 'admin', 'relative' or null.
3503 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3507 * Retrieves the URL to the user's profile editor.
3511 * @param int $user_id Optional. User ID. Defaults to current user.
3512 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3513 * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3514 * @return string Dashboard URL link with optional path appended.
3516 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3517 $user_id = $user_id ? (int) $user_id : get_current_user_id();
3519 if ( is_user_admin() )
3520 $url = user_admin_url( 'profile.php', $scheme );
3521 elseif ( is_network_admin() )
3522 $url = network_admin_url( 'profile.php', $scheme );
3524 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3527 * Filters the URL for a user's profile editor.
3531 * @param string $url The complete URL including scheme and path.
3532 * @param int $user_id The user ID.
3533 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
3534 * 'login_post', 'admin', 'relative' or null.
3536 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3540 * Returns the canonical URL for a post.
3542 * When the post is the same as the current requested page the function will handle the
3543 * pagination arguments too.
3547 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
3548 * @return string|false The canonical URL, or false if the post does not exist or has not
3549 * been published yet.
3551 function wp_get_canonical_url( $post = null ) {
3552 $post = get_post( $post );
3558 if ( 'publish' !== $post->post_status ) {
3562 $canonical_url = get_permalink( $post );
3564 // If a canonical is being generated for the current page, make sure it has pagination if needed.
3565 if ( $post->ID === get_queried_object_id() ) {
3566 $page = get_query_var( 'page', 0 );
3568 if ( '' == get_option( 'permalink_structure' ) ) {
3569 $canonical_url = add_query_arg( 'page', $page, $canonical_url );
3571 $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
3575 $cpage = get_query_var( 'cpage', 0 );
3577 $canonical_url = get_comments_pagenum_link( $cpage );
3582 * Filters the canonical URL for a post.
3586 * @param string $string The post's canonical URL.
3587 * @param WP_Post $post Post object.
3589 return apply_filters( 'get_canonical_url', $canonical_url, $post );
3593 * Outputs rel=canonical for singular queries.
3596 * @since 4.6.0 Adjusted to use wp_get_canonical_url().
3598 function rel_canonical() {
3599 if ( ! is_singular() ) {
3603 $id = get_queried_object_id();
3609 $url = wp_get_canonical_url( $id );
3611 if ( ! empty( $url ) ) {
3612 echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
3617 * Returns a shortlink for a post, page, attachment, or site.
3619 * This function exists to provide a shortlink tag that all themes and plugins can target.
3620 * A plugin must hook in to provide the actual shortlinks. Default shortlink support is
3621 * limited to providing ?p= style links for posts. Plugins can short-circuit this function
3622 * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'}
3627 * @param int $id Optional. A post or site id. Default is 0, which means the current post or site.
3628 * @param string $context Optional. Whether the id is a 'site' id, 'post' id, or 'media' id. If 'post',
3629 * the post_type of the post is consulted. If 'query', the current query is consulted
3630 * to determine the id and context. Default 'post'.
3631 * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how
3632 * and whether to honor this. Default true.
3633 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks
3636 function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
3638 * Filters whether to preempt generating a shortlink for the given post.
3640 * Passing a truthy value to the filter will effectively short-circuit the
3641 * shortlink-generation process, returning that value instead.
3645 * @param bool|string $return Short-circuit return value. Either false or a URL string.
3646 * @param int $id Post ID, or 0 for the current post.
3647 * @param string $context The context for the link. One of 'post' or 'query',
3648 * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
3650 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3652 if ( false !== $shortlink ) {
3657 if ( 'query' == $context && is_singular() ) {
3658 $post_id = get_queried_object_id();
3659 $post = get_post( $post_id );
3660 } elseif ( 'post' == $context ) {
3661 $post = get_post( $id );
3662 if ( ! empty( $post->ID ) )
3663 $post_id = $post->ID;
3668 // Return p= link for all public post types.
3669 if ( ! empty( $post_id ) ) {
3670 $post_type = get_post_type_object( $post->post_type );
3672 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3673 $shortlink = home_url( '/' );
3674 } elseif ( $post_type->public ) {
3675 $shortlink = home_url( '?p=' . $post_id );
3680 * Filters the shortlink for a post.
3684 * @param string $shortlink Shortlink URL.
3685 * @param int $id Post ID, or 0 for the current post.
3686 * @param string $context The context for the link. One of 'post' or 'query',
3687 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3689 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3693 * Injects rel=shortlink into the head if a shortlink is defined for the current page.
3695 * Attached to the {@see 'wp_head'} action.
3699 function wp_shortlink_wp_head() {
3700 $shortlink = wp_get_shortlink( 0, 'query' );
3702 if ( empty( $shortlink ) )
3705 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3709 * Sends a Link: rel=shortlink header if a shortlink is defined for the current page.
3711 * Attached to the {@see 'wp'} action.
3715 function wp_shortlink_header() {
3716 if ( headers_sent() )
3719 $shortlink = wp_get_shortlink(0, 'query');
3721 if ( empty($shortlink) )
3724 header('Link: <' . $shortlink . '>; rel=shortlink', false);
3728 * Displays the shortlink for a post.
3730 * Must be called from inside "The Loop"
3732 * Call like the_shortlink( __( 'Shortlinkage FTW' ) )
3736 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3737 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3738 * @param string $before Optional HTML to display before the link. Default empty.
3739 * @param string $after Optional HTML to display after the link. Default empty.
3741 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3744 if ( empty( $text ) )
3745 $text = __('This is the short link.');
3747 if ( empty( $title ) )
3748 $title = the_title_attribute( array( 'echo' => false ) );
3750 $shortlink = wp_get_shortlink( $post->ID );
3752 if ( !empty( $shortlink ) ) {
3753 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3756 * Filters the short link anchor tag for a post.
3760 * @param string $link Shortlink anchor tag.
3761 * @param string $shortlink Shortlink URL.
3762 * @param string $text Shortlink's text.
3763 * @param string $title Shortlink's title attribute.
3765 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3766 echo $before, $link, $after;
3772 * Retrieves the avatar URL.
3776 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3777 * user email, WP_User object, WP_Post object, or WP_Comment object.
3778 * @param array $args {
3779 * Optional. Arguments to return instead of the default arguments.
3781 * @type int $size Height and width of the avatar in pixels. Default 96.
3782 * @type string $default URL for the default image or a default type. Accepts '404' (return
3783 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3784 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3785 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3786 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3787 * 'avatar_default' option, with a fallback of 'mystery'.
3788 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3789 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3790 * judged in that order. Default is the value of the 'avatar_rating' option.
3791 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3793 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3794 * plus a "found_avatar" guess. Pass as a reference. Default null.
3796 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3798 function get_avatar_url( $id_or_email, $args = null ) {
3799 $args = get_avatar_data( $id_or_email, $args );
3800 return $args['url'];
3804 * Retrieves default data about the avatar.
3808 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3809 * user email, WP_User object, WP_Post object, or WP_Comment object.
3810 * @param array $args {
3811 * Optional. Arguments to return instead of the default arguments.
3813 * @type int $size Height and width of the avatar image file in pixels. Default 96.
3814 * @type int $height Display height of the avatar in pixels. Defaults to $size.
3815 * @type int $width Display width of the avatar in pixels. Defaults to $size.
3816 * @type string $default URL for the default image or a default type. Accepts '404' (return
3817 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3818 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3819 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3820 * 'gravatar_default' (the Gravatar logo). Default is the value of the
3821 * 'avatar_default' option, with a fallback of 'mystery'.
3822 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
3823 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3824 * judged in that order. Default is the value of the 'avatar_rating' option.
3825 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
3827 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
3828 * plus a "found_avatar" guess. Pass as a reference. Default null.
3829 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3831 * @return array $processed_args {
3832 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3834 * @type bool $found_avatar True if we were able to find an avatar for this user,
3835 * false or not set if we couldn't.
3836 * @type string $url The URL of the avatar we found.
3839 function get_avatar_data( $id_or_email, $args = null ) {
3840 $args = wp_parse_args( $args, array(
3844 'default' => get_option( 'avatar_default', 'mystery' ),
3845 'force_default' => false,
3846 'rating' => get_option( 'avatar_rating' ),
3848 'processed_args' => null, // if used, should be a reference
3852 if ( is_numeric( $args['size'] ) ) {
3853 $args['size'] = absint( $args['size'] );
3854 if ( ! $args['size'] ) {
3861 if ( is_numeric( $args['height'] ) ) {
3862 $args['height'] = absint( $args['height'] );
3863 if ( ! $args['height'] ) {
3864 $args['height'] = $args['size'];
3867 $args['height'] = $args['size'];
3870 if ( is_numeric( $args['width'] ) ) {
3871 $args['width'] = absint( $args['width'] );
3872 if ( ! $args['width'] ) {
3873 $args['width'] = $args['size'];
3876 $args['width'] = $args['size'];
3879 if ( empty( $args['default'] ) ) {
3880 $args['default'] = get_option( 'avatar_default', 'mystery' );
3883 switch ( $args['default'] ) {
3887 $args['default'] = 'mm';
3889 case 'gravatar_default' :
3890 $args['default'] = false;
3894 $args['force_default'] = (bool) $args['force_default'];
3896 $args['rating'] = strtolower( $args['rating'] );
3898 $args['found_avatar'] = false;
3901 * Filters whether to retrieve the avatar URL early.
3903 * Passing a non-null value in the 'url' member of the return array will
3904 * effectively short circuit get_avatar_data(), passing the value through
3905 * the {@see 'get_avatar_data'} filter and returning early.
3909 * @param array $args Arguments passed to get_avatar_data(), after processing.
3910 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3911 * user email, WP_User object, WP_Post object, or WP_Comment object.
3913 $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3915 if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3916 /** This filter is documented in wp-includes/link-template.php */
3917 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3921 $user = $email = false;
3923 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3924 $id_or_email = get_comment( $id_or_email );
3927 // Process the user identifier.
3928 if ( is_numeric( $id_or_email ) ) {
3929 $user = get_user_by( 'id', absint( $id_or_email ) );
3930 } elseif ( is_string( $id_or_email ) ) {
3931 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3933 list( $email_hash ) = explode( '@', $id_or_email );
3936 $email = $id_or_email;
3938 } elseif ( $id_or_email instanceof WP_User ) {
3940 $user = $id_or_email;
3941 } elseif ( $id_or_email instanceof WP_Post ) {
3943 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3944 } elseif ( $id_or_email instanceof WP_Comment ) {
3946 * Filters the list of allowed comment types for retrieving avatars.
3950 * @param array $types An array of content types. Default only contains 'comment'.
3952 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3953 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3954 $args['url'] = false;
3955 /** This filter is documented in wp-includes/link-template.php */
3956 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3959 if ( ! empty( $id_or_email->user_id ) ) {
3960 $user = get_user_by( 'id', (int) $id_or_email->user_id );
3962 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3963 $email = $id_or_email->comment_author_email;
3967 if ( ! $email_hash ) {
3969 $email = $user->user_email;
3973 $email_hash = md5( strtolower( trim( $email ) ) );
3977 if ( $email_hash ) {
3978 $args['found_avatar'] = true;
3979 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3981 $gravatar_server = rand( 0, 2 );
3985 's' => $args['size'],
3986 'd' => $args['default'],
3987 'f' => $args['force_default'] ? 'y' : false,
3988 'r' => $args['rating'],
3992 $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
3994 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
3997 $url = add_query_arg(
3998 rawurlencode_deep( array_filter( $url_args ) ),
3999 set_url_scheme( $url, $args['scheme'] )
4003 * Filters the avatar URL.
4007 * @param string $url The URL of the avatar.
4008 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4009 * user email, WP_User object, WP_Post object, or WP_Comment object.
4010 * @param array $args Arguments passed to get_avatar_data(), after processing.
4012 $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
4015 * Filters the avatar data.
4019 * @param array $args Arguments passed to get_avatar_data(), after processing.
4020 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4021 * user email, WP_User object, WP_Post object, or WP_Comment object.
4023 return apply_filters( 'get_avatar_data', $args, $id_or_email );
4027 * Retrieves the URL of a file in the theme.
4029 * Searches in the stylesheet directory before the template directory so themes
4030 * which inherit from a parent theme can just override one file.
4034 * @param string $file Optional. File to search for in the stylesheet directory.
4035 * @return string The URL of the file.
4037 function get_theme_file_uri( $file = '' ) {
4038 $file = ltrim( $file, '/' );
4040 if ( empty( $file ) ) {
4041 $url = get_stylesheet_directory_uri();
4042 } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
4043 $url = get_stylesheet_directory_uri() . '/' . $file;
4045 $url = get_template_directory_uri() . '/' . $file;
4049 * Filters the URL to a file in the theme.
4053 * @param string $url The file URL.
4054 * @param string $file The requested file to search for.
4056 return apply_filters( 'theme_file_uri', $url, $file );
4060 * Retrieves the URL of a file in the parent theme.
4064 * @param string $file Optional. File to return the URL for in the template directory.
4065 * @return string The URL of the file.
4067 function get_parent_theme_file_uri( $file = '' ) {
4068 $file = ltrim( $file, '/' );
4070 if ( empty( $file ) ) {
4071 $url = get_template_directory_uri();
4073 $url = get_template_directory_uri() . '/' . $file;
4077 * Filters the URL to a file in the parent theme.
4081 * @param string $url The file URL.
4082 * @param string $file The requested file to search for.
4084 return apply_filters( 'parent_theme_file_uri', $url, $file );
4088 * Retrieves the path of a file in the theme.
4090 * Searches in the stylesheet directory before the template directory so themes
4091 * which inherit from a parent theme can just override one file.
4095 * @param string $file Optional. File to search for in the stylesheet directory.
4096 * @return string The path of the file.
4098 function get_theme_file_path( $file = '' ) {
4099 $file = ltrim( $file, '/' );
4101 if ( empty( $file ) ) {
4102 $path = get_stylesheet_directory();
4103 } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
4104 $path = get_stylesheet_directory() . '/' . $file;
4106 $path = get_template_directory() . '/' . $file;
4110 * Filters the path to a file in the theme.
4114 * @param string $path The file path.
4115 * @param string $file The requested file to search for.
4117 return apply_filters( 'theme_file_path', $path, $file );
4121 * Retrieves the path of a file in the parent theme.
4125 * @param string $file Optional. File to return the path for in the template directory.
4126 * @return string The path of the file.
4128 function get_parent_theme_file_path( $file = '' ) {
4129 $file = ltrim( $file, '/' );
4131 if ( empty( $file ) ) {
4132 $path = get_template_directory();
4134 $path = get_template_directory() . '/' . $file;
4138 * Filters the path to a file in the parent theme.
4142 * @param string $path The file path.
4143 * @param string $file The requested file to search for.
4145 return apply_filters( 'parent_theme_file_path', $path, $file );