| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Author Template functions for use in themes. 4 * 5 * These functions must be used within the WordPress Loop. 6 * 7 * @link https://codex.wordpress.org/Author_Templates 8 * 9 * @package WordPress 10 * @subpackage Template 11 */ 12 13 /** 14 * Retrieves the author of the current post. 15 * 16 * @since 1.5.0 17 * @since 6.3.0 Returns an empty string if the author's display name is unknown. 18 * 19 * @global WP_User|false|null $authordata The current author's data. 20 * 21 * @param string $deprecated Deprecated. 22 * @return string The author's display name, empty string if unknown. 23 * 24 * @phpstan-param '' $deprecated 25 */ 26 function get_the_author( $deprecated = '' ) { 27 global $authordata; 28 29 if ( ! empty( $deprecated ) ) { 30 _deprecated_argument( __FUNCTION__, '2.1.0' ); 31 } 32 33 /** 34 * Filters the display name of the current post's author. 35 * 36 * @since 2.9.0 37 * 38 * @param string $display_name The author's display name. 39 */ 40 return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : '' ); 41 } 42 43 /** 44 * Displays the name of the author of the current post. 45 * 46 * The behavior of this function is based off of old functionality predating 47 * get_the_author(). This function is not deprecated, but is designed to echo 48 * the value from get_the_author() and as an result of any old theme that might 49 * still use the old behavior will also pass the value from get_the_author(). 50 * 51 * The normal, expected behavior of this function is to echo the author and not 52 * return it. However, backward compatibility has to be maintained. 53 * 54 * @since 0.71 55 * 56 * @see get_the_author() 57 * @link https://developer.wordpress.org/reference/functions/the_author/ 58 * 59 * @param string $deprecated Deprecated. 60 * @param bool $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it. 61 * @return string The author's display name, from get_the_author(). 62 * 63 * @phpstan-param '' $deprecated 64 * @phpstan-param true $deprecated_echo 65 */ 66 function the_author( $deprecated = '', $deprecated_echo = true ) { 67 if ( ! empty( $deprecated ) ) { 68 _deprecated_argument( __FUNCTION__, '2.1.0' ); 69 } 70 71 if ( true !== $deprecated_echo ) { 72 _deprecated_argument( 73 __FUNCTION__, 74 '1.5.0', 75 sprintf( 76 /* translators: %s: get_the_author() */ 77 __( 'Use %s instead if you do not want the value echoed.' ), 78 '<code>get_the_author()</code>' 79 ) 80 ); 81 } 82 83 if ( $deprecated_echo ) { 84 echo get_the_author(); 85 } 86 87 return get_the_author(); 88 } 89 90 /** 91 * Retrieves the author who last edited the current post. 92 * 93 * @since 2.8.0 94 * @since 6.9.0 Added the `$post` parameter. Unknown return value is now explicitly null instead of void. 95 * 96 * @param int|WP_Post|null $post Optional. Post ID or post object. Default is global `$post` object. 97 * @return string|null The author's display name. Empty string if user is unavailable. Null if there was no last editor or the post is invalid. 98 */ 99 function get_the_modified_author( $post = null ) { 100 $post = get_post( $post ); 101 if ( ! $post ) { 102 return null; 103 } 104 105 $last_id = get_post_meta( $post->ID, '_edit_last', true ); 106 if ( ! $last_id ) { 107 return null; 108 } 109 $last_user = get_userdata( $last_id ); 110 111 /** 112 * Filters the display name of the author who last edited the current post. 113 * 114 * @since 2.8.0 115 * 116 * @param string $display_name The author's display name, empty string if user is unavailable. 117 */ 118 return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' ); 119 } 120 121 /** 122 * Displays the name of the author who last edited the current post, 123 * if the author's ID is available. 124 * 125 * @since 2.8.0 126 * 127 * @see get_the_author() 128 */ 129 function the_modified_author() { 130 echo get_the_modified_author(); 131 } 132 133 /** 134 * Retrieves the requested data of the author of the current post. 135 * 136 * Valid values for the `$field` parameter include: 137 * 138 * - admin_color 139 * - comment_shortcuts 140 * - description 141 * - display_name 142 * - first_name 143 * - ID 144 * - last_name 145 * - nickname 146 * - plugins_last_view 147 * - plugins_per_page 148 * - rich_editing 149 * - syntax_highlighting 150 * - user_activation_key 151 * - user_description 152 * - user_email 153 * - user_firstname 154 * - user_lastname 155 * - user_level 156 * - user_login 157 * - user_nicename 158 * - user_pass 159 * - user_registered 160 * - user_status 161 * - user_url 162 * 163 * @since 2.8.0 164 * @since 6.9.0 Removed `aim`, `jabber`, and `yim` as valid values for the `$field` parameter. 165 * 166 * @global WP_User|false|null $authordata The current author's data. 167 * 168 * @param string $field Optional. The user field to retrieve. Default empty. 169 * @param int|false $user_id Optional. User ID. Defaults to the current post author. 170 * @return string The author's field from the current author's DB object, otherwise an empty string. 171 */ 172 function get_the_author_meta( $field = '', $user_id = false ) { 173 $original_user_id = $user_id; 174 175 if ( ! $user_id ) { 176 global $authordata; 177 $user_id = $authordata->ID ?? 0; 178 } else { 179 $authordata = get_userdata( $user_id ); 180 } 181 182 if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) { 183 $field = 'user_' . $field; 184 } 185 186 $value = $authordata->$field ?? ''; 187 188 /** 189 * Filters the value of the requested user metadata. 190 * 191 * The filter name is dynamic and depends on the $field parameter of the function. 192 * 193 * @since 2.8.0 194 * @since 4.3.0 The `$original_user_id` parameter was added. 195 * 196 * @param string $value The value of the metadata. 197 * @param int $user_id The user ID for the value. 198 * @param int|false $original_user_id The original user ID, as passed to the function. 199 */ 200 return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id ); 201 } 202 203 /** 204 * Outputs the field from the user's DB object. Defaults to current post's author. 205 * 206 * @since 2.8.0 207 * 208 * @param string $field Selects the field of the users record. See get_the_author_meta() 209 * for the list of possible fields. 210 * @param int|false $user_id Optional. User ID. Defaults to the current post author. 211 * 212 * @see get_the_author_meta() 213 */ 214 function the_author_meta( $field = '', $user_id = false ) { 215 $author_meta = get_the_author_meta( $field, $user_id ); 216 217 /** 218 * Filters the value of the requested user metadata. 219 * 220 * The filter name is dynamic and depends on the $field parameter of the function. 221 * 222 * @since 2.8.0 223 * 224 * @param string $author_meta The value of the metadata. 225 * @param int|false $user_id The user ID. 226 */ 227 echo apply_filters( "the_author_{$field}", $author_meta, $user_id ); 228 } 229 230 /** 231 * Retrieves either author's link or author's name. 232 * 233 * If the author has a home page set, return an HTML link, otherwise just return 234 * the author's name. 235 * 236 * @since 3.0.0 237 * @since 7.0.0 Added `$use_title_attr` parameter. 238 * 239 * @global WP_User|false|null $authordata The current author's data. 240 * 241 * @param bool $use_title_attr Optional. Whether to add a title attribute. 242 * Default true. 243 * @return string An HTML link if the author's URL exists in user meta, 244 * otherwise the result of get_the_author(). 245 */ 246 function get_the_author_link( $use_title_attr = true ) { 247 if ( get_the_author_meta( 'url' ) ) { 248 global $authordata; 249 250 $author_url = get_the_author_meta( 'url' ); 251 $author_display_name = get_the_author(); 252 253 /* translators: %s: Author's display name. */ 254 $author_title = sprintf( __( 'Visit %s’s website' ), $author_display_name ); 255 256 $link = sprintf( 257 '<a href="%1$s"%2$s rel="author external">%3$s</a>', 258 esc_url( $author_url ), 259 $use_title_attr ? ' title="' . esc_attr( $author_title ) . '"' : '', 260 $author_display_name 261 ); 262 263 /** 264 * Filters the author URL link HTML. 265 * 266 * @since 6.0.0 267 * 268 * @param string $link The default rendered author HTML link. 269 * @param string $author_url Author's URL. 270 * @param WP_User $authordata Author user data. 271 */ 272 return apply_filters( 'the_author_link', $link, $author_url, $authordata ); 273 } else { 274 return get_the_author(); 275 } 276 } 277 278 /** 279 * Displays either author's link or author's name. 280 * 281 * If the author has a home page set, echo an HTML link, otherwise just echo the 282 * author's name. 283 * 284 * @link https://developer.wordpress.org/reference/functions/the_author_link/ 285 * 286 * @since 2.1.0 287 * @since 7.0.0 Added `$use_title_attr` parameter. 288 * 289 * @param bool $use_title_attr Optional. Whether to add a title attribute. 290 * Default true. 291 */ 292 function the_author_link( $use_title_attr = true ) { 293 echo get_the_author_link( $use_title_attr ); 294 } 295 296 /** 297 * Retrieves the number of posts by the author of the current post. 298 * 299 * @since 1.5.0 300 * 301 * @return int The number of posts by the author. 302 */ 303 function get_the_author_posts() { 304 $post = get_post(); 305 if ( ! $post ) { 306 return 0; 307 } 308 return (int) count_user_posts( $post->post_author, $post->post_type ); 309 } 310 311 /** 312 * Displays the number of posts by the author of the current post. 313 * 314 * @link https://developer.wordpress.org/reference/functions/the_author_posts/ 315 * @since 0.71 316 */ 317 function the_author_posts() { 318 echo get_the_author_posts(); 319 } 320 321 /** 322 * Retrieves an HTML link to the author page of the current post's author. 323 * 324 * Returns an HTML-formatted link using get_author_posts_url(). 325 * 326 * @since 4.4.0 327 * @since 7.0.0 Removed title attribute. 328 * 329 * @global WP_User|false|null $authordata The current author's data. 330 * 331 * @return string An HTML link to the author page, or an empty string if $authordata is not set. 332 */ 333 function get_the_author_posts_link() { 334 global $authordata; 335 336 if ( ! is_object( $authordata ) ) { 337 return ''; 338 } 339 340 $author = get_the_author(); 341 /* translators: %s: Author's display name. */ 342 $title = sprintf( __( 'Posts by %s' ), $author ); 343 344 $link = sprintf( 345 '<a href="%1$s" rel="author">%2$s</a>', 346 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ), 347 $author 348 ); 349 350 /** 351 * Filters the link to the author page of the author of the current post. 352 * 353 * @since 2.9.0 354 * @since 7.0.0 Added `$author` and `$title` parameters. 355 * 356 * @param string $link HTML link. 357 * @param string $author Author's display name. 358 * @param string $title Text originally used for a title attribute. 359 */ 360 return apply_filters( 'the_author_posts_link', $link, $author, $title ); 361 } 362 363 /** 364 * Displays an HTML link to the author page of the current post's author. 365 * 366 * @since 1.2.0 367 * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link() 368 * 369 * @param string $deprecated Unused. 370 * 371 * @phpstan-param '' $deprecated 372 */ 373 function the_author_posts_link( $deprecated = '' ) { 374 if ( ! empty( $deprecated ) ) { 375 _deprecated_argument( __FUNCTION__, '2.1.0' ); 376 } 377 echo get_the_author_posts_link(); 378 } 379 380 /** 381 * Retrieves the URL to the author page for the user with the ID provided. 382 * 383 * @since 2.1.0 384 * 385 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 386 * 387 * @param int $author_id Author ID. 388 * @param string $author_nicename Optional. The author's nicename (slug). Default empty. 389 * @return string The URL to the author's page. 390 */ 391 function get_author_posts_url( $author_id, $author_nicename = '' ) { 392 global $wp_rewrite; 393 394 $author_id = (int) $author_id; 395 $link = $wp_rewrite->get_author_permastruct(); 396 397 if ( empty( $link ) ) { 398 $file = home_url( '/' ); 399 $link = $file . '?author=' . $author_id; 400 } else { 401 if ( '' === $author_nicename ) { 402 $user = get_userdata( $author_id ); 403 if ( ! empty( $user->user_nicename ) ) { 404 $author_nicename = $user->user_nicename; 405 } 406 } 407 $link = str_replace( '%author%', $author_nicename, $link ); 408 $link = home_url( user_trailingslashit( $link ) ); 409 } 410 411 /** 412 * Filters the URL to the author's page. 413 * 414 * @since 2.1.0 415 * 416 * @param string $link The URL to the author's page. 417 * @param int $author_id The author's ID. 418 * @param string $author_nicename The author's nice name. 419 */ 420 $link = apply_filters( 'author_link', $link, $author_id, $author_nicename ); 421 422 return $link; 423 } 424 425 /** 426 * Lists all the authors of the site, with several options available. 427 * 428 * @link https://developer.wordpress.org/reference/functions/wp_list_authors/ 429 * 430 * @since 1.2.0 431 * 432 * @global wpdb $wpdb WordPress database abstraction object. 433 * 434 * @param string|array $args { 435 * Optional. Array or string of default arguments. 436 * 437 * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered', 438 * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name', 439 * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'. 440 * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'. 441 * @type int $number Maximum authors to return or display. Default empty (all authors). 442 * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false. 443 * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default true. 444 * @type bool $show_fullname Whether to show the author's full name. Default false. 445 * @type bool $hide_empty Whether to hide any authors with no posts. Default true. 446 * @type string $feed If not empty, show a link to the author's feed and use this text as the alt 447 * parameter of the link. Default empty. 448 * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as 449 * clickable anchor. Default empty. 450 * @type string $feed_type The feed type to link to. Possible values include 'rss2', 'atom'. 451 * Default is the value of get_default_feed(). 452 * @type bool $echo Whether to output the result or instead return it. Default true. 453 * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors 454 * will be separated by commas. 455 * @type bool $html Whether to list the items in HTML form or plaintext. Default true. 456 * @type int[]|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty. 457 * @type int[]|string $include Array or comma/space-separated list of author IDs to include. Default empty. 458 * } 459 * @return string|void List of authors if 'echo' is false, nothing otherwise. 460 * @phpstan-return ( 461 * $args is array{ echo: false|0|''|'0', ... } 462 * ? string 463 * : ( $args is ''|'0'|array ? void : string|null ) 464 * ) 465 */ 466 function wp_list_authors( $args = '' ) { 467 global $wpdb; 468 469 $defaults = array( 470 'orderby' => 'name', 471 'order' => 'ASC', 472 'number' => '', 473 'optioncount' => false, 474 'exclude_admin' => true, 475 'show_fullname' => false, 476 'hide_empty' => true, 477 'feed' => '', 478 'feed_image' => '', 479 'feed_type' => '', 480 'echo' => true, 481 'style' => 'list', 482 'html' => true, 483 'exclude' => '', 484 'include' => '', 485 ); 486 487 $parsed_args = wp_parse_args( $args, $defaults ); 488 489 $return = ''; 490 491 $query_args = wp_array_slice_assoc( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) ); 492 $query_args['fields'] = 'ids'; 493 494 /** 495 * Filters the query arguments for the list of all authors of the site. 496 * 497 * @since 6.1.0 498 * 499 * @param array $query_args The query arguments for get_users(). 500 * @param array $parsed_args The arguments passed to wp_list_authors() combined with the defaults. 501 */ 502 $query_args = apply_filters( 'wp_list_authors_args', $query_args, $parsed_args ); 503 504 $authors = get_users( $query_args ); 505 506 /** 507 * Filters whether to short-circuit performing the query for author post counts. 508 * 509 * @since 6.1.0 510 * 511 * @param int[]|false $post_counts Array of post counts, keyed by author ID. 512 * @param array $parsed_args The arguments passed to wp_list_authors() combined with the defaults. 513 */ 514 $post_counts = apply_filters( 'pre_wp_list_authors_post_counts_query', false, $parsed_args ); 515 516 if ( ! is_array( $post_counts ) ) { 517 $post_counts = array(); 518 $post_counts_query = $wpdb->get_results( 519 "SELECT DISTINCT post_author, COUNT(ID) AS count 520 FROM $wpdb->posts 521 WHERE " . get_private_posts_cap_sql( 'post' ) . ' 522 GROUP BY post_author' 523 ); 524 525 foreach ( (array) $post_counts_query as $row ) { 526 $post_counts[ $row->post_author ] = $row->count; 527 } 528 } 529 530 foreach ( $authors as $author_id ) { 531 $posts = $post_counts[ $author_id ] ?? 0; 532 533 if ( ! $posts && $parsed_args['hide_empty'] ) { 534 continue; 535 } 536 537 $author = get_userdata( $author_id ); 538 539 if ( $parsed_args['exclude_admin'] && 'admin' === $author->display_name ) { 540 continue; 541 } 542 543 if ( $parsed_args['show_fullname'] && $author->first_name && $author->last_name ) { 544 $name = sprintf( 545 /* translators: 1: User's first name, 2: Last name. */ 546 _x( '%1$s %2$s', 'Display name based on first name and last name' ), 547 $author->first_name, 548 $author->last_name 549 ); 550 } else { 551 $name = $author->display_name; 552 } 553 554 if ( ! $parsed_args['html'] ) { 555 $return .= $name . ', '; 556 557 continue; // No need to go further to process HTML. 558 } 559 560 if ( 'list' === $parsed_args['style'] ) { 561 $return .= '<li>'; 562 } 563 564 $link = sprintf( 565 '<a href="%1$s">%2$s</a>', 566 esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ), 567 $name 568 ); 569 570 if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) { 571 $link .= ' '; 572 if ( empty( $parsed_args['feed_image'] ) ) { 573 $link .= '('; 574 } 575 576 $link .= '<a href="' . get_author_feed_link( $author->ID, $parsed_args['feed_type'] ) . '"'; 577 578 $alt = ''; 579 if ( ! empty( $parsed_args['feed'] ) ) { 580 $alt = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"'; 581 $name = $parsed_args['feed']; 582 } 583 584 $link .= '>'; 585 586 if ( ! empty( $parsed_args['feed_image'] ) ) { 587 $link .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />'; 588 } else { 589 $link .= $name; 590 } 591 592 $link .= '</a>'; 593 594 if ( empty( $parsed_args['feed_image'] ) ) { 595 $link .= ')'; 596 } 597 } 598 599 if ( $parsed_args['optioncount'] ) { 600 $link .= ' (' . $posts . ')'; 601 } 602 603 $return .= $link; 604 $return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', '; 605 } 606 607 $return = rtrim( $return, ', ' ); 608 609 if ( $parsed_args['echo'] ) { 610 echo $return; 611 } else { 612 return $return; 613 } 614 } 615 616 /** 617 * Determines whether this site has more than one author. 618 * 619 * Checks to see if more than one author has published posts. 620 * 621 * For more information on this and similar theme functions, check out 622 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 623 * Conditional Tags} article in the Theme Developer Handbook. 624 * 625 * @since 3.2.0 626 * 627 * @global wpdb $wpdb WordPress database abstraction object. 628 * 629 * @return bool Whether or not we have more than one author 630 */ 631 function is_multi_author() { 632 global $wpdb; 633 634 $is_multi_author = get_transient( 'is_multi_author' ); 635 if ( false === $is_multi_author ) { 636 $rows = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" ); 637 $is_multi_author = 1 < count( $rows ) ? 1 : 0; 638 set_transient( 'is_multi_author', $is_multi_author ); 639 } 640 641 /** 642 * Filters whether the site has more than one author with published posts. 643 * 644 * @since 3.2.0 645 * 646 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true. 647 */ 648 return apply_filters( 'is_multi_author', (bool) $is_multi_author ); 649 } 650 651 /** 652 * Helper function to clear the cache for number of authors. 653 * 654 * @since 3.2.0 655 * @access private 656 */ 657 function __clear_multi_author_cache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore 658 delete_transient( 'is_multi_author' ); 659 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Oct 13 08:20:33 2026 | Cross-referenced by PHPXref |