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