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