display_name The author’s display name.
35 */
36 return apply_filters(‚the_author‘, is_object($authordata) ? $authordata->display_name : null);
37 }
38
39 /**
40 * Display the name of the author of the current post.
41 *
42 * The behavior of this function is based off of old functionality predating
43 * get_the_author(). This function is not deprecated, but is designed to echo
44 * the value from get_the_author() and as an result of any old theme that might
45 * still use the old behavior will also pass the value from get_the_author().
46 *
47 * The normal, expected behavior of this function is to echo the author and not
48 * return it. However, backward compatibility has to be maintained.
49 *
50 * @since 0.71
51 * @see get_the_author()
52 * @link https://codex.wordpress.org/Template_Tags/the_author
53 *
54 * @param string $deprecated Deprecated.
55 * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
56 * @return string|null The author’s display name, from get_the_author().
57 */
58 function the_author( $deprecated = “, $deprecated_echo = true ) {
59 if ( ! empty( $deprecated ) ) {
60 _deprecated_argument( __FUNCTION__, ‚2.1.0‘ );
61 }
62
63 if ( true !== $deprecated_echo ) {
64 _deprecated_argument( __FUNCTION__, ‚1.5.0‘,
65 /* translators: %s: get_the_author() */
66 sprintf( __( ‚Use %s instead if you do not want the value echoed.‘ ),
67 ‚get_the_author()
‚
68 )
69 );
70 }
71
72 if ( $deprecated_echo ) {
73 echo get_the_author();
74 }
75
76 return get_the_author();
77 }
78
79 /**
80 * Retrieve the author who last edited the current post.
81 *
82 * @since 2.8.0
83 *
84 * @return string|void The author’s display name.
85 */
86 function get_the_modified_author() {
87 if ( $last_id = get_post_meta( get_post()->ID, ‚_edit_last‘, true) ) {
88 $last_user = get_userdata($last_id);
89
90 /**
91 * Filters the display name of the author who last edited the current post.
92 *
93 * @since 2.8.0
94 *
95 * @param string $last_user->display_name The author’s display name.
96 */
97 return apply_filters(‚the_modified_author‘, $last_user->display_name);
98 }
99 }
100
101 /**
102 * Display the name of the author who last edited the current post,
103 * if the author’s ID is available.
104 *
105 * @since 2.8.0
106 *
107 * @see get_the_author()
108 */
109 function the_modified_author() {
110 echo get_the_modified_author();
111 }
112
113 /**
114 * Retrieve the requested data of the author of the current post.
115 * @link https://codex.wordpress.org/Template_Tags/the_author_meta
116 * @since 2.8.0
117 *
118 * @global object $authordata The current author’s DB object.
119 *
120 * @param string $field selects the field of the users record.
121 * @param int $user_id Optional. User ID.
122 * @return string The author’s field from the current author’s DB object.
123 */
124 function get_the_author_meta( $field = “, $user_id = false ) {
125 $original_user_id = $user_id;
126
127 if ( ! $user_id ) {
128 global $authordata;
129 $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
130 } else {
131 $authordata = get_userdata( $user_id );
132 }
133
134 if ( in_array( $field, array( ‚login‘, ‚pass‘, ‚nicename‘, ‚email‘, ‚url‘, ‚registered‘, ‚activation_key‘, ‚status‘ ) ) )
135 $field = ‚user_‘ . $field;
136
137 $value = isset( $authordata->$field ) ? $authordata->$field : “;
138
139 /**
140 * Filters the value of the requested user metadata.
141 *
142 * The filter name is dynamic and depends on the $field parameter of the function.
143 *
144 * @since 2.8.0
145 * @since 4.3.0 The `$original_user_id` parameter was added.
146 *
147 * @param string $value The value of the metadata.
148 * @param int $user_id The user ID for the value.
149 * @param int|bool $original_user_id The original user ID, as passed to the function.
150 */
151 return apply_filters( „get_the_author_{$field}“, $value, $user_id, $original_user_id );
152 }
153
154 /**
155 * Outputs the field from the user’s DB object. Defaults to current post’s author.
156 *
157 * @link https://codex.wordpress.org/Template_Tags/the_author_meta
158 *
159 * @since 2.8.0
160 *
161 * @param string $field selects the field of the users record.
162 * @param int $user_id Optional. User ID.
163 */
164 function the_author_meta( $field = “, $user_id = false ) {
165 $author_meta = get_the_author_meta( $field, $user_id );
166
167 /**
168 * The value of the requested user metadata.
169 *
170 * The filter name is dynamic and depends on the $field parameter of the function.
171 *
172 * @since 2.8.0
173 *
174 * @param string $author_meta The value of the metadata.
175 * @param int $user_id The user ID.
176 */
177 echo apply_filters( „the_author_{$field}“, $author_meta, $user_id );
178 }
179
180 /**
181 * Retrieve either author’s link or author’s name.
182 *
183 * If the author has a home page set, return an HTML link, otherwise just return the
184 * author’s name.
185 *
186 * @since 3.0.0
187 *
188 * @return string|null An HTML link if the author’s url exist in user meta,
189 * else the result of get_the_author().
190 */
191 function get_the_author_link() {
192 if ( get_the_author_meta(‚url‘) ) {
193 return sprintf( ‚%3$s‚,
194 esc_url( get_the_author_meta(‚url‘) ),
195 /* translators: %s: author’s display name */
196 esc_attr( sprintf( __( ‚Visit %s’s website‘ ), get_the_author() ) ),
197 get_the_author()
198 );
199 } else {
200 return get_the_author();
201 }
202 }
203
204 /**
205 * Display either author’s link or author’s name.
206 *
207 * If the author has a home page set, echo an HTML link, otherwise just echo the
208 * author’s name.
209 *
210 * @link https://codex.wordpress.org/Template_Tags/the_author_link
211 *
212 * @since 2.1.0
213 */
214 function the_author_link() {
215 echo get_the_author_link();
216 }
217
218 /**
219 * Retrieve the number of posts by the author of the current post.
220 *
221 * @since 1.5.0
222 *
223 * @return int The number of posts by the author.
224 */
225 function get_the_author_posts() {
226 $post = get_post();
227 if ( ! $post ) {
228 return 0;
229 }
230 return count_user_posts( $post->post_author, $post->post_type );
231 }
232
233 /**
234 * Display the number of posts by the author of the current post.
235 *
236 * @link https://codex.wordpress.org/Template_Tags/the_author_posts
237 * @since 0.71
238 */
239 function the_author_posts() {
240 echo get_the_author_posts();
241 }
242
243 /**
244 * Retrieves an HTML link to the author page of the current post’s author.
245 *
246 * Returns an HTML-formatted link using get_author_posts_url().
247 *
248 * @since 4.4.0
249 *
250 * @global object $authordata The current author’s DB object.
251 *
252 * @return string An HTML link to the author page.
253 */
254 function get_the_author_posts_link() {
255 global $authordata;
256 if ( ! is_object( $authordata ) ) {
257 return;
258 }
259
260 $link = sprintf( ‚%3$s‚,
261 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
262 /* translators: %s: author’s display name */
263 esc_attr( sprintf( __( ‚Posts by %s‘ ), get_the_author() ) ),
264 get_the_author()
265 );
266
267 /**
268 * Filters the link to the author page of the author of the current post.
269 *
270 * @since 2.9.0
271 *
272 * @param string $link HTML link.
273 */
274 return apply_filters( ‚the_author_posts_link‘, $link );
275 }
276
277 /**
278 * Displays an HTML link to the author page of the current post’s author.
279 *
280 * @since 1.2.0
281 * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link()
282 *
283 * @param string $deprecated Unused.
284 */
285 function the_author_posts_link( $deprecated = “ ) {
286 if ( ! empty( $deprecated ) ) {
287 _deprecated_argument( __FUNCTION__, ‚2.1.0‘ );
288 }
289 echo get_the_author_posts_link();
290 }
291
292 /**
293 * Retrieve the URL to the author page for the user with the ID provided.
294 *
295 * @since 2.1.0
296 *
297 * @global WP_Rewrite $wp_rewrite
298 *
299 * @param int $author_id Author ID.
300 * @param string $author_nicename Optional. The author’s nicename (slug). Default empty.
301 * @return string The URL to the author’s page.
302 */
303 function get_author_posts_url( $author_id, $author_nicename = “ ) {
304 global $wp_rewrite;
305 $auth_ID = (int) $author_id;
306 $link = $wp_rewrite->get_author_permastruct();
307
308 if ( empty($link) ) {
309 $file = home_url( ‚/‘ );
310 $link = $file . ‚?author=‘ . $auth_ID;
311 } else {
312 if ( “ == $author_nicename ) {
313 $user = get_userdata($author_id);
314 if ( !empty($user->user_nicename) )
315 $author_nicename = $user->user_nicename;
316 }
317 $link = str_replace(‚%author%‘, $author_nicename, $link);
318 $link = home_url( user_trailingslashit( $link ) );
319 }
320
321 /**
322 * Filters the URL to the author’s page.
323 *
324 * @since 2.1.0
325 *
326 * @param string $link The URL to the author’s page.
327 * @param int $author_id The author’s id.
328 * @param string $author_nicename The author’s nice name.
329 */
330 $link = apply_filters( ‚author_link‘, $link, $author_id, $author_nicename );
331
332 return $link;
333 }
334
335 /**
336 * List all the authors of the site, with several options available.
337 *
338 * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
339 *
340 * @since 1.2.0
341 *
342 * @global wpdb $wpdb WordPress database abstraction object.
343 *
344 * @param string|array $args {
345 * Optional. Array or string of default arguments.
346 *
347 * @type string $orderby How to sort the authors. Accepts ‚nicename‘, ‚email‘, ‚url‘, ‚registered‘,
348 * ‚user_nicename‘, ‚user_email‘, ‚user_url‘, ‚user_registered‘, ‚name‘,
349 * ‚display_name‘, ‚post_count‘, ‚ID‘, ‚meta_value‘, ‚user_login‘. Default ‚name‘.
350 * @type string $order Sorting direction for $orderby. Accepts ‚ASC‘, ‚DESC‘. Default ‚ASC‘.
351 * @type int $number Maximum authors to return or display. Default empty (all authors).
352 * @type bool $optioncount Show the count in parenthesis next to the author’s name. Default false.
353 * @type bool $exclude_admin Whether to exclude the ‚admin‘ account, if it exists. Default false.
354 * @type bool $show_fullname Whether to show the author’s full name. Default false.
355 * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
356 * @type string $feed If not empty, show a link to the author’s feed and use this text as the alt
357 * parameter of the link. Default empty.
358 * @type string $feed_image If not empty, show a link to the author’s feed and use this image URL as
359 * clickable anchor. Default empty.
360 * @type string $feed_type The feed type to link to, such as ‚rss2‘. Defaults to default feed type.
361 * @type bool $echo Whether to output the result or instead return it. Default true.
362 * @type string $style If ‚list‘, each author is wrapped in an `
363 * will be separated by commas.
364 * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
365 * @type array|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty.
366 * @type array|string $include Array or comma/space-separated list of author IDs to include. Default empty.
367 * }
368 * @return string|void The output, if echo is set to false.
369 */
370 function wp_list_authors( $args = “ ) {
371 global $wpdb;
372
373 $defaults = array(
374 ‚orderby‘ => ‚name‘, ‚order‘ => ‚ASC‘, ‚number‘ => “,
375 ‚optioncount‘ => false, ‚exclude_admin‘ => true,
376 ‚show_fullname‘ => false, ‚hide_empty‘ => true,
377 ‚feed‘ => “, ‚feed_image‘ => “, ‚feed_type‘ => “, ‚echo‘ => true,
378 ‚style‘ => ‚list‘, ‚html‘ => true, ‚exclude‘ => “, ‚include‘ => “
379 );
380
381 $args = wp_parse_args( $args, $defaults );
382
383 $return = “;
384
385 $query_args = wp_array_slice_assoc( $args, array( ‚orderby‘, ‚order‘, ‚number‘, ‚exclude‘, ‚include‘ ) );
386 $query_args[‚fields‘] = ‚ids‘;
387 $authors = get_users( $query_args );
388
389 $author_count = array();
390 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 ) {
391 $author_count[$row->post_author] = $row->count;
392 }
393 foreach ( $authors as $author_id ) {
394 $author = get_userdata( $author_id );
395
396 if ( $args[‚exclude_admin‘] && ‚admin‘ == $author->display_name ) {
397 continue;
398 }
399
400 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
401
402 if ( ! $posts && $args[‚hide_empty‘] ) {
403 continue;
404 }
405
406 if ( $args[‚show_fullname‘] && $author->first_name && $author->last_name ) {
407 $name = „$author->first_name $author->last_name“;
408 } else {
409 $name = $author->display_name;
410 }
411
412 if ( ! $args[‚html‘] ) {
413 $return .= $name . ‚, ‚;
414
415 continue; // No need to go further to process HTML.
416 }
417
418 if ( ‚list‘ == $args[‚style‘] ) {
419 $return .= ‚
420 }
421
422 $link = sprintf( ‚%3$s‚,
423 get_author_posts_url( $author->ID, $author->user_nicename ),
424 /* translators: %s: author’s display name */
425 esc_attr( sprintf( __( ‚Posts by %s‘ ), $author->display_name ) ),
426 $name
427 );
428
429 if ( ! empty( $args[‚feed_image‘] ) || ! empty( $args[‚feed‘] ) ) {
430 $link .= ‚ ‚;
431 if ( empty( $args[‚feed_image‘] ) ) {
432 $link .= ‚(‚;
433 }
434
435 $link .= ‚‚;
447 } else {
448 $link .= $name;
449 }
450
451 $link .= ‚‚;
452
453 if ( empty( $args[‚feed_image‘] ) ) {
454 $link .= ‚)‘;
455 }
456 }
457
458 if ( $args[‚optioncount‘] ) {
459 $link .= ‚ (‚. $posts . ‚)‘;
460 }
461
462 $return .= $link;
463 $return .= ( ‚list‘ == $args[‚style‘] ) ? ‚
‚ : ‚, ‚;
464 }
465
466 $return = rtrim( $return, ‚, ‚ );
467
468 if ( ! $args[‚echo‘] ) {
469 return $return;
470 }
471 echo $return;
472 }
473
474 /**
475 * Does this site have more than one author
476 *
477 * Checks to see if more than one author has published posts.
478 *
479 * @since 3.2.0
480 *
481 * @global wpdb $wpdb WordPress database abstraction object.
482 *
483 * @return bool Whether or not we have more than one author
484 */
485 function is_multi_author() {
486 global $wpdb;
487
488 if ( false === ( $is_multi_author = get_transient( ‚is_multi_author‘ ) ) ) {
489 $rows = (array) $wpdb->get_col(„SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = ‚post‘ AND post_status = ‚publish‘ LIMIT 2“);
490 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
491 set_transient( 'is_multi_author', $is_multi_author );
492 }
493
494 /**
495 * Filters whether the site has more than one author with published posts.
496 *
497 * @since 3.2.0
498 *
499 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
500 */
501 return apply_filters( 'is_multi_author', (bool) $is_multi_author );
502 }
503
504 /**
505 * Helper function to clear the cache for number of authors.
506 *
507 * @since 3.2.0
508 * @access private
509 */
510 function __clear_multi_author_cache() {
511 delete_transient( 'is_multi_author' );
512 }