[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Link Template Functions 4 * 5 * @package WordPress 6 * @subpackage Template 7 */ 8 9 /** 10 * Displays the permalink for the current post. 11 * 12 * @since 1.2.0 13 * @since 4.4.0 Added the `$post` parameter. 14 * 15 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 16 */ 17 function the_permalink( $post = 0 ) { 18 /** 19 * Filters the display of the permalink for the current post. 20 * 21 * @since 1.5.0 22 * @since 4.4.0 Added the `$post` parameter. 23 * 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. 26 */ 27 echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) ); 28 } 29 30 /** 31 * Retrieves a trailing-slashed string if the site is set for adding trailing slashes. 32 * 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. 37 * 38 * @since 2.2.0 39 * 40 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 41 * 42 * @param string $url 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. 46 */ 47 function user_trailingslashit( $url, $type_of_url = '' ) { 48 global $wp_rewrite; 49 if ( $wp_rewrite->use_trailing_slashes ) { 50 $url = trailingslashit( $url ); 51 } else { 52 $url = untrailingslashit( $url ); 53 } 54 55 /** 56 * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes. 57 * 58 * @since 2.2.0 59 * 60 * @param string $url URL with or without a trailing slash. 61 * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback', 62 * 'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed', 63 * 'category', 'page', 'year', 'month', 'day', 'post_type_archive'. 64 */ 65 return apply_filters( 'user_trailingslashit', $url, $type_of_url ); 66 } 67 68 /** 69 * Displays the permalink anchor for the current post. 70 * 71 * The permalink mode title will use the post title for the 'a' element 'id' 72 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute. 73 * 74 * @since 0.71 75 * 76 * @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'. 77 */ 78 function permalink_anchor( $mode = 'id' ) { 79 $post = get_post(); 80 switch ( strtolower( $mode ) ) { 81 case 'title': 82 $title = sanitize_title( $post->post_title ) . '-' . $post->ID; 83 echo '<a id="' . $title . '"></a>'; 84 break; 85 case 'id': 86 default: 87 echo '<a id="post-' . $post->ID . '"></a>'; 88 break; 89 } 90 } 91 92 /** 93 * Determine whether post should always use a plain permalink structure. 94 * 95 * @since 5.7.0 96 * 97 * @param WP_Post|int|null $post Optional. Post ID or post object. Defaults to global $post. 98 * @param bool|null $sample Optional. Whether to force consideration based on sample links. 99 * If omitted, a sample link is generated if a post object is passed 100 * with the filter property set to 'sample'. 101 * @return bool Whether to use a plain permalink structure. 102 */ 103 function wp_force_plain_post_permalink( $post = null, $sample = null ) { 104 if ( 105 null === $sample && 106 is_object( $post ) && 107 isset( $post->filter ) && 108 'sample' === $post->filter 109 ) { 110 $sample = true; 111 } else { 112 $post = get_post( $post ); 113 $sample = null !== $sample ? $sample : false; 114 } 115 116 if ( ! $post ) { 117 return true; 118 } 119 120 $post_status_obj = get_post_status_object( get_post_status( $post ) ); 121 $post_type_obj = get_post_type_object( get_post_type( $post ) ); 122 123 if ( ! $post_status_obj || ! $post_type_obj ) { 124 return true; 125 } 126 127 if ( 128 // Publicly viewable links never have plain permalinks. 129 is_post_status_viewable( $post_status_obj ) || 130 ( 131 // Private posts don't have plain permalinks if the user can read them. 132 $post_status_obj->private && 133 current_user_can( 'read_post', $post->ID ) 134 ) || 135 // Protected posts don't have plain links if getting a sample URL. 136 ( $post_status_obj->protected && $sample ) 137 ) { 138 return false; 139 } 140 141 return true; 142 } 143 144 /** 145 * Retrieves the full permalink for the current post or post ID. 146 * 147 * This function is an alias for get_permalink(). 148 * 149 * @since 3.9.0 150 * 151 * @see get_permalink() 152 * 153 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 154 * @param bool $leavename Optional. Whether to keep post name or page name. Default false. 155 * @return string|false The permalink URL. False if the post does not exist. 156 */ 157 function get_the_permalink( $post = 0, $leavename = false ) { 158 return get_permalink( $post, $leavename ); 159 } 160 161 /** 162 * Retrieves the full permalink for the current post or post ID. 163 * 164 * @since 1.0.0 165 * 166 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 167 * @param bool $leavename Optional. Whether to keep post name or page name. Default false. 168 * @return string|false The permalink URL. False if the post does not exist. 169 */ 170 function get_permalink( $post = 0, $leavename = false ) { 171 $rewritecode = array( 172 '%year%', 173 '%monthnum%', 174 '%day%', 175 '%hour%', 176 '%minute%', 177 '%second%', 178 $leavename ? '' : '%postname%', 179 '%post_id%', 180 '%category%', 181 '%author%', 182 $leavename ? '' : '%pagename%', 183 ); 184 185 if ( is_object( $post ) && isset( $post->filter ) && 'sample' === $post->filter ) { 186 $sample = true; 187 } else { 188 $post = get_post( $post ); 189 $sample = false; 190 } 191 192 if ( empty( $post->ID ) ) { 193 return false; 194 } 195 196 if ( 'page' === $post->post_type ) { 197 return get_page_link( $post, $leavename, $sample ); 198 } elseif ( 'attachment' === $post->post_type ) { 199 return get_attachment_link( $post, $leavename ); 200 } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) { 201 return get_post_permalink( $post, $leavename, $sample ); 202 } 203 204 $permalink = get_option( 'permalink_structure' ); 205 206 /** 207 * Filters the permalink structure for a post before token replacement occurs. 208 * 209 * Only applies to posts with post_type of 'post'. 210 * 211 * @since 3.0.0 212 * 213 * @param string $permalink The site's permalink structure. 214 * @param WP_Post $post The post in question. 215 * @param bool $leavename Whether to keep the post name. 216 */ 217 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); 218 219 if ( 220 $permalink && 221 ! wp_force_plain_post_permalink( $post ) 222 ) { 223 224 $category = ''; 225 if ( str_contains( $permalink, '%category%' ) ) { 226 $cats = get_the_category( $post->ID ); 227 if ( $cats ) { 228 $cats = wp_list_sort( 229 $cats, 230 array( 231 'term_id' => 'ASC', 232 ) 233 ); 234 235 /** 236 * Filters the category that gets used in the %category% permalink token. 237 * 238 * @since 3.5.0 239 * 240 * @param WP_Term $cat The category to use in the permalink. 241 * @param array $cats Array of all categories (WP_Term objects) associated with the post. 242 * @param WP_Post $post The post in question. 243 */ 244 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); 245 246 $category_object = get_term( $category_object, 'category' ); 247 $category = $category_object->slug; 248 if ( $category_object->parent ) { 249 $category = get_category_parents( $category_object->parent, false, '/', true ) . $category; 250 } 251 } 252 /* 253 * Show default category in permalinks, 254 * without having to assign it explicitly. 255 */ 256 if ( empty( $category ) ) { 257 $default_category = get_term( get_option( 'default_category' ), 'category' ); 258 if ( $default_category && ! is_wp_error( $default_category ) ) { 259 $category = $default_category->slug; 260 } 261 } 262 } 263 264 $author = ''; 265 if ( str_contains( $permalink, '%author%' ) ) { 266 $authordata = get_userdata( $post->post_author ); 267 $author = $authordata->user_nicename; 268 } 269 270 /* 271 * This is not an API call because the permalink is based on the stored post_date value, 272 * which should be parsed as local time regardless of the default PHP timezone. 273 */ 274 $date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) ); 275 276 $rewritereplace = array( 277 $date[0], 278 $date[1], 279 $date[2], 280 $date[3], 281 $date[4], 282 $date[5], 283 $post->post_name, 284 $post->ID, 285 $category, 286 $author, 287 $post->post_name, 288 ); 289 290 $permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) ); 291 $permalink = user_trailingslashit( $permalink, 'single' ); 292 293 } else { // If they're not using the fancy permalink option. 294 $permalink = home_url( '?p=' . $post->ID ); 295 } 296 297 /** 298 * Filters the permalink for a post. 299 * 300 * Only applies to posts with post_type of 'post'. 301 * 302 * @since 1.5.0 303 * 304 * @param string $permalink The post's permalink. 305 * @param WP_Post $post The post in question. 306 * @param bool $leavename Whether to keep the post name. 307 */ 308 return apply_filters( 'post_link', $permalink, $post, $leavename ); 309 } 310 311 /** 312 * Retrieves the permalink for a post of a custom post type. 313 * 314 * @since 3.0.0 315 * @since 6.1.0 Returns false if the post does not exist. 316 * 317 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 318 * 319 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 320 * @param bool $leavename Optional. Whether to keep post name. Default false. 321 * @param bool $sample Optional. Is it a sample permalink. Default false. 322 * @return string|false The post permalink URL. False if the post does not exist. 323 */ 324 function get_post_permalink( $post = 0, $leavename = false, $sample = false ) { 325 global $wp_rewrite; 326 327 $post = get_post( $post ); 328 329 if ( ! $post ) { 330 return false; 331 } 332 333 $post_link = $wp_rewrite->get_extra_permastruct( $post->post_type ); 334 335 $slug = $post->post_name; 336 337 $force_plain_link = wp_force_plain_post_permalink( $post ); 338 339 $post_type = get_post_type_object( $post->post_type ); 340 341 if ( $post_type->hierarchical ) { 342 $slug = get_page_uri( $post ); 343 } 344 345 if ( ! empty( $post_link ) && ( ! $force_plain_link || $sample ) ) { 346 if ( ! $leavename ) { 347 $post_link = str_replace( "%$post->post_type%", $slug, $post_link ); 348 } 349 $post_link = home_url( user_trailingslashit( $post_link ) ); 350 } else { 351 if ( $post_type->query_var && ( isset( $post->post_status ) && ! $force_plain_link ) ) { 352 $post_link = add_query_arg( $post_type->query_var, $slug, '' ); 353 } else { 354 $post_link = add_query_arg( 355 array( 356 'post_type' => $post->post_type, 357 'p' => $post->ID, 358 ), 359 '' 360 ); 361 } 362 $post_link = home_url( $post_link ); 363 } 364 365 /** 366 * Filters the permalink for a post of a custom post type. 367 * 368 * @since 3.0.0 369 * 370 * @param string $post_link The post's permalink. 371 * @param WP_Post $post The post in question. 372 * @param bool $leavename Whether to keep the post name. 373 * @param bool $sample Is it a sample permalink. 374 */ 375 return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample ); 376 } 377 378 /** 379 * Retrieves the permalink for the current page or page ID. 380 * 381 * Respects page_on_front. Use this one. 382 * 383 * @since 1.5.0 384 * 385 * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`. 386 * @param bool $leavename Optional. Whether to keep the page name. Default false. 387 * @param bool $sample Optional. Whether it should be treated as a sample permalink. 388 * Default false. 389 * @return string The page permalink. 390 */ 391 function get_page_link( $post = false, $leavename = false, $sample = false ) { 392 $post = get_post( $post ); 393 394 if ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID ) { 395 $link = home_url( '/' ); 396 } else { 397 $link = _get_page_link( $post, $leavename, $sample ); 398 } 399 400 /** 401 * Filters the permalink for a page. 402 * 403 * @since 1.5.0 404 * 405 * @param string $link The page's permalink. 406 * @param int $post_id The ID of the page. 407 * @param bool $sample Is it a sample permalink. 408 */ 409 return apply_filters( 'page_link', $link, $post->ID, $sample ); 410 } 411 412 /** 413 * Retrieves the page permalink. 414 * 415 * Ignores page_on_front. Internal use only. 416 * 417 * @since 2.1.0 418 * @access private 419 * 420 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 421 * 422 * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`. 423 * @param bool $leavename Optional. Whether to keep the page name. Default false. 424 * @param bool $sample Optional. Whether it should be treated as a sample permalink. 425 * Default false. 426 * @return string The page permalink. 427 */ 428 function _get_page_link( $post = false, $leavename = false, $sample = false ) { 429 global $wp_rewrite; 430 431 $post = get_post( $post ); 432 433 $force_plain_link = wp_force_plain_post_permalink( $post ); 434 435 $link = $wp_rewrite->get_page_permastruct(); 436 437 if ( ! empty( $link ) && ( ( isset( $post->post_status ) && ! $force_plain_link ) || $sample ) ) { 438 if ( ! $leavename ) { 439 $link = str_replace( '%pagename%', get_page_uri( $post ), $link ); 440 } 441 442 $link = home_url( $link ); 443 $link = user_trailingslashit( $link, 'page' ); 444 } else { 445 $link = home_url( '?page_id=' . $post->ID ); 446 } 447 448 /** 449 * Filters the permalink for a non-page_on_front page. 450 * 451 * @since 2.1.0 452 * 453 * @param string $link The page's permalink. 454 * @param int $post_id The ID of the page. 455 */ 456 return apply_filters( '_get_page_link', $link, $post->ID ); 457 } 458 459 /** 460 * Retrieves the permalink for an attachment. 461 * 462 * This can be used in the WordPress Loop or outside of it. 463 * 464 * @since 2.0.0 465 * 466 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 467 * 468 * @param int|object $post Optional. Post ID or object. Default uses the global `$post`. 469 * @param bool $leavename Optional. Whether to keep the page name. Default false. 470 * @return string The attachment permalink. 471 */ 472 function get_attachment_link( $post = null, $leavename = false ) { 473 global $wp_rewrite; 474 475 $link = false; 476 477 $post = get_post( $post ); 478 $force_plain_link = wp_force_plain_post_permalink( $post ); 479 $parent_id = $post->post_parent; 480 $parent = $parent_id ? get_post( $parent_id ) : false; 481 $parent_valid = true; // Default for no parent. 482 if ( 483 $parent_id && 484 ( 485 $post->post_parent === $post->ID || 486 ! $parent || 487 ! is_post_type_viewable( get_post_type( $parent ) ) 488 ) 489 ) { 490 // Post is either its own parent or parent post unavailable. 491 $parent_valid = false; 492 } 493 494 if ( $force_plain_link || ! $parent_valid ) { 495 $link = false; 496 } elseif ( $wp_rewrite->using_permalinks() && $parent ) { 497 if ( 'page' === $parent->post_type ) { 498 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front. 499 } else { 500 $parentlink = get_permalink( $post->post_parent ); 501 } 502 503 if ( is_numeric( $post->post_name ) || str_contains( get_option( 'permalink_structure' ), '%category%' ) ) { 504 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker. 505 } else { 506 $name = $post->post_name; 507 } 508 509 if ( ! str_contains( $parentlink, '?' ) ) { 510 $link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' ); 511 } 512 513 if ( ! $leavename ) { 514 $link = str_replace( '%postname%', $name, $link ); 515 } 516 } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) { 517 $link = home_url( user_trailingslashit( $post->post_name ) ); 518 } 519 520 if ( ! $link ) { 521 $link = home_url( '/?attachment_id=' . $post->ID ); 522 } 523 524 /** 525 * Filters the permalink for an attachment. 526 * 527 * @since 2.0.0 528 * @since 5.6.0 Providing an empty string will now disable 529 * the view attachment page link on the media modal. 530 * 531 * @param string $link The attachment's permalink. 532 * @param int $post_id Attachment ID. 533 */ 534 return apply_filters( 'attachment_link', $link, $post->ID ); 535 } 536 537 /** 538 * Retrieves the permalink for the year archives. 539 * 540 * @since 1.5.0 541 * 542 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 543 * 544 * @param int|false $year Integer of year. False for current year. 545 * @return string The permalink for the specified year archive. 546 */ 547 function get_year_link( $year ) { 548 global $wp_rewrite; 549 if ( ! $year ) { 550 $year = current_time( 'Y' ); 551 } 552 $yearlink = $wp_rewrite->get_year_permastruct(); 553 if ( ! empty( $yearlink ) ) { 554 $yearlink = str_replace( '%year%', $year, $yearlink ); 555 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) ); 556 } else { 557 $yearlink = home_url( '?m=' . $year ); 558 } 559 560 /** 561 * Filters the year archive permalink. 562 * 563 * @since 1.5.0 564 * 565 * @param string $yearlink Permalink for the year archive. 566 * @param int $year Year for the archive. 567 */ 568 return apply_filters( 'year_link', $yearlink, $year ); 569 } 570 571 /** 572 * Retrieves the permalink for the month archives with year. 573 * 574 * @since 1.0.0 575 * 576 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 577 * 578 * @param int|false $year Integer of year. False for current year. 579 * @param int|false $month Integer of month. False for current month. 580 * @return string The permalink for the specified month and year archive. 581 */ 582 function get_month_link( $year, $month ) { 583 global $wp_rewrite; 584 if ( ! $year ) { 585 $year = current_time( 'Y' ); 586 } 587 if ( ! $month ) { 588 $month = current_time( 'm' ); 589 } 590 $monthlink = $wp_rewrite->get_month_permastruct(); 591 if ( ! empty( $monthlink ) ) { 592 $monthlink = str_replace( '%year%', $year, $monthlink ); 593 $monthlink = str_replace( '%monthnum%', zeroise( (int) $month, 2 ), $monthlink ); 594 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) ); 595 } else { 596 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) ); 597 } 598 599 /** 600 * Filters the month archive permalink. 601 * 602 * @since 1.5.0 603 * 604 * @param string $monthlink Permalink for the month archive. 605 * @param int $year Year for the archive. 606 * @param int $month The month for the archive. 607 */ 608 return apply_filters( 'month_link', $monthlink, $year, $month ); 609 } 610 611 /** 612 * Retrieves the permalink for the day archives with year and month. 613 * 614 * @since 1.0.0 615 * 616 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 617 * 618 * @param int|false $year Integer of year. False for current year. 619 * @param int|false $month Integer of month. False for current month. 620 * @param int|false $day Integer of day. False for current day. 621 * @return string The permalink for the specified day, month, and year archive. 622 */ 623 function get_day_link( $year, $month, $day ) { 624 global $wp_rewrite; 625 if ( ! $year ) { 626 $year = current_time( 'Y' ); 627 } 628 if ( ! $month ) { 629 $month = current_time( 'm' ); 630 } 631 if ( ! $day ) { 632 $day = current_time( 'j' ); 633 } 634 635 $daylink = $wp_rewrite->get_day_permastruct(); 636 if ( ! empty( $daylink ) ) { 637 $daylink = str_replace( '%year%', $year, $daylink ); 638 $daylink = str_replace( '%monthnum%', zeroise( (int) $month, 2 ), $daylink ); 639 $daylink = str_replace( '%day%', zeroise( (int) $day, 2 ), $daylink ); 640 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) ); 641 } else { 642 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) ); 643 } 644 645 /** 646 * Filters the day archive permalink. 647 * 648 * @since 1.5.0 649 * 650 * @param string $daylink Permalink for the day archive. 651 * @param int $year Year for the archive. 652 * @param int $month Month for the archive. 653 * @param int $day The day for the archive. 654 */ 655 return apply_filters( 'day_link', $daylink, $year, $month, $day ); 656 } 657 658 /** 659 * Displays the permalink for the feed type. 660 * 661 * @since 3.0.0 662 * 663 * @param string $anchor The link's anchor text. 664 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 665 * Default is the value of get_default_feed(). 666 */ 667 function the_feed_link( $anchor, $feed = '' ) { 668 $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>'; 669 670 /** 671 * Filters the feed link anchor tag. 672 * 673 * @since 3.0.0 674 * 675 * @param string $link The complete anchor tag for a feed link. 676 * @param string $feed The feed type. Possible values include 'rss2', 'atom', 677 * or an empty string for the default feed type. 678 */ 679 echo apply_filters( 'the_feed_link', $link, $feed ); 680 } 681 682 /** 683 * Retrieves the permalink for the feed type. 684 * 685 * @since 1.5.0 686 * 687 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 688 * 689 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 690 * Default is the value of get_default_feed(). 691 * @return string The feed permalink. 692 */ 693 function get_feed_link( $feed = '' ) { 694 global $wp_rewrite; 695 696 $permalink = $wp_rewrite->get_feed_permastruct(); 697 698 if ( $permalink ) { 699 if ( str_contains( $feed, 'comments_' ) ) { 700 $feed = str_replace( 'comments_', '', $feed ); 701 $permalink = $wp_rewrite->get_comment_feed_permastruct(); 702 } 703 704 if ( get_default_feed() === $feed ) { 705 $feed = ''; 706 } 707 708 $permalink = str_replace( '%feed%', $feed, $permalink ); 709 $permalink = preg_replace( '#/+#', '/', "/$permalink" ); 710 $output = home_url( user_trailingslashit( $permalink, 'feed' ) ); 711 } else { 712 if ( empty( $feed ) ) { 713 $feed = get_default_feed(); 714 } 715 716 if ( str_contains( $feed, 'comments_' ) ) { 717 $feed = str_replace( 'comments_', 'comments-', $feed ); 718 } 719 720 $output = home_url( "?feed={$feed}" ); 721 } 722 723 /** 724 * Filters the feed type permalink. 725 * 726 * @since 1.5.0 727 * 728 * @param string $output The feed permalink. 729 * @param string $feed The feed type. Possible values include 'rss2', 'atom', 730 * or an empty string for the default feed type. 731 */ 732 return apply_filters( 'feed_link', $output, $feed ); 733 } 734 735 /** 736 * Retrieves the permalink for the post comments feed. 737 * 738 * @since 2.2.0 739 * 740 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 741 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 742 * Default is the value of get_default_feed(). 743 * @return string The permalink for the comments feed for the given post on success, empty string on failure. 744 */ 745 function get_post_comments_feed_link( $post_id = 0, $feed = '' ) { 746 $post_id = absint( $post_id ); 747 748 if ( ! $post_id ) { 749 $post_id = get_the_ID(); 750 } 751 752 if ( empty( $feed ) ) { 753 $feed = get_default_feed(); 754 } 755 756 $post = get_post( $post_id ); 757 758 // Bail out if the post does not exist. 759 if ( ! $post instanceof WP_Post ) { 760 return ''; 761 } 762 763 $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent; 764 765 if ( get_option( 'permalink_structure' ) ) { 766 if ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post_id ) { 767 $url = _get_page_link( $post_id ); 768 } else { 769 $url = get_permalink( $post_id ); 770 } 771 772 if ( $unattached ) { 773 $url = home_url( '/feed/' ); 774 if ( get_default_feed() !== $feed ) { 775 $url .= "$feed/"; 776 } 777 $url = add_query_arg( 'attachment_id', $post_id, $url ); 778 } else { 779 $url = trailingslashit( $url ) . 'feed'; 780 if ( get_default_feed() !== $feed ) { 781 $url .= "/$feed"; 782 } 783 $url = user_trailingslashit( $url, 'single_feed' ); 784 } 785 } else { 786 if ( $unattached ) { 787 $url = add_query_arg( 788 array( 789 'feed' => $feed, 790 'attachment_id' => $post_id, 791 ), 792 home_url( '/' ) 793 ); 794 } elseif ( 'page' === $post->post_type ) { 795 $url = add_query_arg( 796 array( 797 'feed' => $feed, 798 'page_id' => $post_id, 799 ), 800 home_url( '/' ) 801 ); 802 } else { 803 $url = add_query_arg( 804 array( 805 'feed' => $feed, 806 'p' => $post_id, 807 ), 808 home_url( '/' ) 809 ); 810 } 811 } 812 813 /** 814 * Filters the post comments feed permalink. 815 * 816 * @since 1.5.1 817 * 818 * @param string $url Post comments feed permalink. 819 */ 820 return apply_filters( 'post_comments_feed_link', $url ); 821 } 822 823 /** 824 * Displays the comment feed link for a post. 825 * 826 * Prints out the comment feed link for a post. Link text is placed in the 827 * anchor. If no link text is specified, default text is used. If no post ID is 828 * specified, the current post is used. 829 * 830 * @since 2.5.0 831 * 832 * @param string $link_text Optional. Descriptive link text. Default 'Comments Feed'. 833 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 834 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 835 * Default is the value of get_default_feed(). 836 */ 837 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) { 838 $url = get_post_comments_feed_link( $post_id, $feed ); 839 if ( empty( $link_text ) ) { 840 $link_text = __( 'Comments Feed' ); 841 } 842 843 $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>'; 844 /** 845 * Filters the post comment feed link anchor tag. 846 * 847 * @since 2.8.0 848 * 849 * @param string $link The complete anchor tag for the comment feed link. 850 * @param int $post_id Post ID. 851 * @param string $feed The feed type. Possible values include 'rss2', 'atom', 852 * or an empty string for the default feed type. 853 */ 854 echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed ); 855 } 856 857 /** 858 * Retrieves the feed link for a given author. 859 * 860 * Returns a link to the feed for all posts by a given author. A specific feed 861 * can be requested or left blank to get the default feed. 862 * 863 * @since 2.5.0 864 * 865 * @param int $author_id Author ID. 866 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 867 * Default is the value of get_default_feed(). 868 * @return string Link to the feed for the author specified by $author_id. 869 */ 870 function get_author_feed_link( $author_id, $feed = '' ) { 871 $author_id = (int) $author_id; 872 $permalink_structure = get_option( 'permalink_structure' ); 873 874 if ( empty( $feed ) ) { 875 $feed = get_default_feed(); 876 } 877 878 if ( ! $permalink_structure ) { 879 $link = home_url( "?feed=$feed&author=" . $author_id ); 880 } else { 881 $link = get_author_posts_url( $author_id ); 882 if ( get_default_feed() === $feed ) { 883 $feed_link = 'feed'; 884 } else { 885 $feed_link = "feed/$feed"; 886 } 887 888 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' ); 889 } 890 891 /** 892 * Filters the feed link for a given author. 893 * 894 * @since 1.5.1 895 * 896 * @param string $link The author feed link. 897 * @param string $feed Feed type. Possible values include 'rss2', 'atom'. 898 */ 899 $link = apply_filters( 'author_feed_link', $link, $feed ); 900 901 return $link; 902 } 903 904 /** 905 * Retrieves the feed link for a category. 906 * 907 * Returns a link to the feed for all posts in a given category. A specific feed 908 * can be requested or left blank to get the default feed. 909 * 910 * @since 2.5.0 911 * 912 * @param int|WP_Term|object $cat The ID or category object whose feed link will be retrieved. 913 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 914 * Default is the value of get_default_feed(). 915 * @return string Link to the feed for the category specified by `$cat`. 916 */ 917 function get_category_feed_link( $cat, $feed = '' ) { 918 return get_term_feed_link( $cat, 'category', $feed ); 919 } 920 921 /** 922 * Retrieves the feed link for a term. 923 * 924 * Returns a link to the feed for all posts in a given term. A specific feed 925 * can be requested or left blank to get the default feed. 926 * 927 * @since 3.0.0 928 * 929 * @param int|WP_Term|object $term The ID or term object whose feed link will be retrieved. 930 * @param string $taxonomy Optional. Taxonomy of `$term_id`. 931 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 932 * Default is the value of get_default_feed(). 933 * @return string|false Link to the feed for the term specified by `$term` and `$taxonomy`. 934 */ 935 function get_term_feed_link( $term, $taxonomy = '', $feed = '' ) { 936 if ( ! is_object( $term ) ) { 937 $term = (int) $term; 938 } 939 940 $term = get_term( $term, $taxonomy ); 941 942 if ( empty( $term ) || is_wp_error( $term ) ) { 943 return false; 944 } 945 946 $taxonomy = $term->taxonomy; 947 948 if ( empty( $feed ) ) { 949 $feed = get_default_feed(); 950 } 951 952 $permalink_structure = get_option( 'permalink_structure' ); 953 954 if ( ! $permalink_structure ) { 955 if ( 'category' === $taxonomy ) { 956 $link = home_url( "?feed=$feed&cat=$term->term_id" ); 957 } elseif ( 'post_tag' === $taxonomy ) { 958 $link = home_url( "?feed=$feed&tag=$term->slug" ); 959 } else { 960 $t = get_taxonomy( $taxonomy ); 961 $link = home_url( "?feed=$feed&$t->query_var=$term->slug" ); 962 } 963 } else { 964 $link = get_term_link( $term, $term->taxonomy ); 965 if ( get_default_feed() === $feed ) { 966 $feed_link = 'feed'; 967 } else { 968 $feed_link = "feed/$feed"; 969 } 970 971 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' ); 972 } 973 974 if ( 'category' === $taxonomy ) { 975 /** 976 * Filters the category feed link. 977 * 978 * @since 1.5.1 979 * 980 * @param string $link The category feed link. 981 * @param string $feed Feed type. Possible values include 'rss2', 'atom'. 982 */ 983 $link = apply_filters( 'category_feed_link', $link, $feed ); 984 } elseif ( 'post_tag' === $taxonomy ) { 985 /** 986 * Filters the post tag feed link. 987 * 988 * @since 2.3.0 989 * 990 * @param string $link The tag feed link. 991 * @param string $feed Feed type. Possible values include 'rss2', 'atom'. 992 */ 993 $link = apply_filters( 'tag_feed_link', $link, $feed ); 994 } else { 995 /** 996 * Filters the feed link for a taxonomy other than 'category' or 'post_tag'. 997 * 998 * @since 3.0.0 999 * 1000 * @param string $link The taxonomy feed link. 1001 * @param string $feed Feed type. Possible values include 'rss2', 'atom'. 1002 * @param string $taxonomy The taxonomy name. 1003 */ 1004 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy ); 1005 } 1006 1007 return $link; 1008 } 1009 1010 /** 1011 * Retrieves the permalink for a tag feed. 1012 * 1013 * @since 2.3.0 1014 * 1015 * @param int|WP_Term|object $tag The ID or term object whose feed link will be retrieved. 1016 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 1017 * Default is the value of get_default_feed(). 1018 * @return string The feed permalink for the given tag. 1019 */ 1020 function get_tag_feed_link( $tag, $feed = '' ) { 1021 return get_term_feed_link( $tag, 'post_tag', $feed ); 1022 } 1023 1024 /** 1025 * Retrieves the edit link for a tag. 1026 * 1027 * @since 2.7.0 1028 * 1029 * @param int|WP_Term|object $tag The ID or term object whose edit link will be retrieved. 1030 * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'. 1031 * @return string The edit tag link URL for the given tag. 1032 */ 1033 function get_edit_tag_link( $tag, $taxonomy = 'post_tag' ) { 1034 /** 1035 * Filters the edit link for a tag (or term in another taxonomy). 1036 * 1037 * @since 2.7.0 1038 * 1039 * @param string $link The term edit link. 1040 */ 1041 return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag, $taxonomy ) ); 1042 } 1043 1044 /** 1045 * Displays or retrieves the edit link for a tag with formatting. 1046 * 1047 * @since 2.7.0 1048 * 1049 * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty. 1050 * @param string $before Optional. Display before edit link. Default empty. 1051 * @param string $after Optional. Display after edit link. Default empty. 1052 * @param WP_Term $tag Optional. Term object. If null, the queried object will be inspected. 1053 * Default null. 1054 */ 1055 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) { 1056 $link = edit_term_link( $link, '', '', $tag, false ); 1057 1058 /** 1059 * Filters the anchor tag for the edit link for a tag (or term in another taxonomy). 1060 * 1061 * @since 2.7.0 1062 * 1063 * @param string $link The anchor tag for the edit link. 1064 */ 1065 echo $before . apply_filters( 'edit_tag_link', $link ) . $after; 1066 } 1067 1068 /** 1069 * Retrieves the URL for editing a given term. 1070 * 1071 * @since 3.1.0 1072 * @since 4.5.0 The `$taxonomy` parameter was made optional. 1073 * 1074 * @param int|WP_Term|object $term The ID or term object whose edit link will be retrieved. 1075 * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified 1076 * by `$term`. 1077 * @param string $object_type Optional. The object type. Used to highlight the proper post type 1078 * menu on the linked page. Defaults to the first object_type associated 1079 * with the taxonomy. 1080 * @return string|null The edit term link URL for the given term, or null on failure. 1081 */ 1082 function get_edit_term_link( $term, $taxonomy = '', $object_type = '' ) { 1083 $term = get_term( $term, $taxonomy ); 1084 if ( ! $term || is_wp_error( $term ) ) { 1085 return; 1086 } 1087 1088 $tax = get_taxonomy( $term->taxonomy ); 1089 $term_id = $term->term_id; 1090 if ( ! $tax || ! current_user_can( 'edit_term', $term_id ) ) { 1091 return; 1092 } 1093 1094 $args = array( 1095 'taxonomy' => $taxonomy, 1096 'tag_ID' => $term_id, 1097 ); 1098 1099 if ( $object_type ) { 1100 $args['post_type'] = $object_type; 1101 } elseif ( ! empty( $tax->object_type ) ) { 1102 $args['post_type'] = reset( $tax->object_type ); 1103 } 1104 1105 if ( $tax->show_ui ) { 1106 $location = add_query_arg( $args, admin_url( 'term.php' ) ); 1107 } else { 1108 $location = ''; 1109 } 1110 1111 /** 1112 * Filters the edit link for a term. 1113 * 1114 * @since 3.1.0 1115 * 1116 * @param string $location The edit link. 1117 * @param int $term_id Term ID. 1118 * @param string $taxonomy Taxonomy name. 1119 * @param string $object_type The object type. 1120 */ 1121 return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type ); 1122 } 1123 1124 /** 1125 * Displays or retrieves the edit term link with formatting. 1126 * 1127 * @since 3.1.0 1128 * 1129 * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty. 1130 * @param string $before Optional. Display before edit link. Default empty. 1131 * @param string $after Optional. Display after edit link. Default empty. 1132 * @param int|WP_Term|null $term Optional. Term ID or object. If null, the queried object will be inspected. Default null. 1133 * @param bool $display Optional. Whether or not to echo the return. Default true. 1134 * @return string|void HTML content. 1135 */ 1136 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $display = true ) { 1137 if ( is_null( $term ) ) { 1138 $term = get_queried_object(); 1139 } else { 1140 $term = get_term( $term ); 1141 } 1142 1143 if ( ! $term ) { 1144 return; 1145 } 1146 1147 $tax = get_taxonomy( $term->taxonomy ); 1148 if ( ! current_user_can( 'edit_term', $term->term_id ) ) { 1149 return; 1150 } 1151 1152 if ( empty( $link ) ) { 1153 $link = __( 'Edit This' ); 1154 } 1155 1156 $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>'; 1157 1158 /** 1159 * Filters the anchor tag for the edit link of a term. 1160 * 1161 * @since 3.1.0 1162 * 1163 * @param string $link The anchor tag for the edit link. 1164 * @param int $term_id Term ID. 1165 */ 1166 $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after; 1167 1168 if ( $display ) { 1169 echo $link; 1170 } else { 1171 return $link; 1172 } 1173 } 1174 1175 /** 1176 * Retrieves the permalink for a search. 1177 * 1178 * @since 3.0.0 1179 * 1180 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1181 * 1182 * @param string $query Optional. The query string to use. If empty the current query is used. Default empty. 1183 * @return string The search permalink. 1184 */ 1185 function get_search_link( $query = '' ) { 1186 global $wp_rewrite; 1187 1188 if ( empty( $query ) ) { 1189 $search = get_search_query( false ); 1190 } else { 1191 $search = stripslashes( $query ); 1192 } 1193 1194 $permastruct = $wp_rewrite->get_search_permastruct(); 1195 1196 if ( empty( $permastruct ) ) { 1197 $link = home_url( '?s=' . urlencode( $search ) ); 1198 } else { 1199 $search = urlencode( $search ); 1200 $search = str_replace( '%2F', '/', $search ); // %2F(/) is not valid within a URL, send it un-encoded. 1201 $link = str_replace( '%search%', $search, $permastruct ); 1202 $link = home_url( user_trailingslashit( $link, 'search' ) ); 1203 } 1204 1205 /** 1206 * Filters the search permalink. 1207 * 1208 * @since 3.0.0 1209 * 1210 * @param string $link Search permalink. 1211 * @param string $search The URL-encoded search term. 1212 */ 1213 return apply_filters( 'search_link', $link, $search ); 1214 } 1215 1216 /** 1217 * Retrieves the permalink for the search results feed. 1218 * 1219 * @since 2.5.0 1220 * 1221 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1222 * 1223 * @param string $search_query Optional. Search query. Default empty. 1224 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 1225 * Default is the value of get_default_feed(). 1226 * @return string The search results feed permalink. 1227 */ 1228 function get_search_feed_link( $search_query = '', $feed = '' ) { 1229 global $wp_rewrite; 1230 $link = get_search_link( $search_query ); 1231 1232 if ( empty( $feed ) ) { 1233 $feed = get_default_feed(); 1234 } 1235 1236 $permastruct = $wp_rewrite->get_search_permastruct(); 1237 1238 if ( empty( $permastruct ) ) { 1239 $link = add_query_arg( 'feed', $feed, $link ); 1240 } else { 1241 $link = trailingslashit( $link ); 1242 $link .= "feed/$feed/"; 1243 } 1244 1245 /** 1246 * Filters the search feed link. 1247 * 1248 * @since 2.5.0 1249 * 1250 * @param string $link Search feed link. 1251 * @param string $feed Feed type. Possible values include 'rss2', 'atom'. 1252 * @param string $type The search type. One of 'posts' or 'comments'. 1253 */ 1254 return apply_filters( 'search_feed_link', $link, $feed, 'posts' ); 1255 } 1256 1257 /** 1258 * Retrieves the permalink for the search results comments feed. 1259 * 1260 * @since 2.5.0 1261 * 1262 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1263 * 1264 * @param string $search_query Optional. Search query. Default empty. 1265 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 1266 * Default is the value of get_default_feed(). 1267 * @return string The comments feed search results permalink. 1268 */ 1269 function get_search_comments_feed_link( $search_query = '', $feed = '' ) { 1270 global $wp_rewrite; 1271 1272 if ( empty( $feed ) ) { 1273 $feed = get_default_feed(); 1274 } 1275 1276 $link = get_search_feed_link( $search_query, $feed ); 1277 1278 $permastruct = $wp_rewrite->get_search_permastruct(); 1279 1280 if ( empty( $permastruct ) ) { 1281 $link = add_query_arg( 'feed', 'comments-' . $feed, $link ); 1282 } else { 1283 $link = add_query_arg( 'withcomments', 1, $link ); 1284 } 1285 1286 /** This filter is documented in wp-includes/link-template.php */ 1287 return apply_filters( 'search_feed_link', $link, $feed, 'comments' ); 1288 } 1289 1290 /** 1291 * Retrieves the permalink for a post type archive. 1292 * 1293 * @since 3.1.0 1294 * @since 4.5.0 Support for posts was added. 1295 * 1296 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1297 * 1298 * @param string $post_type Post type. 1299 * @return string|false The post type archive permalink. False if the post type 1300 * does not exist or does not have an archive. 1301 */ 1302 function get_post_type_archive_link( $post_type ) { 1303 global $wp_rewrite; 1304 1305 $post_type_obj = get_post_type_object( $post_type ); 1306 1307 if ( ! $post_type_obj ) { 1308 return false; 1309 } 1310 1311 if ( 'post' === $post_type ) { 1312 $show_on_front = get_option( 'show_on_front' ); 1313 $page_for_posts = get_option( 'page_for_posts' ); 1314 1315 if ( 'page' === $show_on_front && $page_for_posts ) { 1316 $link = get_permalink( $page_for_posts ); 1317 } else { 1318 $link = get_home_url(); 1319 } 1320 /** This filter is documented in wp-includes/link-template.php */ 1321 return apply_filters( 'post_type_archive_link', $link, $post_type ); 1322 } 1323 1324 if ( ! $post_type_obj->has_archive ) { 1325 return false; 1326 } 1327 1328 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) { 1329 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive; 1330 if ( $post_type_obj->rewrite['with_front'] ) { 1331 $struct = $wp_rewrite->front . $struct; 1332 } else { 1333 $struct = $wp_rewrite->root . $struct; 1334 } 1335 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) ); 1336 } else { 1337 $link = home_url( '?post_type=' . $post_type ); 1338 } 1339 1340 /** 1341 * Filters the post type archive permalink. 1342 * 1343 * @since 3.1.0 1344 * 1345 * @param string $link The post type archive permalink. 1346 * @param string $post_type Post type name. 1347 */ 1348 return apply_filters( 'post_type_archive_link', $link, $post_type ); 1349 } 1350 1351 /** 1352 * Retrieves the permalink for a post type archive feed. 1353 * 1354 * @since 3.1.0 1355 * 1356 * @param string $post_type Post type. 1357 * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. 1358 * Default is the value of get_default_feed(). 1359 * @return string|false The post type feed permalink. False if the post type 1360 * does not exist or does not have an archive. 1361 */ 1362 function get_post_type_archive_feed_link( $post_type, $feed = '' ) { 1363 $default_feed = get_default_feed(); 1364 if ( empty( $feed ) ) { 1365 $feed = $default_feed; 1366 } 1367 1368 $link = get_post_type_archive_link( $post_type ); 1369 if ( ! $link ) { 1370 return false; 1371 } 1372 1373 $post_type_obj = get_post_type_object( $post_type ); 1374 if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) { 1375 $link = trailingslashit( $link ); 1376 $link .= 'feed/'; 1377 if ( $feed !== $default_feed ) { 1378 $link .= "$feed/"; 1379 } 1380 } else { 1381 $link = add_query_arg( 'feed', $feed, $link ); 1382 } 1383 1384 /** 1385 * Filters the post type archive feed link. 1386 * 1387 * @since 3.1.0 1388 * 1389 * @param string $link The post type archive feed link. 1390 * @param string $feed Feed type. Possible values include 'rss2', 'atom'. 1391 */ 1392 return apply_filters( 'post_type_archive_feed_link', $link, $feed ); 1393 } 1394 1395 /** 1396 * Retrieves the URL used for the post preview. 1397 * 1398 * Allows additional query args to be appended. 1399 * 1400 * @since 4.4.0 1401 * 1402 * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global `$post`. 1403 * @param array $query_args Optional. Array of additional query args to be appended to the link. 1404 * Default empty array. 1405 * @param string $preview_link Optional. Base preview link to be used if it should differ from the 1406 * post permalink. Default empty. 1407 * @return string|null URL used for the post preview, or null if the post does not exist. 1408 */ 1409 function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) { 1410 $post = get_post( $post ); 1411 1412 if ( ! $post ) { 1413 return; 1414 } 1415 1416 $post_type_object = get_post_type_object( $post->post_type ); 1417 if ( is_post_type_viewable( $post_type_object ) ) { 1418 if ( ! $preview_link ) { 1419 $preview_link = set_url_scheme( get_permalink( $post ) ); 1420 } 1421 1422 $query_args['preview'] = 'true'; 1423 $preview_link = add_query_arg( $query_args, $preview_link ); 1424 } 1425 1426 /** 1427 * Filters the URL used for a post preview. 1428 * 1429 * @since 2.0.5 1430 * @since 4.0.0 Added the `$post` parameter. 1431 * 1432 * @param string $preview_link URL used for the post preview. 1433 * @param WP_Post $post Post object. 1434 */ 1435 return apply_filters( 'preview_post_link', $preview_link, $post ); 1436 } 1437 1438 /** 1439 * Retrieves the edit post link for post. 1440 * 1441 * Can be used within the WordPress loop or outside of it. Can be used with 1442 * pages, posts, attachments, revisions, global styles, templates, and template parts. 1443 * 1444 * @since 2.3.0 1445 * @since 6.3.0 Adds custom link for wp_navigation post types. 1446 * Adds custom links for wp_template_part and wp_template post types. 1447 * 1448 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 1449 * @param string $context Optional. How to output the '&' character. Default '&'. 1450 * @return string|null The edit post link for the given post. Null if the post type does not exist 1451 * or does not allow an editing UI. 1452 */ 1453 function get_edit_post_link( $post = 0, $context = 'display' ) { 1454 $post = get_post( $post ); 1455 1456 if ( ! $post ) { 1457 return; 1458 } 1459 1460 if ( 'revision' === $post->post_type ) { 1461 $action = ''; 1462 } elseif ( 'display' === $context ) { 1463 $action = '&action=edit'; 1464 } else { 1465 $action = '&action=edit'; 1466 } 1467 1468 $post_type_object = get_post_type_object( $post->post_type ); 1469 1470 if ( ! $post_type_object ) { 1471 return; 1472 } 1473 1474 if ( ! current_user_can( 'edit_post', $post->ID ) ) { 1475 return; 1476 } 1477 1478 $link = ''; 1479 1480 if ( 'wp_template' === $post->post_type || 'wp_template_part' === $post->post_type ) { 1481 $slug = urlencode( get_stylesheet() . '//' . $post->post_name ); 1482 $link = admin_url( sprintf( $post_type_object->_edit_link, $post->post_type, $slug ) ); 1483 } elseif ( 'wp_navigation' === $post->post_type ) { 1484 $link = admin_url( sprintf( $post_type_object->_edit_link, (string) $post->ID ) ); 1485 } elseif ( $post_type_object->_edit_link ) { 1486 $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ); 1487 } 1488 1489 /** 1490 * Filters the post edit link. 1491 * 1492 * @since 2.3.0 1493 * 1494 * @param string $link The edit link. 1495 * @param int $post_id Post ID. 1496 * @param string $context The link context. If set to 'display' then ampersands 1497 * are encoded. 1498 */ 1499 return apply_filters( 'get_edit_post_link', $link, $post->ID, $context ); 1500 } 1501 1502 /** 1503 * Displays the edit post link for post. 1504 * 1505 * @since 1.0.0 1506 * @since 4.4.0 The `$css_class` argument was added. 1507 * 1508 * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null. 1509 * @param string $before Optional. Display before edit link. Default empty. 1510 * @param string $after Optional. Display after edit link. Default empty. 1511 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 1512 * @param string $css_class Optional. Add custom class to link. Default 'post-edit-link'. 1513 */ 1514 function edit_post_link( $text = null, $before = '', $after = '', $post = 0, $css_class = 'post-edit-link' ) { 1515 $post = get_post( $post ); 1516 1517 if ( ! $post ) { 1518 return; 1519 } 1520 1521 $url = get_edit_post_link( $post->ID ); 1522 1523 if ( ! $url ) { 1524 return; 1525 } 1526 1527 if ( null === $text ) { 1528 $text = __( 'Edit This' ); 1529 } 1530 1531 $link = '<a class="' . esc_attr( $css_class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>'; 1532 1533 /** 1534 * Filters the post edit link anchor tag. 1535 * 1536 * @since 2.3.0 1537 * 1538 * @param string $link Anchor tag for the edit link. 1539 * @param int $post_id Post ID. 1540 * @param string $text Anchor text. 1541 */ 1542 echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after; 1543 } 1544 1545 /** 1546 * Retrieves the delete posts link for post. 1547 * 1548 * Can be used within the WordPress loop or outside of it, with any post type. 1549 * 1550 * @since 2.9.0 1551 * 1552 * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 1553 * @param string $deprecated Not used. 1554 * @param bool $force_delete Optional. Whether to bypass Trash and force deletion. Default false. 1555 * @return string|void The delete post link URL for the given post. 1556 */ 1557 function get_delete_post_link( $post = 0, $deprecated = '', $force_delete = false ) { 1558 if ( ! empty( $deprecated ) ) { 1559 _deprecated_argument( __FUNCTION__, '3.0.0' ); 1560 } 1561 1562 $post = get_post( $post ); 1563 1564 if ( ! $post ) { 1565 return; 1566 } 1567 1568 $post_type_object = get_post_type_object( $post->post_type ); 1569 1570 if ( ! $post_type_object ) { 1571 return; 1572 } 1573 1574 if ( ! current_user_can( 'delete_post', $post->ID ) ) { 1575 return; 1576 } 1577 1578 $action = ( $force_delete || ! EMPTY_TRASH_DAYS ) ? 'delete' : 'trash'; 1579 1580 $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) ); 1581 1582 /** 1583 * Filters the post delete link. 1584 * 1585 * @since 2.9.0 1586 * 1587 * @param string $link The delete link. 1588 * @param int $post_id Post ID. 1589 * @param bool $force_delete Whether to bypass the Trash and force deletion. Default false. 1590 */ 1591 return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete ); 1592 } 1593 1594 /** 1595 * Retrieves the edit comment link. 1596 * 1597 * @since 2.3.0 1598 * 1599 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object. 1600 * @return string|void The edit comment link URL for the given comment. 1601 */ 1602 function get_edit_comment_link( $comment_id = 0 ) { 1603 $comment = get_comment( $comment_id ); 1604 1605 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { 1606 return; 1607 } 1608 1609 $location = admin_url( 'comment.php?action=editcomment&c=' ) . $comment->comment_ID; 1610 1611 /** 1612 * Filters the comment edit link. 1613 * 1614 * @since 2.3.0 1615 * 1616 * @param string $location The edit link. 1617 */ 1618 return apply_filters( 'get_edit_comment_link', $location ); 1619 } 1620 1621 /** 1622 * Displays the edit comment link with formatting. 1623 * 1624 * @since 1.0.0 1625 * 1626 * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null. 1627 * @param string $before Optional. Display before edit link. Default empty. 1628 * @param string $after Optional. Display after edit link. Default empty. 1629 */ 1630 function edit_comment_link( $text = null, $before = '', $after = '' ) { 1631 $comment = get_comment(); 1632 1633 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { 1634 return; 1635 } 1636 1637 if ( null === $text ) { 1638 $text = __( 'Edit This' ); 1639 } 1640 1641 $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>'; 1642 1643 /** 1644 * Filters the comment edit link anchor tag. 1645 * 1646 * @since 2.3.0 1647 * 1648 * @param string $link Anchor tag for the edit link. 1649 * @param string $comment_id Comment ID as a numeric string. 1650 * @param string $text Anchor text. 1651 */ 1652 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after; 1653 } 1654 1655 /** 1656 * Displays the edit bookmark link. 1657 * 1658 * @since 2.7.0 1659 * 1660 * @param int|stdClass $link Optional. Bookmark ID. Default is the ID of the current bookmark. 1661 * @return string|void The edit bookmark link URL. 1662 */ 1663 function get_edit_bookmark_link( $link = 0 ) { 1664 $link = get_bookmark( $link ); 1665 1666 if ( ! current_user_can( 'manage_links' ) ) { 1667 return; 1668 } 1669 1670 $location = admin_url( 'link.php?action=edit&link_id=' ) . $link->link_id; 1671 1672 /** 1673 * Filters the bookmark edit link. 1674 * 1675 * @since 2.7.0 1676 * 1677 * @param string $location The edit link. 1678 * @param int $link_id Bookmark ID. 1679 */ 1680 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id ); 1681 } 1682 1683 /** 1684 * Displays the edit bookmark link anchor content. 1685 * 1686 * @since 2.7.0 1687 * 1688 * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty. 1689 * @param string $before Optional. Display before edit link. Default empty. 1690 * @param string $after Optional. Display after edit link. Default empty. 1691 * @param int $bookmark Optional. Bookmark ID. Default is the current bookmark. 1692 */ 1693 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) { 1694 $bookmark = get_bookmark( $bookmark ); 1695 1696 if ( ! current_user_can( 'manage_links' ) ) { 1697 return; 1698 } 1699 1700 if ( empty( $link ) ) { 1701 $link = __( 'Edit This' ); 1702 } 1703 1704 $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>'; 1705 1706 /** 1707 * Filters the bookmark edit link anchor tag. 1708 * 1709 * @since 2.7.0 1710 * 1711 * @param string $link Anchor tag for the edit link. 1712 * @param int $link_id Bookmark ID. 1713 */ 1714 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after; 1715 } 1716 1717 /** 1718 * Retrieves the edit user link. 1719 * 1720 * @since 3.5.0 1721 * 1722 * @param int $user_id Optional. User ID. Defaults to the current user. 1723 * @return string URL to edit user page or empty string. 1724 */ 1725 function get_edit_user_link( $user_id = null ) { 1726 if ( ! $user_id ) { 1727 $user_id = get_current_user_id(); 1728 } 1729 1730 if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) { 1731 return ''; 1732 } 1733 1734 $user = get_userdata( $user_id ); 1735 1736 if ( ! $user ) { 1737 return ''; 1738 } 1739 1740 if ( get_current_user_id() === $user->ID ) { 1741 $link = get_edit_profile_url( $user->ID ); 1742 } else { 1743 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) ); 1744 } 1745 1746 /** 1747 * Filters the user edit link. 1748 * 1749 * @since 3.5.0 1750 * 1751 * @param string $link The edit link. 1752 * @param int $user_id User ID. 1753 */ 1754 return apply_filters( 'get_edit_user_link', $link, $user->ID ); 1755 } 1756 1757 // 1758 // Navigation links. 1759 // 1760 1761 /** 1762 * Retrieves the previous post that is adjacent to the current post. 1763 * 1764 * @since 1.5.0 1765 * 1766 * @param bool $in_same_term Optional. Whether post should be in the same taxonomy term. 1767 * Default false. 1768 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1769 * Default empty. 1770 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 1771 * @return WP_Post|null|string Post object if successful. Null if global `$post` is not set. 1772 * Empty string if no corresponding post exists. 1773 */ 1774 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1775 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 1776 } 1777 1778 /** 1779 * Retrieves the next post that is adjacent to the current post. 1780 * 1781 * @since 1.5.0 1782 * 1783 * @param bool $in_same_term Optional. Whether post should be in the same taxonomy term. 1784 * Default false. 1785 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1786 * Default empty. 1787 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 1788 * @return WP_Post|null|string Post object if successful. Null if global `$post` is not set. 1789 * Empty string if no corresponding post exists. 1790 */ 1791 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1792 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 1793 } 1794 1795 /** 1796 * Retrieves the adjacent post. 1797 * 1798 * Can either be next or previous post. 1799 * 1800 * @since 2.5.0 1801 * 1802 * @global wpdb $wpdb WordPress database abstraction object. 1803 * 1804 * @param bool $in_same_term Optional. Whether post should be in the same taxonomy term. 1805 * Default false. 1806 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1807 * Default empty string. 1808 * @param bool $previous Optional. Whether to retrieve previous post. 1809 * Default true. 1810 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 1811 * @return WP_Post|null|string Post object if successful. Null if global `$post` is not set. 1812 * Empty string if no corresponding post exists. 1813 */ 1814 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1815 global $wpdb; 1816 1817 $post = get_post(); 1818 1819 if ( ! $post || ! taxonomy_exists( $taxonomy ) ) { 1820 return null; 1821 } 1822 1823 $current_post_date = $post->post_date; 1824 1825 $join = ''; 1826 $where = ''; 1827 $adjacent = $previous ? 'previous' : 'next'; 1828 1829 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { 1830 // Back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and ". 1831 if ( str_contains( $excluded_terms, ' and ' ) ) { 1832 _deprecated_argument( 1833 __FUNCTION__, 1834 '3.3.0', 1835 sprintf( 1836 /* translators: %s: The word 'and'. */ 1837 __( 'Use commas instead of %s to separate excluded terms.' ), 1838 "'and'" 1839 ) 1840 ); 1841 $excluded_terms = explode( ' and ', $excluded_terms ); 1842 } else { 1843 $excluded_terms = explode( ',', $excluded_terms ); 1844 } 1845 1846 $excluded_terms = array_map( 'intval', $excluded_terms ); 1847 } 1848 1849 /** 1850 * Filters the IDs of terms excluded from adjacent post queries. 1851 * 1852 * The dynamic portion of the hook name, `$adjacent`, refers to the type 1853 * of adjacency, 'next' or 'previous'. 1854 * 1855 * Possible hook names include: 1856 * 1857 * - `get_next_post_excluded_terms` 1858 * - `get_previous_post_excluded_terms` 1859 * 1860 * @since 4.4.0 1861 * 1862 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided. 1863 */ 1864 $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms ); 1865 1866 if ( $in_same_term || ! empty( $excluded_terms ) ) { 1867 if ( $in_same_term ) { 1868 $join .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; 1869 $where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy ); 1870 1871 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) { 1872 return ''; 1873 } 1874 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 1875 1876 // Remove any exclusions from the term array to include. 1877 $term_array = array_diff( $term_array, (array) $excluded_terms ); 1878 $term_array = array_map( 'intval', $term_array ); 1879 1880 if ( ! $term_array || is_wp_error( $term_array ) ) { 1881 return ''; 1882 } 1883 1884 $where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')'; 1885 } 1886 1887 if ( ! empty( $excluded_terms ) ) { 1888 $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 ) ) . ') )'; 1889 } 1890 } 1891 1892 // 'post_status' clause depends on the current user. 1893 if ( is_user_logged_in() ) { 1894 $user_id = get_current_user_id(); 1895 1896 $post_type_object = get_post_type_object( $post->post_type ); 1897 if ( empty( $post_type_object ) ) { 1898 $post_type_cap = $post->post_type; 1899 $read_private_cap = 'read_private_' . $post_type_cap . 's'; 1900 } else { 1901 $read_private_cap = $post_type_object->cap->read_private_posts; 1902 } 1903 1904 /* 1905 * Results should include private posts belonging to the current user, or private posts where the 1906 * current user has the 'read_private_posts' cap. 1907 */ 1908 $private_states = get_post_stati( array( 'private' => true ) ); 1909 $where .= " AND ( p.post_status = 'publish'"; 1910 foreach ( $private_states as $state ) { 1911 if ( current_user_can( $read_private_cap ) ) { 1912 $where .= $wpdb->prepare( ' OR p.post_status = %s', $state ); 1913 } else { 1914 $where .= $wpdb->prepare( ' OR (p.post_author = %d AND p.post_status = %s)', $user_id, $state ); 1915 } 1916 } 1917 $where .= ' )'; 1918 } else { 1919 $where .= " AND p.post_status = 'publish'"; 1920 } 1921 1922 $op = $previous ? '<' : '>'; 1923 $order = $previous ? 'DESC' : 'ASC'; 1924 1925 /** 1926 * Filters the JOIN clause in the SQL for an adjacent post query. 1927 * 1928 * The dynamic portion of the hook name, `$adjacent`, refers to the type 1929 * of adjacency, 'next' or 'previous'. 1930 * 1931 * Possible hook names include: 1932 * 1933 * - `get_next_post_join` 1934 * - `get_previous_post_join` 1935 * 1936 * @since 2.5.0 1937 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters. 1938 * 1939 * @param string $join The JOIN clause in the SQL. 1940 * @param bool $in_same_term Whether post should be in the same taxonomy term. 1941 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided. 1942 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 1943 * @param WP_Post $post WP_Post object. 1944 */ 1945 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post ); 1946 1947 /** 1948 * Filters the WHERE clause in the SQL for an adjacent post query. 1949 * 1950 * The dynamic portion of the hook name, `$adjacent`, refers to the type 1951 * of adjacency, 'next' or 'previous'. 1952 * 1953 * Possible hook names include: 1954 * 1955 * - `get_next_post_where` 1956 * - `get_previous_post_where` 1957 * 1958 * @since 2.5.0 1959 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters. 1960 * 1961 * @param string $where The `WHERE` clause in the SQL. 1962 * @param bool $in_same_term Whether post should be in the same taxonomy term. 1963 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided. 1964 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 1965 * @param WP_Post $post WP_Post object. 1966 */ 1967 $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 ); 1968 1969 /** 1970 * Filters the ORDER BY clause in the SQL for an adjacent post query. 1971 * 1972 * The dynamic portion of the hook name, `$adjacent`, refers to the type 1973 * of adjacency, 'next' or 'previous'. 1974 * 1975 * Possible hook names include: 1976 * 1977 * - `get_next_post_sort` 1978 * - `get_previous_post_sort` 1979 * 1980 * @since 2.5.0 1981 * @since 4.4.0 Added the `$post` parameter. 1982 * @since 4.9.0 Added the `$order` parameter. 1983 * 1984 * @param string $order_by The `ORDER BY` clause in the SQL. 1985 * @param WP_Post $post WP_Post object. 1986 * @param string $order Sort order. 'DESC' for previous post, 'ASC' for next. 1987 */ 1988 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order ); 1989 1990 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort"; 1991 $key = md5( $query ); 1992 $last_changed = wp_cache_get_last_changed( 'posts' ); 1993 if ( $in_same_term || ! empty( $excluded_terms ) ) { 1994 $last_changed .= wp_cache_get_last_changed( 'terms' ); 1995 } 1996 $cache_key = "adjacent_post:$key:$last_changed"; 1997 1998 $result = wp_cache_get( $cache_key, 'post-queries' ); 1999 if ( false !== $result ) { 2000 if ( $result ) { 2001 $result = get_post( $result ); 2002 } 2003 return $result; 2004 } 2005 2006 $result = $wpdb->get_var( $query ); 2007 if ( null === $result ) { 2008 $result = ''; 2009 } 2010 2011 wp_cache_set( $cache_key, $result, 'post-queries' ); 2012 2013 if ( $result ) { 2014 $result = get_post( $result ); 2015 } 2016 2017 return $result; 2018 } 2019 2020 /** 2021 * Retrieves the adjacent post relational link. 2022 * 2023 * Can either be next or previous post relational link. 2024 * 2025 * @since 2.8.0 2026 * 2027 * @param string $title Optional. Link title format. Default '%title'. 2028 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2029 * Default false. 2030 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2031 * Default empty. 2032 * @param bool $previous Optional. Whether to display link to previous or next post. 2033 * Default true. 2034 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2035 * @return string|void The adjacent post relational link URL. 2036 */ 2037 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 2038 $post = get_post(); 2039 if ( $previous && is_attachment() && $post ) { 2040 $post = get_post( $post->post_parent ); 2041 } else { 2042 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 2043 } 2044 2045 if ( empty( $post ) ) { 2046 return; 2047 } 2048 2049 $post_title = the_title_attribute( 2050 array( 2051 'echo' => false, 2052 'post' => $post, 2053 ) 2054 ); 2055 2056 if ( empty( $post_title ) ) { 2057 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 2058 } 2059 2060 $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 2061 2062 $title = str_replace( '%title', $post_title, $title ); 2063 $title = str_replace( '%date', $date, $title ); 2064 2065 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 2066 $link .= esc_attr( $title ); 2067 $link .= "' href='" . get_permalink( $post ) . "' />\n"; 2068 2069 $adjacent = $previous ? 'previous' : 'next'; 2070 2071 /** 2072 * Filters the adjacent post relational link. 2073 * 2074 * The dynamic portion of the hook name, `$adjacent`, refers to the type 2075 * of adjacency, 'next' or 'previous'. 2076 * 2077 * Possible hook names include: 2078 * 2079 * - `next_post_rel_link` 2080 * - `previous_post_rel_link` 2081 * 2082 * @since 2.8.0 2083 * 2084 * @param string $link The relational link. 2085 */ 2086 return apply_filters( "{$adjacent}_post_rel_link", $link ); 2087 } 2088 2089 /** 2090 * Displays the relational links for the posts adjacent to the current post. 2091 * 2092 * @since 2.8.0 2093 * 2094 * @param string $title Optional. Link title format. Default '%title'. 2095 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2096 * Default false. 2097 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2098 * Default empty. 2099 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2100 */ 2101 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 2102 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); 2103 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); 2104 } 2105 2106 /** 2107 * Displays relational links for the posts adjacent to the current post for single post pages. 2108 * 2109 * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins 2110 * or theme templates. 2111 * 2112 * @since 3.0.0 2113 * @since 5.6.0 No longer used in core. 2114 * 2115 * @see adjacent_posts_rel_link() 2116 */ 2117 function adjacent_posts_rel_link_wp_head() { 2118 if ( ! is_single() || is_attachment() ) { 2119 return; 2120 } 2121 adjacent_posts_rel_link(); 2122 } 2123 2124 /** 2125 * Displays the relational link for the next post adjacent to the current post. 2126 * 2127 * @since 2.8.0 2128 * 2129 * @see get_adjacent_post_rel_link() 2130 * 2131 * @param string $title Optional. Link title format. Default '%title'. 2132 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2133 * Default false. 2134 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2135 * Default empty. 2136 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2137 */ 2138 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 2139 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); 2140 } 2141 2142 /** 2143 * Displays the relational link for the previous post adjacent to the current post. 2144 * 2145 * @since 2.8.0 2146 * 2147 * @see get_adjacent_post_rel_link() 2148 * 2149 * @param string $title Optional. Link title format. Default '%title'. 2150 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2151 * Default false. 2152 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2153 * Default true. 2154 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2155 */ 2156 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 2157 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); 2158 } 2159 2160 /** 2161 * Retrieves the boundary post. 2162 * 2163 * Boundary being either the first or last post by publish date within the constraints specified 2164 * by `$in_same_term` or `$excluded_terms`. 2165 * 2166 * @since 2.8.0 2167 * 2168 * @param bool $in_same_term Optional. Whether returned post should be in the same taxonomy term. 2169 * Default false. 2170 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2171 * Default empty. 2172 * @param bool $start Optional. Whether to retrieve first or last post. 2173 * Default true. 2174 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2175 * @return array|null Array containing the boundary post object if successful, null otherwise. 2176 */ 2177 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 2178 $post = get_post(); 2179 2180 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) { 2181 return null; 2182 } 2183 2184 $query_args = array( 2185 'posts_per_page' => 1, 2186 'order' => $start ? 'ASC' : 'DESC', 2187 'update_post_term_cache' => false, 2188 'update_post_meta_cache' => false, 2189 ); 2190 2191 $term_array = array(); 2192 2193 if ( ! is_array( $excluded_terms ) ) { 2194 if ( ! empty( $excluded_terms ) ) { 2195 $excluded_terms = explode( ',', $excluded_terms ); 2196 } else { 2197 $excluded_terms = array(); 2198 } 2199 } 2200 2201 if ( $in_same_term || ! empty( $excluded_terms ) ) { 2202 if ( $in_same_term ) { 2203 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 2204 } 2205 2206 if ( ! empty( $excluded_terms ) ) { 2207 $excluded_terms = array_map( 'intval', $excluded_terms ); 2208 $excluded_terms = array_diff( $excluded_terms, $term_array ); 2209 2210 $inverse_terms = array(); 2211 foreach ( $excluded_terms as $excluded_term ) { 2212 $inverse_terms[] = $excluded_term * -1; 2213 } 2214 $excluded_terms = $inverse_terms; 2215 } 2216 2217 $query_args['tax_query'] = array( 2218 array( 2219 'taxonomy' => $taxonomy, 2220 'terms' => array_merge( $term_array, $excluded_terms ), 2221 ), 2222 ); 2223 } 2224 2225 return get_posts( $query_args ); 2226 } 2227 2228 /** 2229 * Retrieves the previous post link that is adjacent to the current post. 2230 * 2231 * @since 3.7.0 2232 * 2233 * @param string $format Optional. Link anchor format. Default '« %link'. 2234 * @param string $link Optional. Link permalink format. Default '%title'. 2235 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2236 * Default false. 2237 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2238 * Default empty. 2239 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2240 * @return string The link URL of the previous post in relation to the current post. 2241 */ 2242 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 2243 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 2244 } 2245 2246 /** 2247 * Displays the previous post link that is adjacent to the current post. 2248 * 2249 * @since 1.5.0 2250 * 2251 * @see get_previous_post_link() 2252 * 2253 * @param string $format Optional. Link anchor format. Default '« %link'. 2254 * @param string $link Optional. Link permalink format. Default '%title'. 2255 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2256 * Default false. 2257 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2258 * Default empty. 2259 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2260 */ 2261 function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 2262 echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); 2263 } 2264 2265 /** 2266 * Retrieves the next post link that is adjacent to the current post. 2267 * 2268 * @since 3.7.0 2269 * 2270 * @param string $format Optional. Link anchor format. Default '« %link'. 2271 * @param string $link Optional. Link permalink format. Default '%title'. 2272 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2273 * Default false. 2274 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2275 * Default empty. 2276 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2277 * @return string The link URL of the next post in relation to the current post. 2278 */ 2279 function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 2280 return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 2281 } 2282 2283 /** 2284 * Displays the next post link that is adjacent to the current post. 2285 * 2286 * @since 1.5.0 2287 * 2288 * @see get_next_post_link() 2289 * 2290 * @param string $format Optional. Link anchor format. Default '« %link'. 2291 * @param string $link Optional. Link permalink format. Default '%title'. 2292 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2293 * Default false. 2294 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 2295 * Default empty. 2296 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2297 */ 2298 function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 2299 echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); 2300 } 2301 2302 /** 2303 * Retrieves the adjacent post link. 2304 * 2305 * Can be either next post link or previous. 2306 * 2307 * @since 3.7.0 2308 * 2309 * @param string $format Link anchor format. 2310 * @param string $link Link permalink format. 2311 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2312 * Default false. 2313 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. 2314 * Default empty. 2315 * @param bool $previous Optional. Whether to display link to previous or next post. 2316 * Default true. 2317 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2318 * @return string The link URL of the previous or next post in relation to the current post. 2319 */ 2320 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 2321 if ( $previous && is_attachment() ) { 2322 $post = get_post( get_post()->post_parent ); 2323 } else { 2324 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 2325 } 2326 2327 if ( ! $post ) { 2328 $output = ''; 2329 } else { 2330 $title = $post->post_title; 2331 2332 if ( empty( $post->post_title ) ) { 2333 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 2334 } 2335 2336 /** This filter is documented in wp-includes/post-template.php */ 2337 $title = apply_filters( 'the_title', $title, $post->ID ); 2338 2339 $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 2340 $rel = $previous ? 'prev' : 'next'; 2341 2342 $string = '<a href="' . get_permalink( $post ) . '" rel="' . $rel . '">'; 2343 $inlink = str_replace( '%title', $title, $link ); 2344 $inlink = str_replace( '%date', $date, $inlink ); 2345 $inlink = $string . $inlink . '</a>'; 2346 2347 $output = str_replace( '%link', $inlink, $format ); 2348 } 2349 2350 $adjacent = $previous ? 'previous' : 'next'; 2351 2352 /** 2353 * Filters the adjacent post link. 2354 * 2355 * The dynamic portion of the hook name, `$adjacent`, refers to the type 2356 * of adjacency, 'next' or 'previous'. 2357 * 2358 * Possible hook names include: 2359 * 2360 * - `next_post_link` 2361 * - `previous_post_link` 2362 * 2363 * @since 2.6.0 2364 * @since 4.2.0 Added the `$adjacent` parameter. 2365 * 2366 * @param string $output The adjacent post link. 2367 * @param string $format Link anchor format. 2368 * @param string $link Link permalink format. 2369 * @param WP_Post|string $post The adjacent post. Empty string if no corresponding post exists. 2370 * @param string $adjacent Whether the post is previous or next. 2371 */ 2372 return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent ); 2373 } 2374 2375 /** 2376 * Displays the adjacent post link. 2377 * 2378 * Can be either next post link or previous. 2379 * 2380 * @since 2.5.0 2381 * 2382 * @param string $format Link anchor format. 2383 * @param string $link Link permalink format. 2384 * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. 2385 * Default false. 2386 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs. 2387 * Default empty. 2388 * @param bool $previous Optional. Whether to display link to previous or next post. 2389 * Default true. 2390 * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. 2391 */ 2392 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 2393 echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy ); 2394 } 2395 2396 /** 2397 * Retrieves the link for a page number. 2398 * 2399 * @since 1.5.0 2400 * 2401 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 2402 * 2403 * @param int $pagenum Optional. Page number. Default 1. 2404 * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). 2405 * If set to false, prepares the URL with sanitize_url(). Default true. 2406 * @return string The link URL for the given page number. 2407 */ 2408 function get_pagenum_link( $pagenum = 1, $escape = true ) { 2409 global $wp_rewrite; 2410 2411 $pagenum = (int) $pagenum; 2412 2413 $request = remove_query_arg( 'paged' ); 2414 2415 $home_root = parse_url( home_url() ); 2416 $home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : ''; 2417 $home_root = preg_quote( $home_root, '|' ); 2418 2419 $request = preg_replace( '|^' . $home_root . '|i', '', $request ); 2420 $request = preg_replace( '|^/+|', '', $request ); 2421 2422 if ( ! $wp_rewrite->using_permalinks() || is_admin() ) { 2423 $base = trailingslashit( get_bloginfo( 'url' ) ); 2424 2425 if ( $pagenum > 1 ) { 2426 $result = add_query_arg( 'paged', $pagenum, $base . $request ); 2427 } else { 2428 $result = $base . $request; 2429 } 2430 } else { 2431 $qs_regex = '|\?.*?$|'; 2432 preg_match( $qs_regex, $request, $qs_match ); 2433 2434 $parts = array(); 2435 $parts[] = untrailingslashit( get_bloginfo( 'url' ) ); 2436 2437 if ( ! empty( $qs_match[0] ) ) { 2438 $query_string = $qs_match[0]; 2439 $request = preg_replace( $qs_regex, '', $request ); 2440 } else { 2441 $query_string = ''; 2442 } 2443 2444 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request ); 2445 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request ); 2446 $request = ltrim( $request, '/' ); 2447 2448 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' !== $request ) ) { 2449 $parts[] = $wp_rewrite->index; 2450 } 2451 2452 $parts[] = untrailingslashit( $request ); 2453 2454 if ( $pagenum > 1 ) { 2455 $parts[] = $wp_rewrite->pagination_base; 2456 $parts[] = $pagenum; 2457 } 2458 2459 $result = user_trailingslashit( implode( '/', array_filter( $parts ) ), 'paged' ); 2460 if ( ! empty( $query_string ) ) { 2461 $result .= $query_string; 2462 } 2463 } 2464 2465 /** 2466 * Filters the page number link for the current request. 2467 * 2468 * @since 2.5.0 2469 * @since 5.2.0 Added the `$pagenum` argument. 2470 * 2471 * @param string $result The page number link. 2472 * @param int $pagenum The page number. 2473 */ 2474 $result = apply_filters( 'get_pagenum_link', $result, $pagenum ); 2475 2476 if ( $escape ) { 2477 return esc_url( $result ); 2478 } else { 2479 return sanitize_url( $result ); 2480 } 2481 } 2482 2483 /** 2484 * Retrieves the next posts page link. 2485 * 2486 * Backported from 2.1.3 to 2.0.10. 2487 * 2488 * @since 2.0.10 2489 * 2490 * @global int $paged 2491 * 2492 * @param int $max_page Optional. Max pages. Default 0. 2493 * @return string|void The link URL for next posts page. 2494 */ 2495 function get_next_posts_page_link( $max_page = 0 ) { 2496 global $paged; 2497 2498 if ( ! is_single() ) { 2499 if ( ! $paged ) { 2500 $paged = 1; 2501 } 2502 2503 $next_page = (int) $paged + 1; 2504 2505 if ( ! $max_page || $max_page >= $next_page ) { 2506 return get_pagenum_link( $next_page ); 2507 } 2508 } 2509 } 2510 2511 /** 2512 * Displays or retrieves the next posts page link. 2513 * 2514 * @since 0.71 2515 * 2516 * @param int $max_page Optional. Max pages. Default 0. 2517 * @param bool $display Optional. Whether to echo the link. Default true. 2518 * @return string|void The link URL for next posts page if `$display = false`. 2519 */ 2520 function next_posts( $max_page = 0, $display = true ) { 2521 $link = get_next_posts_page_link( $max_page ); 2522 $output = $link ? esc_url( $link ) : ''; 2523 2524 if ( $display ) { 2525 echo $output; 2526 } else { 2527 return $output; 2528 } 2529 } 2530 2531 /** 2532 * Retrieves the next posts page link. 2533 * 2534 * @since 2.7.0 2535 * 2536 * @global int $paged 2537 * @global WP_Query $wp_query WordPress Query object. 2538 * 2539 * @param string $label Content for link text. 2540 * @param int $max_page Optional. Max pages. Default 0. 2541 * @return string|void HTML-formatted next posts page link. 2542 */ 2543 function get_next_posts_link( $label = null, $max_page = 0 ) { 2544 global $paged, $wp_query; 2545 2546 if ( ! $max_page ) { 2547 $max_page = $wp_query->max_num_pages; 2548 } 2549 2550 if ( ! $paged ) { 2551 $paged = 1; 2552 } 2553 2554 $next_page = (int) $paged + 1; 2555 2556 if ( null === $label ) { 2557 $label = __( 'Next Page »' ); 2558 } 2559 2560 if ( ! is_single() && ( $next_page <= $max_page ) ) { 2561 /** 2562 * Filters the anchor tag attributes for the next posts page link. 2563 * 2564 * @since 2.7.0 2565 * 2566 * @param string $attributes Attributes for the anchor tag. 2567 */ 2568 $attr = apply_filters( 'next_posts_link_attributes', '' ); 2569 2570 return sprintf( 2571 '<a href="%1$s" %2$s>%3$s</a>', 2572 next_posts( $max_page, false ), 2573 $attr, 2574 preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) 2575 ); 2576 } 2577 } 2578 2579 /** 2580 * Displays the next posts page link. 2581 * 2582 * @since 0.71 2583 * 2584 * @param string $label Content for link text. 2585 * @param int $max_page Optional. Max pages. Default 0. 2586 */ 2587 function next_posts_link( $label = null, $max_page = 0 ) { 2588 echo get_next_posts_link( $label, $max_page ); 2589 } 2590 2591 /** 2592 * Retrieves the previous posts page link. 2593 * 2594 * Will only return string, if not on a single page or post. 2595 * 2596 * Backported to 2.0.10 from 2.1.3. 2597 * 2598 * @since 2.0.10 2599 * 2600 * @global int $paged 2601 * 2602 * @return string|void The link for the previous posts page. 2603 */ 2604 function get_previous_posts_page_link() { 2605 global $paged; 2606 2607 if ( ! is_single() ) { 2608 $previous_page = (int) $paged - 1; 2609 2610 if ( $previous_page < 1 ) { 2611 $previous_page = 1; 2612 } 2613 2614 return get_pagenum_link( $previous_page ); 2615 } 2616 } 2617 2618 /** 2619 * Displays or retrieves the previous posts page link. 2620 * 2621 * @since 0.71 2622 * 2623 * @param bool $display Optional. Whether to echo the link. Default true. 2624 * @return string|void The previous posts page link if `$display = false`. 2625 */ 2626 function previous_posts( $display = true ) { 2627 $output = esc_url( get_previous_posts_page_link() ); 2628 2629 if ( $display ) { 2630 echo $output; 2631 } else { 2632 return $output; 2633 } 2634 } 2635 2636 /** 2637 * Retrieves the previous posts page link. 2638 * 2639 * @since 2.7.0 2640 * 2641 * @global int $paged 2642 * 2643 * @param string $label Optional. Previous page link text. 2644 * @return string|void HTML-formatted previous page link. 2645 */ 2646 function get_previous_posts_link( $label = null ) { 2647 global $paged; 2648 2649 if ( null === $label ) { 2650 $label = __( '« Previous Page' ); 2651 } 2652 2653 if ( ! is_single() && $paged > 1 ) { 2654 /** 2655 * Filters the anchor tag attributes for the previous posts page link. 2656 * 2657 * @since 2.7.0 2658 * 2659 * @param string $attributes Attributes for the anchor tag. 2660 */ 2661 $attr = apply_filters( 'previous_posts_link_attributes', '' ); 2662 2663 return sprintf( 2664 '<a href="%1$s" %2$s>%3$s</a>', 2665 previous_posts( false ), 2666 $attr, 2667 preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) 2668 ); 2669 } 2670 } 2671 2672 /** 2673 * Displays the previous posts page link. 2674 * 2675 * @since 0.71 2676 * 2677 * @param string $label Optional. Previous page link text. 2678 */ 2679 function previous_posts_link( $label = null ) { 2680 echo get_previous_posts_link( $label ); 2681 } 2682 2683 /** 2684 * Retrieves the post pages link navigation for previous and next pages. 2685 * 2686 * @since 2.8.0 2687 * 2688 * @global WP_Query $wp_query WordPress Query object. 2689 * 2690 * @param string|array $args { 2691 * Optional. Arguments to build the post pages link navigation. 2692 * 2693 * @type string $sep Separator character. Default '—'. 2694 * @type string $prelabel Link text to display for the previous page link. 2695 * Default '« Previous Page'. 2696 * @type string $nxtlabel Link text to display for the next page link. 2697 * Default 'Next Page »'. 2698 * } 2699 * @return string The posts link navigation. 2700 */ 2701 function get_posts_nav_link( $args = array() ) { 2702 global $wp_query; 2703 2704 $return = ''; 2705 2706 if ( ! is_singular() ) { 2707 $defaults = array( 2708 'sep' => ' — ', 2709 'prelabel' => __( '« Previous Page' ), 2710 'nxtlabel' => __( 'Next Page »' ), 2711 ); 2712 $args = wp_parse_args( $args, $defaults ); 2713 2714 $max_num_pages = $wp_query->max_num_pages; 2715 $paged = get_query_var( 'paged' ); 2716 2717 // Only have sep if there's both prev and next results. 2718 if ( $paged < 2 || $paged >= $max_num_pages ) { 2719 $args['sep'] = ''; 2720 } 2721 2722 if ( $max_num_pages > 1 ) { 2723 $return = get_previous_posts_link( $args['prelabel'] ); 2724 $return .= preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $args['sep'] ); 2725 $return .= get_next_posts_link( $args['nxtlabel'] ); 2726 } 2727 } 2728 return $return; 2729 } 2730 2731 /** 2732 * Displays the post pages link navigation for previous and next pages. 2733 * 2734 * @since 0.71 2735 * 2736 * @param string $sep Optional. Separator for posts navigation links. Default empty. 2737 * @param string $prelabel Optional. Label for previous pages. Default empty. 2738 * @param string $nxtlabel Optional Label for next pages. Default empty. 2739 */ 2740 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) { 2741 $args = array_filter( compact( 'sep', 'prelabel', 'nxtlabel' ) ); 2742 echo get_posts_nav_link( $args ); 2743 } 2744 2745 /** 2746 * Retrieves the navigation to next/previous post, when applicable. 2747 * 2748 * @since 4.1.0 2749 * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments. 2750 * @since 5.3.0 Added the `aria_label` parameter. 2751 * @since 5.5.0 Added the `class` parameter. 2752 * 2753 * @param array $args { 2754 * Optional. Default post navigation arguments. Default empty array. 2755 * 2756 * @type string $prev_text Anchor text to display in the previous post link. 2757 * Default '%title'. 2758 * @type string $next_text Anchor text to display in the next post link. 2759 * Default '%title'. 2760 * @type bool $in_same_term Whether link should be in the same taxonomy term. 2761 * Default false. 2762 * @type int[]|string $excluded_terms Array or comma-separated list of excluded term IDs. 2763 * Default empty. 2764 * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'. 2765 * @type string $screen_reader_text Screen reader text for the nav element. 2766 * Default 'Post navigation'. 2767 * @type string $aria_label ARIA label text for the nav element. Default 'Posts'. 2768 * @type string $class Custom class for the nav element. Default 'post-navigation'. 2769 * } 2770 * @return string Markup for post links. 2771 */ 2772 function get_the_post_navigation( $args = array() ) { 2773 // Make sure the nav element has an aria-label attribute: fallback to the screen reader text. 2774 if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) { 2775 $args['aria_label'] = $args['screen_reader_text']; 2776 } 2777 2778 $args = wp_parse_args( 2779 $args, 2780 array( 2781 'prev_text' => '%title', 2782 'next_text' => '%title', 2783 'in_same_term' => false, 2784 'excluded_terms' => '', 2785 'taxonomy' => 'category', 2786 'screen_reader_text' => __( 'Post navigation' ), 2787 'aria_label' => __( 'Posts' ), 2788 'class' => 'post-navigation', 2789 ) 2790 ); 2791 2792 $navigation = ''; 2793 2794 $previous = get_previous_post_link( 2795 '<div class="nav-previous">%link</div>', 2796 $args['prev_text'], 2797 $args['in_same_term'], 2798 $args['excluded_terms'], 2799 $args['taxonomy'] 2800 ); 2801 2802 $next = get_next_post_link( 2803 '<div class="nav-next">%link</div>', 2804 $args['next_text'], 2805 $args['in_same_term'], 2806 $args['excluded_terms'], 2807 $args['taxonomy'] 2808 ); 2809 2810 // Only add markup if there's somewhere to navigate to. 2811 if ( $previous || $next ) { 2812 $navigation = _navigation_markup( $previous . $next, $args['class'], $args['screen_reader_text'], $args['aria_label'] ); 2813 } 2814 2815 return $navigation; 2816 } 2817 2818 /** 2819 * Displays the navigation to next/previous post, when applicable. 2820 * 2821 * @since 4.1.0 2822 * 2823 * @param array $args Optional. See get_the_post_navigation() for available arguments. 2824 * Default empty array. 2825 */ 2826 function the_post_navigation( $args = array() ) { 2827 echo get_the_post_navigation( $args ); 2828 } 2829 2830 /** 2831 * Returns the navigation to next/previous set of posts, when applicable. 2832 * 2833 * @since 4.1.0 2834 * @since 5.3.0 Added the `aria_label` parameter. 2835 * @since 5.5.0 Added the `class` parameter. 2836 * 2837 * @global WP_Query $wp_query WordPress Query object. 2838 * 2839 * @param array $args { 2840 * Optional. Default posts navigation arguments. Default empty array. 2841 * 2842 * @type string $prev_text Anchor text to display in the previous posts link. 2843 * Default 'Older posts'. 2844 * @type string $next_text Anchor text to display in the next posts link. 2845 * Default 'Newer posts'. 2846 * @type string $screen_reader_text Screen reader text for the nav element. 2847 * Default 'Posts navigation'. 2848 * @type string $aria_label ARIA label text for the nav element. Default 'Posts'. 2849 * @type string $class Custom class for the nav element. Default 'posts-navigation'. 2850 * } 2851 * @return string Markup for posts links. 2852 */ 2853 function get_the_posts_navigation( $args = array() ) { 2854 global $wp_query; 2855 2856 $navigation = ''; 2857 2858 // Don't print empty markup if there's only one page. 2859 if ( $wp_query->max_num_pages > 1 ) { 2860 // Make sure the nav element has an aria-label attribute: fallback to the screen reader text. 2861 if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) { 2862 $args['aria_label'] = $args['screen_reader_text']; 2863 } 2864 2865 $args = wp_parse_args( 2866 $args, 2867 array( 2868 'prev_text' => __( 'Older posts' ), 2869 'next_text' => __( 'Newer posts' ), 2870 'screen_reader_text' => __( 'Posts navigation' ), 2871 'aria_label' => __( 'Posts' ), 2872 'class' => 'posts-navigation', 2873 ) 2874 ); 2875 2876 $next_link = get_previous_posts_link( $args['next_text'] ); 2877 $prev_link = get_next_posts_link( $args['prev_text'] ); 2878 2879 if ( $prev_link ) { 2880 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>'; 2881 } 2882 2883 if ( $next_link ) { 2884 $navigation .= '<div class="nav-next">' . $next_link . '</div>'; 2885 } 2886 2887 $navigation = _navigation_markup( $navigation, $args['class'], $args['screen_reader_text'], $args['aria_label'] ); 2888 } 2889 2890 return $navigation; 2891 } 2892 2893 /** 2894 * Displays the navigation to next/previous set of posts, when applicable. 2895 * 2896 * @since 4.1.0 2897 * 2898 * @param array $args Optional. See get_the_posts_navigation() for available arguments. 2899 * Default empty array. 2900 */ 2901 function the_posts_navigation( $args = array() ) { 2902 echo get_the_posts_navigation( $args ); 2903 } 2904 2905 /** 2906 * Retrieves a paginated navigation to next/previous set of posts, when applicable. 2907 * 2908 * @since 4.1.0 2909 * @since 5.3.0 Added the `aria_label` parameter. 2910 * @since 5.5.0 Added the `class` parameter. 2911 * 2912 * @global WP_Query $wp_query WordPress Query object. 2913 * 2914 * @param array $args { 2915 * Optional. Default pagination arguments, see paginate_links(). 2916 * 2917 * @type string $screen_reader_text Screen reader text for navigation element. 2918 * Default 'Posts navigation'. 2919 * @type string $aria_label ARIA label text for the nav element. Default 'Posts'. 2920 * @type string $class Custom class for the nav element. Default 'pagination'. 2921 * } 2922 * @return string Markup for pagination links. 2923 */ 2924 function get_the_posts_pagination( $args = array() ) { 2925 global $wp_query; 2926 2927 $navigation = ''; 2928 2929 // Don't print empty markup if there's only one page. 2930 if ( $wp_query->max_num_pages > 1 ) { 2931 // Make sure the nav element has an aria-label attribute: fallback to the screen reader text. 2932 if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) { 2933 $args['aria_label'] = $args['screen_reader_text']; 2934 } 2935 2936 $args = wp_parse_args( 2937 $args, 2938 array( 2939 'mid_size' => 1, 2940 'prev_text' => _x( 'Previous', 'previous set of posts' ), 2941 'next_text' => _x( 'Next', 'next set of posts' ), 2942 'screen_reader_text' => __( 'Posts navigation' ), 2943 'aria_label' => __( 'Posts' ), 2944 'class' => 'pagination', 2945 ) 2946 ); 2947 2948 /** 2949 * Filters the arguments for posts pagination links. 2950 * 2951 * @since 6.1.0 2952 * 2953 * @param array $args { 2954 * Optional. Default pagination arguments, see paginate_links(). 2955 * 2956 * @type string $screen_reader_text Screen reader text for navigation element. 2957 * Default 'Posts navigation'. 2958 * @type string $aria_label ARIA label text for the nav element. Default 'Posts'. 2959 * @type string $class Custom class for the nav element. Default 'pagination'. 2960 * } 2961 */ 2962 $args = apply_filters( 'the_posts_pagination_args', $args ); 2963 2964 // Make sure we get a string back. Plain is the next best thing. 2965 if ( isset( $args['type'] ) && 'array' === $args['type'] ) { 2966 $args['type'] = 'plain'; 2967 } 2968 2969 // Set up paginated links. 2970 $links = paginate_links( $args ); 2971 2972 if ( $links ) { 2973 $navigation = _navigation_markup( $links, $args['class'], $args['screen_reader_text'], $args['aria_label'] ); 2974 } 2975 } 2976 2977 return $navigation; 2978 } 2979 2980 /** 2981 * Displays a paginated navigation to next/previous set of posts, when applicable. 2982 * 2983 * @since 4.1.0 2984 * 2985 * @param array $args Optional. See get_the_posts_pagination() for available arguments. 2986 * Default empty array. 2987 */ 2988 function the_posts_pagination( $args = array() ) { 2989 echo get_the_posts_pagination( $args ); 2990 } 2991 2992 /** 2993 * Wraps passed links in navigational markup. 2994 * 2995 * @since 4.1.0 2996 * @since 5.3.0 Added the `aria_label` parameter. 2997 * @access private 2998 * 2999 * @param string $links Navigational links. 3000 * @param string $css_class Optional. Custom class for the nav element. 3001 * Default 'posts-navigation'. 3002 * @param string $screen_reader_text Optional. Screen reader text for the nav element. 3003 * Default 'Posts navigation'. 3004 * @param string $aria_label Optional. ARIA label for the nav element. 3005 * Defaults to the value of `$screen_reader_text`. 3006 * @return string Navigation template tag. 3007 */ 3008 function _navigation_markup( $links, $css_class = 'posts-navigation', $screen_reader_text = '', $aria_label = '' ) { 3009 if ( empty( $screen_reader_text ) ) { 3010 $screen_reader_text = /* translators: Hidden accessibility text. */ __( 'Posts navigation' ); 3011 } 3012 if ( empty( $aria_label ) ) { 3013 $aria_label = $screen_reader_text; 3014 } 3015 3016 $template = ' 3017 <nav class="navigation %1$s" aria-label="%4$s"> 3018 <h2 class="screen-reader-text">%2$s</h2> 3019 <div class="nav-links">%3$s</div> 3020 </nav>'; 3021 3022 /** 3023 * Filters the navigation markup template. 3024 * 3025 * Note: The filtered template HTML must contain specifiers for the navigation 3026 * class (%1$s), the screen-reader-text value (%2$s), placement of the navigation 3027 * links (%3$s), and ARIA label text if screen-reader-text does not fit that (%4$s): 3028 * 3029 * <nav class="navigation %1$s" aria-label="%4$s"> 3030 * <h2 class="screen-reader-text">%2$s</h2> 3031 * <div class="nav-links">%3$s</div> 3032 * </nav> 3033 * 3034 * @since 4.4.0 3035 * 3036 * @param string $template The default template. 3037 * @param string $css_class The class passed by the calling function. 3038 * @return string Navigation template. 3039 */ 3040 $template = apply_filters( 'navigation_markup_template', $template, $css_class ); 3041 3042 return sprintf( $template, sanitize_html_class( $css_class ), esc_html( $screen_reader_text ), $links, esc_attr( $aria_label ) ); 3043 } 3044 3045 /** 3046 * Retrieves the comments page number link. 3047 * 3048 * @since 2.7.0 3049 * 3050 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 3051 * 3052 * @param int $pagenum Optional. Page number. Default 1. 3053 * @param int $max_page Optional. The maximum number of comment pages. Default 0. 3054 * @return string The comments page number link URL. 3055 */ 3056 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) { 3057 global $wp_rewrite; 3058 3059 $pagenum = (int) $pagenum; 3060 $max_page = (int) $max_page; 3061 3062 $result = get_permalink(); 3063 3064 if ( 'newest' === get_option( 'default_comments_page' ) ) { 3065 if ( $pagenum !== $max_page ) { 3066 if ( $wp_rewrite->using_permalinks() ) { 3067 $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' ); 3068 } else { 3069 $result = add_query_arg( 'cpage', $pagenum, $result ); 3070 } 3071 } 3072 } elseif ( $pagenum > 1 ) { 3073 if ( $wp_rewrite->using_permalinks() ) { 3074 $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' ); 3075 } else { 3076 $result = add_query_arg( 'cpage', $pagenum, $result ); 3077 } 3078 } 3079 3080 $result .= '#comments'; 3081 3082 /** 3083 * Filters the comments page number link for the current request. 3084 * 3085 * @since 2.7.0 3086 * 3087 * @param string $result The comments page number link. 3088 */ 3089 return apply_filters( 'get_comments_pagenum_link', $result ); 3090 } 3091 3092 /** 3093 * Retrieves the link to the next comments page. 3094 * 3095 * @since 2.7.1 3096 * 3097 * @global WP_Query $wp_query WordPress Query object. 3098 * 3099 * @param string $label Optional. Label for link text. Default empty. 3100 * @param int $max_page Optional. Max page. Default 0. 3101 * @return string|void HTML-formatted link for the next page of comments. 3102 */ 3103 function get_next_comments_link( $label = '', $max_page = 0 ) { 3104 global $wp_query; 3105 3106 if ( ! is_singular() ) { 3107 return; 3108 } 3109 3110 $page = get_query_var( 'cpage' ); 3111 3112 if ( ! $page ) { 3113 $page = 1; 3114 } 3115 3116 $next_page = (int) $page + 1; 3117 3118 if ( empty( $max_page ) ) { 3119 $max_page = $wp_query->max_num_comment_pages; 3120 } 3121 3122 if ( empty( $max_page ) ) { 3123 $max_page = get_comment_pages_count(); 3124 } 3125 3126 if ( $next_page > $max_page ) { 3127 return; 3128 } 3129 3130 if ( empty( $label ) ) { 3131 $label = __( 'Newer Comments »' ); 3132 } 3133 3134 /** 3135 * Filters the anchor tag attributes for the next comments page link. 3136 * 3137 * @since 2.7.0 3138 * 3139 * @param string $attributes Attributes for the anchor tag. 3140 */ 3141 $attr = apply_filters( 'next_comments_link_attributes', '' ); 3142 3143 return sprintf( 3144 '<a href="%1$s" %2$s>%3$s</a>', 3145 esc_url( get_comments_pagenum_link( $next_page, $max_page ) ), 3146 $attr, 3147 preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) 3148 ); 3149 } 3150 3151 /** 3152 * Displays the link to the next comments page. 3153 * 3154 * @since 2.7.0 3155 * 3156 * @param string $label Optional. Label for link text. Default empty. 3157 * @param int $max_page Optional. Max page. Default 0. 3158 */ 3159 function next_comments_link( $label = '', $max_page = 0 ) { 3160 echo get_next_comments_link( $label, $max_page ); 3161 } 3162 3163 /** 3164 * Retrieves the link to the previous comments page. 3165 * 3166 * @since 2.7.1 3167 * 3168 * @param string $label Optional. Label for comments link text. Default empty. 3169 * @return string|void HTML-formatted link for the previous page of comments. 3170 */ 3171 function get_previous_comments_link( $label = '' ) { 3172 if ( ! is_singular() ) { 3173 return; 3174 } 3175 3176 $page = get_query_var( 'cpage' ); 3177 3178 if ( (int) $page <= 1 ) { 3179 return; 3180 } 3181 3182 $previous_page = (int) $page - 1; 3183 3184 if ( empty( $label ) ) { 3185 $label = __( '« Older Comments' ); 3186 } 3187 3188 /** 3189 * Filters the anchor tag attributes for the previous comments page link. 3190 * 3191 * @since 2.7.0 3192 * 3193 * @param string $attributes Attributes for the anchor tag. 3194 */ 3195 $attr = apply_filters( 'previous_comments_link_attributes', '' ); 3196 3197 return sprintf( 3198 '<a href="%1$s" %2$s>%3$s</a>', 3199 esc_url( get_comments_pagenum_link( $previous_page ) ), 3200 $attr, 3201 preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) 3202 ); 3203 } 3204 3205 /** 3206 * Displays the link to the previous comments page. 3207 * 3208 * @since 2.7.0 3209 * 3210 * @param string $label Optional. Label for comments link text. Default empty. 3211 */ 3212 function previous_comments_link( $label = '' ) { 3213 echo get_previous_comments_link( $label ); 3214 } 3215 3216 /** 3217 * Displays or retrieves pagination links for the comments on the current post. 3218 * 3219 * @see paginate_links() 3220 * @since 2.7.0 3221 * 3222 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 3223 * 3224 * @param string|array $args Optional args. See paginate_links(). Default empty array. 3225 * @return void|string|array Void if 'echo' argument is true and 'type' is not an array, 3226 * or if the query is not for an existing single post of any post type. 3227 * Otherwise, markup for comment page links or array of comment page links, 3228 * depending on 'type' argument. 3229 */ 3230 function paginate_comments_links( $args = array() ) { 3231 global $wp_rewrite; 3232 3233 if ( ! is_singular() ) { 3234 return; 3235 } 3236 3237 $page = get_query_var( 'cpage' ); 3238 if ( ! $page ) { 3239 $page = 1; 3240 } 3241 $max_page = get_comment_pages_count(); 3242 $defaults = array( 3243 'base' => add_query_arg( 'cpage', '%#%' ), 3244 'format' => '', 3245 'total' => $max_page, 3246 'current' => $page, 3247 'echo' => true, 3248 'type' => 'plain', 3249 'add_fragment' => '#comments', 3250 ); 3251 if ( $wp_rewrite->using_permalinks() ) { 3252 $defaults['base'] = user_trailingslashit( trailingslashit( get_permalink() ) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged' ); 3253 } 3254 3255 $args = wp_parse_args( $args, $defaults ); 3256 $page_links = paginate_links( $args ); 3257 3258 if ( $args['echo'] && 'array' !== $args['type'] ) { 3259 echo $page_links; 3260 } else { 3261 return $page_links; 3262 } 3263 } 3264 3265 /** 3266 * Retrieves navigation to next/previous set of comments, when applicable. 3267 * 3268 * @since 4.4.0 3269 * @since 5.3.0 Added the `aria_label` parameter. 3270 * @since 5.5.0 Added the `class` parameter. 3271 * 3272 * @param array $args { 3273 * Optional. Default comments navigation arguments. 3274 * 3275 * @type string $prev_text Anchor text to display in the previous comments link. 3276 * Default 'Older comments'. 3277 * @type string $next_text Anchor text to display in the next comments link. 3278 * Default 'Newer comments'. 3279 * @type string $screen_reader_text Screen reader text for the nav element. Default 'Comments navigation'. 3280 * @type string $aria_label ARIA label text for the nav element. Default 'Comments'. 3281 * @type string $class Custom class for the nav element. Default 'comment-navigation'. 3282 * } 3283 * @return string Markup for comments links. 3284 */ 3285 function get_the_comments_navigation( $args = array() ) { 3286 $navigation = ''; 3287 3288 // Are there comments to navigate through? 3289 if ( get_comment_pages_count() > 1 ) { 3290 // Make sure the nav element has an aria-label attribute: fallback to the screen reader text. 3291 if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) { 3292 $args['aria_label'] = $args['screen_reader_text']; 3293 } 3294 3295 $args = wp_parse_args( 3296 $args, 3297 array( 3298 'prev_text' => __( 'Older comments' ), 3299 'next_text' => __( 'Newer comments' ), 3300 'screen_reader_text' => __( 'Comments navigation' ), 3301 'aria_label' => __( 'Comments' ), 3302 'class' => 'comment-navigation', 3303 ) 3304 ); 3305 3306 $prev_link = get_previous_comments_link( $args['prev_text'] ); 3307 $next_link = get_next_comments_link( $args['next_text'] ); 3308 3309 if ( $prev_link ) { 3310 $navigation .= '<div class="nav-previous">' . $prev_link . '</div>'; 3311 } 3312 3313 if ( $next_link ) { 3314 $navigation .= '<div class="nav-next">' . $next_link . '</div>'; 3315 } 3316 3317 $navigation = _navigation_markup( $navigation, $args['class'], $args['screen_reader_text'], $args['aria_label'] ); 3318 } 3319 3320 return $navigation; 3321 } 3322 3323 /** 3324 * Displays navigation to next/previous set of comments, when applicable. 3325 * 3326 * @since 4.4.0 3327 * 3328 * @param array $args See get_the_comments_navigation() for available arguments. Default empty array. 3329 */ 3330 function the_comments_navigation( $args = array() ) { 3331 echo get_the_comments_navigation( $args ); 3332 } 3333 3334 /** 3335 * Retrieves a paginated navigation to next/previous set of comments, when applicable. 3336 * 3337 * @since 4.4.0 3338 * @since 5.3.0 Added the `aria_label` parameter. 3339 * @since 5.5.0 Added the `class` parameter. 3340 * 3341 * @see paginate_comments_links() 3342 * 3343 * @param array $args { 3344 * Optional. Default pagination arguments. 3345 * 3346 * @type string $screen_reader_text Screen reader text for the nav element. Default 'Comments navigation'. 3347 * @type string $aria_label ARIA label text for the nav element. Default 'Comments'. 3348 * @type string $class Custom class for the nav element. Default 'comments-pagination'. 3349 * } 3350 * @return string Markup for pagination links. 3351 */ 3352 function get_the_comments_pagination( $args = array() ) { 3353 $navigation = ''; 3354 3355 // Make sure the nav element has an aria-label attribute: fallback to the screen reader text. 3356 if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) { 3357 $args['aria_label'] = $args['screen_reader_text']; 3358 } 3359 3360 $args = wp_parse_args( 3361 $args, 3362 array( 3363 'screen_reader_text' => __( 'Comments navigation' ), 3364 'aria_label' => __( 'Comments' ), 3365 'class' => 'comments-pagination', 3366 ) 3367 ); 3368 $args['echo'] = false; 3369 3370 // Make sure we get a string back. Plain is the next best thing. 3371 if ( isset( $args['type'] ) && 'array' === $args['type'] ) { 3372 $args['type'] = 'plain'; 3373 } 3374 3375 $links = paginate_comments_links( $args ); 3376 3377 if ( $links ) { 3378 $navigation = _navigation_markup( $links, $args['class'], $args['screen_reader_text'], $args['aria_label'] ); 3379 } 3380 3381 return $navigation; 3382 } 3383 3384 /** 3385 * Displays a paginated navigation to next/previous set of comments, when applicable. 3386 * 3387 * @since 4.4.0 3388 * 3389 * @param array $args See get_the_comments_pagination() for available arguments. Default empty array. 3390 */ 3391 function the_comments_pagination( $args = array() ) { 3392 echo get_the_comments_pagination( $args ); 3393 } 3394 3395 /** 3396 * Retrieves the URL for the current site where the front end is accessible. 3397 * 3398 * Returns the 'home' option with the appropriate protocol. The protocol will be 'https' 3399 * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option. 3400 * If `$scheme` is 'http' or 'https', is_ssl() is overridden. 3401 * 3402 * @since 3.0.0 3403 * 3404 * @param string $path Optional. Path relative to the home URL. Default empty. 3405 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts 3406 * 'http', 'https', 'relative', 'rest', or null. Default null. 3407 * @return string Home URL link with optional path appended. 3408 */ 3409 function home_url( $path = '', $scheme = null ) { 3410 return get_home_url( null, $path, $scheme ); 3411 } 3412 3413 /** 3414 * Retrieves the URL for a given site where the front end is accessible. 3415 * 3416 * Returns the 'home' option with the appropriate protocol. The protocol will be 'https' 3417 * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option. 3418 * If `$scheme` is 'http' or 'https', is_ssl() is overridden. 3419 * 3420 * @since 3.0.0 3421 * 3422 * @param int|null $blog_id Optional. Site ID. Default null (current site). 3423 * @param string $path Optional. Path relative to the home URL. Default empty. 3424 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts 3425 * 'http', 'https', 'relative', 'rest', or null. Default null. 3426 * @return string Home URL link with optional path appended. 3427 */ 3428 function get_home_url( $blog_id = null, $path = '', $scheme = null ) { 3429 $orig_scheme = $scheme; 3430 3431 if ( empty( $blog_id ) || ! is_multisite() ) { 3432 $url = get_option( 'home' ); 3433 } else { 3434 switch_to_blog( $blog_id ); 3435 $url = get_option( 'home' ); 3436 restore_current_blog(); 3437 } 3438 3439 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) { 3440 if ( is_ssl() ) { 3441 $scheme = 'https'; 3442 } else { 3443 $scheme = parse_url( $url, PHP_URL_SCHEME ); 3444 } 3445 } 3446 3447 $url = set_url_scheme( $url, $scheme ); 3448 3449 if ( $path && is_string( $path ) ) { 3450 $url .= '/' . ltrim( $path, '/' ); 3451 } 3452 3453 /** 3454 * Filters the home URL. 3455 * 3456 * @since 3.0.0 3457 * 3458 * @param string $url The complete home URL including scheme and path. 3459 * @param string $path Path relative to the home URL. Blank string if no path is specified. 3460 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 3461 * 'relative', 'rest', or null. 3462 * @param int|null $blog_id Site ID, or null for the current site. 3463 */ 3464 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id ); 3465 } 3466 3467 /** 3468 * Retrieves the URL for the current site where WordPress application files 3469 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible. 3470 * 3471 * Returns the 'site_url' option with the appropriate protocol, 'https' if 3472 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is 3473 * overridden. 3474 * 3475 * @since 3.0.0 3476 * 3477 * @param string $path Optional. Path relative to the site URL. Default empty. 3478 * @param string|null $scheme Optional. Scheme to give the site URL context. See set_url_scheme(). 3479 * @return string Site URL link with optional path appended. 3480 */ 3481 function site_url( $path = '', $scheme = null ) { 3482 return get_site_url( null, $path, $scheme ); 3483 } 3484 3485 /** 3486 * Retrieves the URL for a given site where WordPress application files 3487 * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible. 3488 * 3489 * Returns the 'site_url' option with the appropriate protocol, 'https' if 3490 * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https', 3491 * `is_ssl()` is overridden. 3492 * 3493 * @since 3.0.0 3494 * 3495 * @param int|null $blog_id Optional. Site ID. Default null (current site). 3496 * @param string $path Optional. Path relative to the site URL. Default empty. 3497 * @param string|null $scheme Optional. Scheme to give the site URL context. Accepts 3498 * 'http', 'https', 'login', 'login_post', 'admin', or 3499 * 'relative'. Default null. 3500 * @return string Site URL link with optional path appended. 3501 */ 3502 function get_site_url( $blog_id = null, $path = '', $scheme = null ) { 3503 if ( empty( $blog_id ) || ! is_multisite() ) { 3504 $url = get_option( 'siteurl' ); 3505 } else { 3506 switch_to_blog( $blog_id ); 3507 $url = get_option( 'siteurl' ); 3508 restore_current_blog(); 3509 } 3510 3511 $url = set_url_scheme( $url, $scheme ); 3512 3513 if ( $path && is_string( $path ) ) { 3514 $url .= '/' . ltrim( $path, '/' ); 3515 } 3516 3517 /** 3518 * Filters the site URL. 3519 * 3520 * @since 2.7.0 3521 * 3522 * @param string $url The complete site URL including scheme and path. 3523 * @param string $path Path relative to the site URL. Blank string if no path is specified. 3524 * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login', 3525 * 'login_post', 'admin', 'relative' or null. 3526 * @param int|null $blog_id Site ID, or null for the current site. 3527 */ 3528 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id ); 3529 } 3530 3531 /** 3532 * Retrieves the URL to the admin area for the current site. 3533 * 3534 * @since 2.6.0 3535 * 3536 * @param string $path Optional. Path relative to the admin URL. Default empty. 3537 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 3538 * 'http' or 'https' can be passed to force those schemes. 3539 * @return string Admin URL link with optional path appended. 3540 */ 3541 function admin_url( $path = '', $scheme = 'admin' ) { 3542 return get_admin_url( null, $path, $scheme ); 3543 } 3544 3545 /** 3546 * Retrieves the URL to the admin area for a given site. 3547 * 3548 * @since 3.0.0 3549 * 3550 * @param int|null $blog_id Optional. Site ID. Default null (current site). 3551 * @param string $path Optional. Path relative to the admin URL. Default empty. 3552 * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https', 3553 * to force those schemes. Default 'admin', which obeys 3554 * force_ssl_admin() and is_ssl(). 3555 * @return string Admin URL link with optional path appended. 3556 */ 3557 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) { 3558 $url = get_site_url( $blog_id, 'wp-admin/', $scheme ); 3559 3560 if ( $path && is_string( $path ) ) { 3561 $url .= ltrim( $path, '/' ); 3562 } 3563 3564 /** 3565 * Filters the admin area URL. 3566 * 3567 * @since 2.8.0 3568 * @since 5.8.0 The `$scheme` parameter was added. 3569 * 3570 * @param string $url The complete admin area URL including scheme and path. 3571 * @param string $path Path relative to the admin area URL. Blank string if no path is specified. 3572 * @param int|null $blog_id Site ID, or null for the current site. 3573 * @param string|null $scheme The scheme to use. Accepts 'http', 'https', 3574 * 'admin', or null. Default 'admin', which obeys force_ssl_admin() and is_ssl(). 3575 */ 3576 return apply_filters( 'admin_url', $url, $path, $blog_id, $scheme ); 3577 } 3578 3579 /** 3580 * Retrieves the URL to the includes directory. 3581 * 3582 * @since 2.6.0 3583 * 3584 * @param string $path Optional. Path relative to the includes URL. Default empty. 3585 * @param string|null $scheme Optional. Scheme to give the includes URL context. Accepts 3586 * 'http', 'https', or 'relative'. Default null. 3587 * @return string Includes URL link with optional path appended. 3588 */ 3589 function includes_url( $path = '', $scheme = null ) { 3590 $url = site_url( '/' . WPINC . '/', $scheme ); 3591 3592 if ( $path && is_string( $path ) ) { 3593 $url .= ltrim( $path, '/' ); 3594 } 3595 3596 /** 3597 * Filters the URL to the includes directory. 3598 * 3599 * @since 2.8.0 3600 * @since 5.8.0 The `$scheme` parameter was added. 3601 * 3602 * @param string $url The complete URL to the includes directory including scheme and path. 3603 * @param string $path Path relative to the URL to the wp-includes directory. Blank string 3604 * if no path is specified. 3605 * @param string|null $scheme Scheme to give the includes URL context. Accepts 3606 * 'http', 'https', 'relative', or null. Default null. 3607 */ 3608 return apply_filters( 'includes_url', $url, $path, $scheme ); 3609 } 3610 3611 /** 3612 * Retrieves the URL to the content directory. 3613 * 3614 * @since 2.6.0 3615 * 3616 * @param string $path Optional. Path relative to the content URL. Default empty. 3617 * @return string Content URL link with optional path appended. 3618 */ 3619 function content_url( $path = '' ) { 3620 $url = set_url_scheme( WP_CONTENT_URL ); 3621 3622 if ( $path && is_string( $path ) ) { 3623 $url .= '/' . ltrim( $path, '/' ); 3624 } 3625 3626 /** 3627 * Filters the URL to the content directory. 3628 * 3629 * @since 2.8.0 3630 * 3631 * @param string $url The complete URL to the content directory including scheme and path. 3632 * @param string $path Path relative to the URL to the content directory. Blank string 3633 * if no path is specified. 3634 */ 3635 return apply_filters( 'content_url', $url, $path ); 3636 } 3637 3638 /** 3639 * Retrieves a URL within the plugins or mu-plugins directory. 3640 * 3641 * Defaults to the plugins directory URL if no arguments are supplied. 3642 * 3643 * @since 2.6.0 3644 * 3645 * @param string $path Optional. Extra path appended to the end of the URL, including 3646 * the relative directory if $plugin is supplied. Default empty. 3647 * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin. 3648 * The URL will be relative to its directory. Default empty. 3649 * Typically this is done by passing `__FILE__` as the argument. 3650 * @return string Plugins URL link with optional paths appended. 3651 */ 3652 function plugins_url( $path = '', $plugin = '' ) { 3653 3654 $path = wp_normalize_path( $path ); 3655 $plugin = wp_normalize_path( $plugin ); 3656 $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); 3657 3658 if ( ! empty( $plugin ) && str_starts_with( $plugin, $mu_plugin_dir ) ) { 3659 $url = WPMU_PLUGIN_URL; 3660 } else { 3661 $url = WP_PLUGIN_URL; 3662 } 3663 3664 $url = set_url_scheme( $url ); 3665 3666 if ( ! empty( $plugin ) && is_string( $plugin ) ) { 3667 $folder = dirname( plugin_basename( $plugin ) ); 3668 if ( '.' !== $folder ) { 3669 $url .= '/' . ltrim( $folder, '/' ); 3670 } 3671 } 3672 3673 if ( $path && is_string( $path ) ) { 3674 $url .= '/' . ltrim( $path, '/' ); 3675 } 3676 3677 /** 3678 * Filters the URL to the plugins directory. 3679 * 3680 * @since 2.8.0 3681 * 3682 * @param string $url The complete URL to the plugins directory including scheme and path. 3683 * @param string $path Path relative to the URL to the plugins directory. Blank string 3684 * if no path is specified. 3685 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin 3686 * is specified. 3687 */ 3688 return apply_filters( 'plugins_url', $url, $path, $plugin ); 3689 } 3690 3691 /** 3692 * Retrieves the site URL for the current network. 3693 * 3694 * Returns the site URL with the appropriate protocol, 'https' if 3695 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is 3696 * overridden. 3697 * 3698 * @since 3.0.0 3699 * 3700 * @see set_url_scheme() 3701 * 3702 * @param string $path Optional. Path relative to the site URL. Default empty. 3703 * @param string|null $scheme Optional. Scheme to give the site URL context. Accepts 3704 * 'http', 'https', or 'relative'. Default null. 3705 * @return string Site URL link with optional path appended. 3706 */ 3707 function network_site_url( $path = '', $scheme = null ) { 3708 if ( ! is_multisite() ) { 3709 return site_url( $path, $scheme ); 3710 } 3711 3712 $current_network = get_network(); 3713 3714 if ( 'relative' === $scheme ) { 3715 $url = $current_network->path; 3716 } else { 3717 $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme ); 3718 } 3719 3720 if ( $path && is_string( $path ) ) { 3721 $url .= ltrim( $path, '/' ); 3722 } 3723 3724 /** 3725 * Filters the network site URL. 3726 * 3727 * @since 3.0.0 3728 * 3729 * @param string $url The complete network site URL including scheme and path. 3730 * @param string $path Path relative to the network site URL. Blank string if 3731 * no path is specified. 3732 * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https', 3733 * 'relative' or null. 3734 */ 3735 return apply_filters( 'network_site_url', $url, $path, $scheme ); 3736 } 3737 3738 /** 3739 * Retrieves the home URL for the current network. 3740 * 3741 * Returns the home URL with the appropriate protocol, 'https' is_ssl() 3742 * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is 3743 * overridden. 3744 * 3745 * @since 3.0.0 3746 * 3747 * @param string $path Optional. Path relative to the home URL. Default empty. 3748 * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts 3749 * 'http', 'https', or 'relative'. Default null. 3750 * @return string Home URL link with optional path appended. 3751 */ 3752 function network_home_url( $path = '', $scheme = null ) { 3753 if ( ! is_multisite() ) { 3754 return home_url( $path, $scheme ); 3755 } 3756 3757 $current_network = get_network(); 3758 $orig_scheme = $scheme; 3759 3760 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) { 3761 $scheme = is_ssl() ? 'https' : 'http'; 3762 } 3763 3764 if ( 'relative' === $scheme ) { 3765 $url = $current_network->path; 3766 } else { 3767 $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme ); 3768 } 3769 3770 if ( $path && is_string( $path ) ) { 3771 $url .= ltrim( $path, '/' ); 3772 } 3773 3774 /** 3775 * Filters the network home URL. 3776 * 3777 * @since 3.0.0 3778 * 3779 * @param string $url The complete network home URL including scheme and path. 3780 * @param string $path Path relative to the network home URL. Blank string 3781 * if no path is specified. 3782 * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https', 3783 * 'relative' or null. 3784 */ 3785 return apply_filters( 'network_home_url', $url, $path, $orig_scheme ); 3786 } 3787 3788 /** 3789 * Retrieves the URL to the admin area for the network. 3790 * 3791 * @since 3.0.0 3792 * 3793 * @param string $path Optional path relative to the admin URL. Default empty. 3794 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 3795 * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 3796 * @return string Admin URL link with optional path appended. 3797 */ 3798 function network_admin_url( $path = '', $scheme = 'admin' ) { 3799 if ( ! is_multisite() ) { 3800 return admin_url( $path, $scheme ); 3801 } 3802 3803 $url = network_site_url( 'wp-admin/network/', $scheme ); 3804 3805 if ( $path && is_string( $path ) ) { 3806 $url .= ltrim( $path, '/' ); 3807 } 3808 3809 /** 3810 * Filters the network admin URL. 3811 * 3812 * @since 3.0.0 3813 * @since 5.8.0 The `$scheme` parameter was added. 3814 * 3815 * @param string $url The complete network admin URL including scheme and path. 3816 * @param string $path Path relative to the network admin URL. Blank string if 3817 * no path is specified. 3818 * @param string|null $scheme The scheme to use. Accepts 'http', 'https', 3819 * 'admin', or null. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 3820 */ 3821 return apply_filters( 'network_admin_url', $url, $path, $scheme ); 3822 } 3823 3824 /** 3825 * Retrieves the URL to the admin area for the current user. 3826 * 3827 * @since 3.0.0 3828 * 3829 * @param string $path Optional. Path relative to the admin URL. Default empty. 3830 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 3831 * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 3832 * @return string Admin URL link with optional path appended. 3833 */ 3834 function user_admin_url( $path = '', $scheme = 'admin' ) { 3835 $url = network_site_url( 'wp-admin/user/', $scheme ); 3836 3837 if ( $path && is_string( $path ) ) { 3838 $url .= ltrim( $path, '/' ); 3839 } 3840 3841 /** 3842 * Filters the user admin URL for the current user. 3843 * 3844 * @since 3.1.0 3845 * @since 5.8.0 The `$scheme` parameter was added. 3846 * 3847 * @param string $url The complete URL including scheme and path. 3848 * @param string $path Path relative to the URL. Blank string if 3849 * no path is specified. 3850 * @param string|null $scheme The scheme to use. Accepts 'http', 'https', 3851 * 'admin', or null. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 3852 */ 3853 return apply_filters( 'user_admin_url', $url, $path, $scheme ); 3854 } 3855 3856 /** 3857 * Retrieves the URL to the admin area for either the current site or the network depending on context. 3858 * 3859 * @since 3.1.0 3860 * 3861 * @param string $path Optional. Path relative to the admin URL. Default empty. 3862 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 3863 * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 3864 * @return string Admin URL link with optional path appended. 3865 */ 3866 function self_admin_url( $path = '', $scheme = 'admin' ) { 3867 if ( is_network_admin() ) { 3868 $url = network_admin_url( $path, $scheme ); 3869 } elseif ( is_user_admin() ) { 3870 $url = user_admin_url( $path, $scheme ); 3871 } else { 3872 $url = admin_url( $path, $scheme ); 3873 } 3874 3875 /** 3876 * Filters the admin URL for the current site or network depending on context. 3877 * 3878 * @since 4.9.0 3879 * 3880 * @param string $url The complete URL including scheme and path. 3881 * @param string $path Path relative to the URL. Blank string if no path is specified. 3882 * @param string $scheme The scheme to use. 3883 */ 3884 return apply_filters( 'self_admin_url', $url, $path, $scheme ); 3885 } 3886 3887 /** 3888 * Sets the scheme for a URL. 3889 * 3890 * @since 3.4.0 3891 * @since 4.4.0 The 'rest' scheme was added. 3892 * 3893 * @param string $url Absolute URL that includes a scheme 3894 * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 3895 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null. 3896 * @return string URL with chosen scheme. 3897 */ 3898 function set_url_scheme( $url, $scheme = null ) { 3899 $orig_scheme = $scheme; 3900 3901 if ( ! $scheme ) { 3902 $scheme = is_ssl() ? 'https' : 'http'; 3903 } elseif ( 'admin' === $scheme || 'login' === $scheme || 'login_post' === $scheme || 'rpc' === $scheme ) { 3904 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http'; 3905 } elseif ( 'http' !== $scheme && 'https' !== $scheme && 'relative' !== $scheme ) { 3906 $scheme = is_ssl() ? 'https' : 'http'; 3907 } 3908 3909 $url = trim( $url ); 3910 if ( str_starts_with( $url, '//' ) ) { 3911 $url = 'http:' . $url; 3912 } 3913 3914 if ( 'relative' === $scheme ) { 3915 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) ); 3916 if ( '' !== $url && '/' === $url[0] ) { 3917 $url = '/' . ltrim( $url, "/ \t\n\r\0\x0B" ); 3918 } 3919 } else { 3920 $url = preg_replace( '#^\w+://#', $scheme . '://', $url ); 3921 } 3922 3923 /** 3924 * Filters the resulting URL after setting the scheme. 3925 * 3926 * @since 3.4.0 3927 * 3928 * @param string $url The complete URL including scheme and path. 3929 * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'. 3930 * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login', 3931 * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. 3932 */ 3933 return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme ); 3934 } 3935 3936 /** 3937 * Retrieves the URL to the user's dashboard. 3938 * 3939 * If a user does not belong to any site, the global user dashboard is used. If the user 3940 * belongs to the current site, the dashboard for the current site is returned. If the user 3941 * cannot edit the current site, the dashboard to the user's primary site is returned. 3942 * 3943 * @since 3.1.0 3944 * 3945 * @param int $user_id Optional. User ID. Defaults to current user. 3946 * @param string $path Optional path relative to the dashboard. Use only paths known to 3947 * both site and user admins. Default empty. 3948 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() 3949 * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 3950 * @return string Dashboard URL link with optional path appended. 3951 */ 3952 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { 3953 $user_id = $user_id ? (int) $user_id : get_current_user_id(); 3954 3955 $blogs = get_blogs_of_user( $user_id ); 3956 3957 if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) { 3958 $url = user_admin_url( $path, $scheme ); 3959 } elseif ( ! is_multisite() ) { 3960 $url = admin_url( $path, $scheme ); 3961 } else { 3962 $current_blog = get_current_blog_id(); 3963 3964 if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ), true ) ) ) { 3965 $url = admin_url( $path, $scheme ); 3966 } else { 3967 $active = get_active_blog_for_user( $user_id ); 3968 if ( $active ) { 3969 $url = get_admin_url( $active->blog_id, $path, $scheme ); 3970 } else { 3971 $url = user_admin_url( $path, $scheme ); 3972 } 3973 } 3974 } 3975 3976 /** 3977 * Filters the dashboard URL for a user. 3978 * 3979 * @since 3.1.0 3980 * 3981 * @param string $url The complete URL including scheme and path. 3982 * @param int $user_id The user ID. 3983 * @param string $path Path relative to the URL. Blank string if no path is specified. 3984 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login', 3985 * 'login_post', 'admin', 'relative' or null. 3986 */ 3987 return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme ); 3988 } 3989 3990 /** 3991 * Retrieves the URL to the user's profile editor. 3992 * 3993 * @since 3.1.0 3994 * 3995 * @param int $user_id Optional. User ID. Defaults to current user. 3996 * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 3997 * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 3998 * @return string Dashboard URL link with optional path appended. 3999 */ 4000 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) { 4001 $user_id = $user_id ? (int) $user_id : get_current_user_id(); 4002 4003 if ( is_user_admin() ) { 4004 $url = user_admin_url( 'profile.php', $scheme ); 4005 } elseif ( is_network_admin() ) { 4006 $url = network_admin_url( 'profile.php', $scheme ); 4007 } else { 4008 $url = get_dashboard_url( $user_id, 'profile.php', $scheme ); 4009 } 4010 4011 /** 4012 * Filters the URL for a user's profile editor. 4013 * 4014 * @since 3.1.0 4015 * 4016 * @param string $url The complete URL including scheme and path. 4017 * @param int $user_id The user ID. 4018 * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login', 4019 * 'login_post', 'admin', 'relative' or null. 4020 */ 4021 return apply_filters( 'edit_profile_url', $url, $user_id, $scheme ); 4022 } 4023 4024 /** 4025 * Returns the canonical URL for a post. 4026 * 4027 * When the post is the same as the current requested page the function will handle the 4028 * pagination arguments too. 4029 * 4030 * @since 4.6.0 4031 * 4032 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. 4033 * @return string|false The canonical URL. False if the post does not exist 4034 * or has not been published yet. 4035 */ 4036 function wp_get_canonical_url( $post = null ) { 4037 $post = get_post( $post ); 4038 4039 if ( ! $post ) { 4040 return false; 4041 } 4042 4043 if ( 'publish' !== $post->post_status ) { 4044 return false; 4045 } 4046 4047 $canonical_url = get_permalink( $post ); 4048 4049 // If a canonical is being generated for the current page, make sure it has pagination if needed. 4050 if ( get_queried_object_id() === $post->ID ) { 4051 $page = get_query_var( 'page', 0 ); 4052 if ( $page >= 2 ) { 4053 if ( ! get_option( 'permalink_structure' ) ) { 4054 $canonical_url = add_query_arg( 'page', $page, $canonical_url ); 4055 } else { 4056 $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' ); 4057 } 4058 } 4059 4060 $cpage = get_query_var( 'cpage', 0 ); 4061 if ( $cpage ) { 4062 $canonical_url = get_comments_pagenum_link( $cpage ); 4063 } 4064 } 4065 4066 /** 4067 * Filters the canonical URL for a post. 4068 * 4069 * @since 4.6.0 4070 * 4071 * @param string $canonical_url The post's canonical URL. 4072 * @param WP_Post $post Post object. 4073 */ 4074 return apply_filters( 'get_canonical_url', $canonical_url, $post ); 4075 } 4076 4077 /** 4078 * Outputs rel=canonical for singular queries. 4079 * 4080 * @since 2.9.0 4081 * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`. 4082 */ 4083 function rel_canonical() { 4084 if ( ! is_singular() ) { 4085 return; 4086 } 4087 4088 $id = get_queried_object_id(); 4089 4090 if ( 0 === $id ) { 4091 return; 4092 } 4093 4094 $url = wp_get_canonical_url( $id ); 4095 4096 if ( ! empty( $url ) ) { 4097 echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n"; 4098 } 4099 } 4100 4101 /** 4102 * Returns a shortlink for a post, page, attachment, or site. 4103 * 4104 * This function exists to provide a shortlink tag that all themes and plugins can target. 4105 * A plugin must hook in to provide the actual shortlinks. Default shortlink support is 4106 * limited to providing ?p= style links for posts. Plugins can short-circuit this function 4107 * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'} 4108 * filter. 4109 * 4110 * @since 3.0.0 4111 * 4112 * @param int $id Optional. A post or site ID. Default is 0, which means the current post or site. 4113 * @param string $context Optional. Whether the ID is a 'site' ID, 'post' ID, or 'media' ID. If 'post', 4114 * the post_type of the post is consulted. If 'query', the current query is consulted 4115 * to determine the ID and context. Default 'post'. 4116 * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how 4117 * and whether to honor this. Default true. 4118 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks 4119 * are not enabled. 4120 */ 4121 function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { 4122 /** 4123 * Filters whether to preempt generating a shortlink for the given post. 4124 * 4125 * Returning a value other than false from the filter will short-circuit 4126 * the shortlink generation process, returning that value instead. 4127 * 4128 * @since 3.0.0 4129 * 4130 * @param false|string $return Short-circuit return value. Either false or a URL string. 4131 * @param int $id Post ID, or 0 for the current post. 4132 * @param string $context The context for the link. One of 'post' or 'query', 4133 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. 4134 */ 4135 $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs ); 4136 4137 if ( false !== $shortlink ) { 4138 return $shortlink; 4139 } 4140 4141 $post_id = 0; 4142 if ( 'query' === $context && is_singular() ) { 4143 $post_id = get_queried_object_id(); 4144 $post = get_post( $post_id ); 4145 } elseif ( 'post' === $context ) { 4146 $post = get_post( $id ); 4147 if ( ! empty( $post->ID ) ) { 4148 $post_id = $post->ID; 4149 } 4150 } 4151 4152 $shortlink = ''; 4153 4154 // Return `?p=` link for all public post types. 4155 if ( ! empty( $post_id ) ) { 4156 $post_type = get_post_type_object( $post->post_type ); 4157 4158 if ( 'page' === $post->post_type 4159 && 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID 4160 ) { 4161 $shortlink = home_url( '/' ); 4162 } elseif ( $post_type && $post_type->public ) { 4163 $shortlink = home_url( '?p=' . $post_id ); 4164 } 4165 } 4166 4167 /** 4168 * Filters the shortlink for a post. 4169 * 4170 * @since 3.0.0 4171 * 4172 * @param string $shortlink Shortlink URL. 4173 * @param int $id Post ID, or 0 for the current post. 4174 * @param string $context The context for the link. One of 'post' or 'query', 4175 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default. 4176 */ 4177 return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs ); 4178 } 4179 4180 /** 4181 * Injects rel=shortlink into the head if a shortlink is defined for the current page. 4182 * 4183 * Attached to the {@see 'wp_head'} action. 4184 * 4185 * @since 3.0.0 4186 */ 4187 function wp_shortlink_wp_head() { 4188 $shortlink = wp_get_shortlink( 0, 'query' ); 4189 4190 if ( empty( $shortlink ) ) { 4191 return; 4192 } 4193 4194 echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n"; 4195 } 4196 4197 /** 4198 * Sends a Link: rel=shortlink header if a shortlink is defined for the current page. 4199 * 4200 * Attached to the {@see 'wp'} action. 4201 * 4202 * @since 3.0.0 4203 */ 4204 function wp_shortlink_header() { 4205 if ( headers_sent() ) { 4206 return; 4207 } 4208 4209 $shortlink = wp_get_shortlink( 0, 'query' ); 4210 4211 if ( empty( $shortlink ) ) { 4212 return; 4213 } 4214 4215 header( 'Link: <' . $shortlink . '>; rel=shortlink', false ); 4216 } 4217 4218 /** 4219 * Displays the shortlink for a post. 4220 * 4221 * Must be called from inside "The Loop" 4222 * 4223 * Call like the_shortlink( __( 'Shortlinkage FTW' ) ) 4224 * 4225 * @since 3.0.0 4226 * 4227 * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.' 4228 * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title. 4229 * @param string $before Optional HTML to display before the link. Default empty. 4230 * @param string $after Optional HTML to display after the link. Default empty. 4231 */ 4232 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) { 4233 $post = get_post(); 4234 4235 if ( empty( $text ) ) { 4236 $text = __( 'This is the short link.' ); 4237 } 4238 4239 if ( empty( $title ) ) { 4240 $title = the_title_attribute( array( 'echo' => false ) ); 4241 } 4242 4243 $shortlink = wp_get_shortlink( $post->ID ); 4244 4245 if ( ! empty( $shortlink ) ) { 4246 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>'; 4247 4248 /** 4249 * Filters the short link anchor tag for a post. 4250 * 4251 * @since 3.0.0 4252 * 4253 * @param string $link Shortlink anchor tag. 4254 * @param string $shortlink Shortlink URL. 4255 * @param string $text Shortlink's text. 4256 * @param string $title Shortlink's title attribute. 4257 */ 4258 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title ); 4259 echo $before, $link, $after; 4260 } 4261 } 4262 4263 /** 4264 * Retrieves the avatar URL. 4265 * 4266 * @since 4.2.0 4267 * 4268 * @param mixed $id_or_email The avatar to retrieve a URL for. Accepts a user ID, Gravatar MD5 hash, 4269 * user email, WP_User object, WP_Post object, or WP_Comment object. 4270 * @param array $args { 4271 * Optional. Arguments to use instead of the default arguments. 4272 * 4273 * @type int $size Height and width of the avatar in pixels. Default 96. 4274 * @type string $default URL for the default image or a default type. Accepts: 4275 * - '404' (return a 404 instead of a default image) 4276 * - 'retro' (a 8-bit arcade-style pixelated face) 4277 * - 'robohash' (a robot) 4278 * - 'monsterid' (a monster) 4279 * - 'wavatar' (a cartoon face) 4280 * - 'identicon' (the "quilt", a geometric pattern) 4281 * - 'mystery', 'mm', or 'mysteryman' (The Oyster Man) 4282 * - 'blank' (transparent GIF) 4283 * - 'gravatar_default' (the Gravatar logo) 4284 * Default is the value of the 'avatar_default' option, 4285 * with a fallback of 'mystery'. 4286 * @type bool $force_default Whether to always show the default image, never the Gravatar. 4287 * Default false. 4288 * @type string $rating What rating to display avatars up to. Accepts: 4289 * - 'G' (suitable for all audiences) 4290 * - 'PG' (possibly offensive, usually for audiences 13 and above) 4291 * - 'R' (intended for adult audiences above 17) 4292 * - 'X' (even more mature than above) 4293 * Default is the value of the 'avatar_rating' option. 4294 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 4295 * Default null. 4296 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 4297 * plus a "found_avatar" guess. Pass as a reference. Default null. 4298 * } 4299 * @return string|false The URL of the avatar on success, false on failure. 4300 */ 4301 function get_avatar_url( $id_or_email, $args = null ) { 4302 $args = get_avatar_data( $id_or_email, $args ); 4303 return $args['url']; 4304 } 4305 4306 /** 4307 * Check if this comment type allows avatars to be retrieved. 4308 * 4309 * @since 5.1.0 4310 * 4311 * @param string $comment_type Comment type to check. 4312 * @return bool Whether the comment type is allowed for retrieving avatars. 4313 */ 4314 function is_avatar_comment_type( $comment_type ) { 4315 /** 4316 * Filters the list of allowed comment types for retrieving avatars. 4317 * 4318 * @since 3.0.0 4319 * 4320 * @param array $types An array of content types. Default only contains 'comment'. 4321 */ 4322 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 4323 4324 return in_array( $comment_type, (array) $allowed_comment_types, true ); 4325 } 4326 4327 /** 4328 * Retrieves default data about the avatar. 4329 * 4330 * @since 4.2.0 4331 * 4332 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, 4333 * user email, WP_User object, WP_Post object, or WP_Comment object. 4334 * @param array $args { 4335 * Optional. Arguments to use instead of the default arguments. 4336 * 4337 * @type int $size Height and width of the avatar in pixels. Default 96. 4338 * @type int $height Display height of the avatar in pixels. Defaults to $size. 4339 * @type int $width Display width of the avatar in pixels. Defaults to $size. 4340 * @type string $default URL for the default image or a default type. Accepts: 4341 * - '404' (return a 404 instead of a default image) 4342 * - 'retro' (a 8-bit arcade-style pixelated face) 4343 * - 'robohash' (a robot) 4344 * - 'monsterid' (a monster) 4345 * - 'wavatar' (a cartoon face) 4346 * - 'identicon' (the "quilt", a geometric pattern) 4347 * - 'mystery', 'mm', or 'mysteryman' (The Oyster Man) 4348 * - 'blank' (transparent GIF) 4349 * - 'gravatar_default' (the Gravatar logo) 4350 * Default is the value of the 'avatar_default' option, 4351 * with a fallback of 'mystery'. 4352 * @type bool $force_default Whether to always show the default image, never the Gravatar. 4353 * Default false. 4354 * @type string $rating What rating to display avatars up to. Accepts: 4355 * - 'G' (suitable for all audiences) 4356 * - 'PG' (possibly offensive, usually for audiences 13 and above) 4357 * - 'R' (intended for adult audiences above 17) 4358 * - 'X' (even more mature than above) 4359 * Default is the value of the 'avatar_rating' option. 4360 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 4361 * Default null. 4362 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 4363 * plus a "found_avatar" guess. Pass as a reference. Default null. 4364 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. 4365 * Default empty. 4366 * } 4367 * @return array { 4368 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments. 4369 * 4370 * @type bool $found_avatar True if an avatar was found for this user, 4371 * false or not set if none was found. 4372 * @type string|false $url The URL of the avatar that was found, or false. 4373 * } 4374 */ 4375 function get_avatar_data( $id_or_email, $args = null ) { 4376 $args = wp_parse_args( 4377 $args, 4378 array( 4379 'size' => 96, 4380 'height' => null, 4381 'width' => null, 4382 'default' => get_option( 'avatar_default', 'mystery' ), 4383 'force_default' => false, 4384 'rating' => get_option( 'avatar_rating' ), 4385 'scheme' => null, 4386 'processed_args' => null, // If used, should be a reference. 4387 'extra_attr' => '', 4388 ) 4389 ); 4390 4391 if ( is_numeric( $args['size'] ) ) { 4392 $args['size'] = absint( $args['size'] ); 4393 if ( ! $args['size'] ) { 4394 $args['size'] = 96; 4395 } 4396 } else { 4397 $args['size'] = 96; 4398 } 4399 4400 if ( is_numeric( $args['height'] ) ) { 4401 $args['height'] = absint( $args['height'] ); 4402 if ( ! $args['height'] ) { 4403 $args['height'] = $args['size']; 4404 } 4405 } else { 4406 $args['height'] = $args['size']; 4407 } 4408 4409 if ( is_numeric( $args['width'] ) ) { 4410 $args['width'] = absint( $args['width'] ); 4411 if ( ! $args['width'] ) { 4412 $args['width'] = $args['size']; 4413 } 4414 } else { 4415 $args['width'] = $args['size']; 4416 } 4417 4418 if ( empty( $args['default'] ) ) { 4419 $args['default'] = get_option( 'avatar_default', 'mystery' ); 4420 } 4421 4422 switch ( $args['default'] ) { 4423 case 'mm': 4424 case 'mystery': 4425 case 'mysteryman': 4426 $args['default'] = 'mm'; 4427 break; 4428 case 'gravatar_default': 4429 $args['default'] = false; 4430 break; 4431 } 4432 4433 $args['force_default'] = (bool) $args['force_default']; 4434 4435 $args['rating'] = strtolower( $args['rating'] ); 4436 4437 $args['found_avatar'] = false; 4438 4439 /** 4440 * Filters whether to retrieve the avatar URL early. 4441 * 4442 * Passing a non-null value in the 'url' member of the return array will 4443 * effectively short circuit get_avatar_data(), passing the value through 4444 * the {@see 'get_avatar_data'} filter and returning early. 4445 * 4446 * @since 4.2.0 4447 * 4448 * @param array $args Arguments passed to get_avatar_data(), after processing. 4449 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, 4450 * user email, WP_User object, WP_Post object, or WP_Comment object. 4451 */ 4452 $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email ); 4453 4454 if ( isset( $args['url'] ) ) { 4455 /** This filter is documented in wp-includes/link-template.php */ 4456 return apply_filters( 'get_avatar_data', $args, $id_or_email ); 4457 } 4458 4459 $email_hash = ''; 4460 $user = false; 4461 $email = false; 4462 4463 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 4464 $id_or_email = get_comment( $id_or_email ); 4465 } 4466 4467 // Process the user identifier. 4468 if ( is_numeric( $id_or_email ) ) { 4469 $user = get_user_by( 'id', absint( $id_or_email ) ); 4470 } elseif ( is_string( $id_or_email ) ) { 4471 if ( str_contains( $id_or_email, '@md5.gravatar.com' ) ) { 4472 // MD5 hash. 4473 list( $email_hash ) = explode( '@', $id_or_email ); 4474 } else { 4475 // Email address. 4476 $email = $id_or_email; 4477 } 4478 } elseif ( $id_or_email instanceof WP_User ) { 4479 // User object. 4480 $user = $id_or_email; 4481 } elseif ( $id_or_email instanceof WP_Post ) { 4482 // Post object. 4483 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 4484 } elseif ( $id_or_email instanceof WP_Comment ) { 4485 if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) { 4486 $args['url'] = false; 4487 /** This filter is documented in wp-includes/link-template.php */ 4488 return apply_filters( 'get_avatar_data', $args, $id_or_email ); 4489 } 4490 4491 if ( ! empty( $id_or_email->user_id ) ) { 4492 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 4493 } 4494 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 4495 $email = $id_or_email->comment_author_email; 4496 } 4497 } 4498 4499 if ( ! $email_hash ) { 4500 if ( $user ) { 4501 $email = $user->user_email; 4502 } 4503 4504 if ( $email ) { 4505 $email_hash = md5( strtolower( trim( $email ) ) ); 4506 } 4507 } 4508 4509 if ( $email_hash ) { 4510 $args['found_avatar'] = true; 4511 $gravatar_server = hexdec( $email_hash[0] ) % 3; 4512 } else { 4513 $gravatar_server = rand( 0, 2 ); 4514 } 4515 4516 $url_args = array( 4517 's' => $args['size'], 4518 'd' => $args['default'], 4519 'f' => $args['force_default'] ? 'y' : false, 4520 'r' => $args['rating'], 4521 ); 4522 4523 if ( is_ssl() ) { 4524 $url = 'https://secure.gravatar.com/avatar/' . $email_hash; 4525 } else { 4526 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash ); 4527 } 4528 4529 $url = add_query_arg( 4530 rawurlencode_deep( array_filter( $url_args ) ), 4531 set_url_scheme( $url, $args['scheme'] ) 4532 ); 4533 4534 /** 4535 * Filters the avatar URL. 4536 * 4537 * @since 4.2.0 4538 * 4539 * @param string $url The URL of the avatar. 4540 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, 4541 * user email, WP_User object, WP_Post object, or WP_Comment object. 4542 * @param array $args Arguments passed to get_avatar_data(), after processing. 4543 */ 4544 $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args ); 4545 4546 /** 4547 * Filters the avatar data. 4548 * 4549 * @since 4.2.0 4550 * 4551 * @param array $args Arguments passed to get_avatar_data(), after processing. 4552 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, 4553 * user email, WP_User object, WP_Post object, or WP_Comment object. 4554 */ 4555 return apply_filters( 'get_avatar_data', $args, $id_or_email ); 4556 } 4557 4558 /** 4559 * Retrieves the URL of a file in the theme. 4560 * 4561 * Searches in the stylesheet directory before the template directory so themes 4562 * which inherit from a parent theme can just override one file. 4563 * 4564 * @since 4.7.0 4565 * 4566 * @param string $file Optional. File to search for in the stylesheet directory. 4567 * @return string The URL of the file. 4568 */ 4569 function get_theme_file_uri( $file = '' ) { 4570 $file = ltrim( $file, '/' ); 4571 4572 $stylesheet_directory = get_stylesheet_directory(); 4573 4574 if ( empty( $file ) ) { 4575 $url = get_stylesheet_directory_uri(); 4576 } elseif ( get_template_directory() !== $stylesheet_directory && file_exists( $stylesheet_directory . '/' . $file ) ) { 4577 $url = get_stylesheet_directory_uri() . '/' . $file; 4578 } else { 4579 $url = get_template_directory_uri() . '/' . $file; 4580 } 4581 4582 /** 4583 * Filters the URL to a file in the theme. 4584 * 4585 * @since 4.7.0 4586 * 4587 * @param string $url The file URL. 4588 * @param string $file The requested file to search for. 4589 */ 4590 return apply_filters( 'theme_file_uri', $url, $file ); 4591 } 4592 4593 /** 4594 * Retrieves the URL of a file in the parent theme. 4595 * 4596 * @since 4.7.0 4597 * 4598 * @param string $file Optional. File to return the URL for in the template directory. 4599 * @return string The URL of the file. 4600 */ 4601 function get_parent_theme_file_uri( $file = '' ) { 4602 $file = ltrim( $file, '/' ); 4603 4604 if ( empty( $file ) ) { 4605 $url = get_template_directory_uri(); 4606 } else { 4607 $url = get_template_directory_uri() . '/' . $file; 4608 } 4609 4610 /** 4611 * Filters the URL to a file in the parent theme. 4612 * 4613 * @since 4.7.0 4614 * 4615 * @param string $url The file URL. 4616 * @param string $file The requested file to search for. 4617 */ 4618 return apply_filters( 'parent_theme_file_uri', $url, $file ); 4619 } 4620 4621 /** 4622 * Retrieves the path of a file in the theme. 4623 * 4624 * Searches in the stylesheet directory before the template directory so themes 4625 * which inherit from a parent theme can just override one file. 4626 * 4627 * @since 4.7.0 4628 * 4629 * @param string $file Optional. File to search for in the stylesheet directory. 4630 * @return string The path of the file. 4631 */ 4632 function get_theme_file_path( $file = '' ) { 4633 $file = ltrim( $file, '/' ); 4634 4635 $stylesheet_directory = get_stylesheet_directory(); 4636 $template_directory = get_template_directory(); 4637 4638 if ( empty( $file ) ) { 4639 $path = $stylesheet_directory; 4640 } elseif ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/' . $file ) ) { 4641 $path = $stylesheet_directory . '/' . $file; 4642 } else { 4643 $path = $template_directory . '/' . $file; 4644 } 4645 4646 /** 4647 * Filters the path to a file in the theme. 4648 * 4649 * @since 4.7.0 4650 * 4651 * @param string $path The file path. 4652 * @param string $file The requested file to search for. 4653 */ 4654 return apply_filters( 'theme_file_path', $path, $file ); 4655 } 4656 4657 /** 4658 * Retrieves the path of a file in the parent theme. 4659 * 4660 * @since 4.7.0 4661 * 4662 * @param string $file Optional. File to return the path for in the template directory. 4663 * @return string The path of the file. 4664 */ 4665 function get_parent_theme_file_path( $file = '' ) { 4666 $file = ltrim( $file, '/' ); 4667 4668 if ( empty( $file ) ) { 4669 $path = get_template_directory(); 4670 } else { 4671 $path = get_template_directory() . '/' . $file; 4672 } 4673 4674 /** 4675 * Filters the path to a file in the parent theme. 4676 * 4677 * @since 4.7.0 4678 * 4679 * @param string $path The file path. 4680 * @param string $file The requested file to search for. 4681 */ 4682 return apply_filters( 'parent_theme_file_path', $path, $file ); 4683 } 4684 4685 /** 4686 * Retrieves the URL to the privacy policy page. 4687 * 4688 * @since 4.9.6 4689 * 4690 * @return string The URL to the privacy policy page. Empty string if it doesn't exist. 4691 */ 4692 function get_privacy_policy_url() { 4693 $url = ''; 4694 $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 4695 4696 if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) { 4697 $url = (string) get_permalink( $policy_page_id ); 4698 } 4699 4700 /** 4701 * Filters the URL of the privacy policy page. 4702 * 4703 * @since 4.9.6 4704 * 4705 * @param string $url The URL to the privacy policy page. Empty string 4706 * if it doesn't exist. 4707 * @param int $policy_page_id The ID of privacy policy page. 4708 */ 4709 return apply_filters( 'privacy_policy_url', $url, $policy_page_id ); 4710 } 4711 4712 /** 4713 * Displays the privacy policy link with formatting, when applicable. 4714 * 4715 * @since 4.9.6 4716 * 4717 * @param string $before Optional. Display before privacy policy link. Default empty. 4718 * @param string $after Optional. Display after privacy policy link. Default empty. 4719 */ 4720 function the_privacy_policy_link( $before = '', $after = '' ) { 4721 echo get_the_privacy_policy_link( $before, $after ); 4722 } 4723 4724 /** 4725 * Returns the privacy policy link with formatting, when applicable. 4726 * 4727 * @since 4.9.6 4728 * @since 6.2.0 Added 'privacy-policy' rel attribute. 4729 * 4730 * @param string $before Optional. Display before privacy policy link. Default empty. 4731 * @param string $after Optional. Display after privacy policy link. Default empty. 4732 * @return string Markup for the link and surrounding elements. Empty string if it 4733 * doesn't exist. 4734 */ 4735 function get_the_privacy_policy_link( $before = '', $after = '' ) { 4736 $link = ''; 4737 $privacy_policy_url = get_privacy_policy_url(); 4738 $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 4739 $page_title = ( $policy_page_id ) ? get_the_title( $policy_page_id ) : ''; 4740 4741 if ( $privacy_policy_url && $page_title ) { 4742 $link = sprintf( 4743 '<a class="privacy-policy-link" href="%s" rel="privacy-policy">%s</a>', 4744 esc_url( $privacy_policy_url ), 4745 esc_html( $page_title ) 4746 ); 4747 } 4748 4749 /** 4750 * Filters the privacy policy link. 4751 * 4752 * @since 4.9.6 4753 * 4754 * @param string $link The privacy policy link. Empty string if it 4755 * doesn't exist. 4756 * @param string $privacy_policy_url The URL of the privacy policy. Empty string 4757 * if it doesn't exist. 4758 */ 4759 $link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url ); 4760 4761 if ( $link ) { 4762 return $before . $link . $after; 4763 } 4764 4765 return ''; 4766 } 4767 4768 /** 4769 * Returns an array of URL hosts which are considered to be internal hosts. 4770 * 4771 * By default the list of internal hosts is comprised of the host name of 4772 * the site's home_url() (as parsed by wp_parse_url()). 4773 * 4774 * This list is used when determining if a specified URL is a link to a page on 4775 * the site itself or a link offsite (to an external host). This is used, for 4776 * example, when determining if the "nofollow" attribute should be applied to a 4777 * link. 4778 * 4779 * @see wp_is_internal_link 4780 * 4781 * @since 6.2.0 4782 * 4783 * @return string[] An array of URL hosts. 4784 */ 4785 function wp_internal_hosts() { 4786 static $internal_hosts; 4787 4788 if ( empty( $internal_hosts ) ) { 4789 /** 4790 * Filters the array of URL hosts which are considered internal. 4791 * 4792 * @since 6.2.0 4793 * 4794 * @param string[] $internal_hosts An array of internal URL hostnames. 4795 */ 4796 $internal_hosts = apply_filters( 4797 'wp_internal_hosts', 4798 array( 4799 wp_parse_url( home_url(), PHP_URL_HOST ), 4800 ) 4801 ); 4802 $internal_hosts = array_unique( 4803 array_map( 'strtolower', (array) $internal_hosts ) 4804 ); 4805 } 4806 4807 return $internal_hosts; 4808 } 4809 4810 /** 4811 * Determines whether or not the specified URL is of a host included in the internal hosts list. 4812 * 4813 * @see wp_internal_hosts() 4814 * 4815 * @since 6.2.0 4816 * 4817 * @param string $link The URL to test. 4818 * @return bool Returns true for internal URLs and false for all other URLs. 4819 */ 4820 function wp_is_internal_link( $link ) { 4821 $link = strtolower( $link ); 4822 if ( in_array( wp_parse_url( $link, PHP_URL_SCHEME ), wp_allowed_protocols(), true ) ) { 4823 return in_array( wp_parse_url( $link, PHP_URL_HOST ), wp_internal_hosts(), true ); 4824 } 4825 return false; 4826 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Wed May 8 08:20:02 2024 | Cross-referenced by PHPXref |