3 * Author Template functions for use in themes.
5 * These functions must be used within the WordPress Loop.
7 * @link https://codex.wordpress.org/Author_Templates
10 * @subpackage Template
14 * Retrieve the author of the current post.
18 * @global object $authordata The current author's DB object.
20 * @param string $deprecated Deprecated.
21 * @return string|null The author's display name.
23 function get_the_author($deprecated = '') {
26 if ( !empty( $deprecated ) )
27 _deprecated_argument( __FUNCTION__, '2.1.0' );
30 * Filters the display name of the current post's author.
34 * @param string $authordata->display_name The author's display name.
36 return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
40 * Display the name of the author of the current post.
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().
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.
51 * @see get_the_author()
52 * @link https://codex.wordpress.org/Template_Tags/the_author
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().
58 function the_author( $deprecated = '', $deprecated_echo = true ) {
59 if ( ! empty( $deprecated ) ) {
60 _deprecated_argument( __FUNCTION__, '2.1.0' );
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 '<code>get_the_author()</code>'
72 if ( $deprecated_echo ) {
73 echo get_the_author();
76 return get_the_author();
80 * Retrieve the author who last edited the current post.
84 * @return string|void The author's display name.
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);
91 * Filters the display name of the author who last edited the current post.
95 * @param string $last_user->display_name The author's display name.
97 return apply_filters('the_modified_author', $last_user->display_name);
102 * Display the name of the author who last edited the current post,
103 * if the author's ID is available.
107 * @see get_the_author()
109 function the_modified_author() {
110 echo get_the_modified_author();
114 * Retrieve the requested data of the author of the current post.
115 * @link https://codex.wordpress.org/Template_Tags/the_author_meta
118 * @global object $authordata The current author's DB object.
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.
124 function get_the_author_meta( $field = '', $user_id = false ) {
125 $original_user_id = $user_id;
129 $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
131 $authordata = get_userdata( $user_id );
134 if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
135 $field = 'user_' . $field;
137 $value = isset( $authordata->$field ) ? $authordata->$field : '';
140 * Filters the value of the requested user metadata.
142 * The filter name is dynamic and depends on the $field parameter of the function.
145 * @since 4.3.0 The `$original_user_id` parameter was added.
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.
151 return apply_filters( 'get_the_author_' . $field, $value, $user_id, $original_user_id );
155 * Outputs the field from the user's DB object. Defaults to current post's author.
157 * @link https://codex.wordpress.org/Template_Tags/the_author_meta
161 * @param string $field selects the field of the users record.
162 * @param int $user_id Optional. User ID.
164 function the_author_meta( $field = '', $user_id = false ) {
165 $author_meta = get_the_author_meta( $field, $user_id );
168 * The value of the requested user metadata.
170 * The filter name is dynamic and depends on the $field parameter of the function.
174 * @param string $author_meta The value of the metadata.
175 * @param int $user_id The user ID.
177 echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
181 * Retrieve either author's link or author's name.
183 * If the author has a home page set, return an HTML link, otherwise just return the
186 * @return string|null An HTML link if the author's url exist in user meta,
187 * else the result of get_the_author().
189 function get_the_author_link() {
190 if ( get_the_author_meta('url') ) {
191 return '<a href="' . esc_url( get_the_author_meta('url') ) . '" title="' . esc_attr( sprintf(__("Visit %s’s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>';
193 return get_the_author();
198 * Display either author's link or author's name.
200 * If the author has a home page set, echo an HTML link, otherwise just echo the
203 * @link https://codex.wordpress.org/Template_Tags/the_author_link
207 function the_author_link() {
208 echo get_the_author_link();
212 * Retrieve the number of posts by the author of the current post.
216 * @return int The number of posts by the author.
218 function get_the_author_posts() {
223 return count_user_posts( $post->post_author, $post->post_type );
227 * Display the number of posts by the author of the current post.
229 * @link https://codex.wordpress.org/Template_Tags/the_author_posts
232 function the_author_posts() {
233 echo get_the_author_posts();
237 * Retrieves an HTML link to the author page of the current post's author.
239 * Returns an HTML-formatted link using get_author_posts_url().
243 * @global object $authordata The current author's DB object.
245 * @return string An HTML link to the author page.
247 function get_the_author_posts_link() {
249 if ( ! is_object( $authordata ) ) {
254 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
255 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
256 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
261 * Filters the link to the author page of the author of the current post.
265 * @param string $link HTML link.
267 return apply_filters( 'the_author_posts_link', $link );
271 * Displays an HTML link to the author page of the current post's author.
274 * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link()
276 * @param string $deprecated Unused.
278 function the_author_posts_link( $deprecated = '' ) {
279 if ( ! empty( $deprecated ) ) {
280 _deprecated_argument( __FUNCTION__, '2.1.0' );
282 echo get_the_author_posts_link();
286 * Retrieve the URL to the author page for the user with the ID provided.
290 * @global WP_Rewrite $wp_rewrite
292 * @param int $author_id Author ID.
293 * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
294 * @return string The URL to the author's page.
296 function get_author_posts_url( $author_id, $author_nicename = '' ) {
298 $auth_ID = (int) $author_id;
299 $link = $wp_rewrite->get_author_permastruct();
301 if ( empty($link) ) {
302 $file = home_url( '/' );
303 $link = $file . '?author=' . $auth_ID;
305 if ( '' == $author_nicename ) {
306 $user = get_userdata($author_id);
307 if ( !empty($user->user_nicename) )
308 $author_nicename = $user->user_nicename;
310 $link = str_replace('%author%', $author_nicename, $link);
311 $link = home_url( user_trailingslashit( $link ) );
315 * Filters the URL to the author's page.
319 * @param string $link The URL to the author's page.
320 * @param int $author_id The author's id.
321 * @param string $author_nicename The author's nice name.
323 $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
329 * List all the authors of the site, with several options available.
331 * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
335 * @global wpdb $wpdb WordPress database abstraction object.
337 * @param string|array $args {
338 * Optional. Array or string of default arguments.
340 * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
341 * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
342 * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
343 * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
344 * @type int $number Maximum authors to return or display. Default empty (all authors).
345 * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
346 * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
347 * @type bool $show_fullname Whether to show the author's full name. Default false.
348 * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
349 * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
350 * parameter of the link. Default empty.
351 * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
352 * clickable anchor. Default empty.
353 * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
354 * @type bool $echo Whether to output the result or instead return it. Default true.
355 * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
356 * will be separated by commas.
357 * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
358 * @type array|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty.
359 * @type array|string $include Array or comma/space-separated list of author IDs to include. Default empty.
361 * @return string|void The output, if echo is set to false.
363 function wp_list_authors( $args = '' ) {
367 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
368 'optioncount' => false, 'exclude_admin' => true,
369 'show_fullname' => false, 'hide_empty' => true,
370 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
371 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
374 $args = wp_parse_args( $args, $defaults );
378 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
379 $query_args['fields'] = 'ids';
380 $authors = get_users( $query_args );
382 $author_count = array();
383 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 ) {
384 $author_count[$row->post_author] = $row->count;
386 foreach ( $authors as $author_id ) {
387 $author = get_userdata( $author_id );
389 if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
393 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
395 if ( ! $posts && $args['hide_empty'] ) {
399 if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
400 $name = "$author->first_name $author->last_name";
402 $name = $author->display_name;
405 if ( ! $args['html'] ) {
406 $return .= $name . ', ';
408 continue; // No need to go further to process HTML.
411 if ( 'list' == $args['style'] ) {
415 $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
417 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
419 if ( empty( $args['feed_image'] ) ) {
423 $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
426 if ( ! empty( $args['feed'] ) ) {
427 $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
428 $name = $args['feed'];
433 if ( ! empty( $args['feed_image'] ) ) {
434 $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
441 if ( empty( $args['feed_image'] ) ) {
446 if ( $args['optioncount'] ) {
447 $link .= ' ('. $posts . ')';
451 $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
454 $return = rtrim( $return, ', ' );
456 if ( ! $args['echo'] ) {
463 * Does this site have more than one author
465 * Checks to see if more than one author has published posts.
469 * @global wpdb $wpdb WordPress database abstraction object.
471 * @return bool Whether or not we have more than one author
473 function is_multi_author() {
476 if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
477 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
478 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
479 set_transient( 'is_multi_author', $is_multi_author );
483 * Filters whether the site has more than one author with published posts.
487 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
489 return apply_filters( 'is_multi_author', (bool) $is_multi_author );
493 * Helper function to clear the cache for number of authors.
497 function __clear_multi_author_cache() {
498 delete_transient( 'is_multi_author' );