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