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 sprintf( '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
192 esc_url( get_the_author_meta('url') ),
193 /* translators: %s: author's display name */
194 esc_attr( sprintf( __( 'Visit %s’s website' ), get_the_author() ) ),
198 return get_the_author();
203 * Display either author's link or author's name.
205 * If the author has a home page set, echo an HTML link, otherwise just echo the
208 * @link https://codex.wordpress.org/Template_Tags/the_author_link
212 function the_author_link() {
213 echo get_the_author_link();
217 * Retrieve the number of posts by the author of the current post.
221 * @return int The number of posts by the author.
223 function get_the_author_posts() {
228 return count_user_posts( $post->post_author, $post->post_type );
232 * Display the number of posts by the author of the current post.
234 * @link https://codex.wordpress.org/Template_Tags/the_author_posts
237 function the_author_posts() {
238 echo get_the_author_posts();
242 * Retrieves an HTML link to the author page of the current post's author.
244 * Returns an HTML-formatted link using get_author_posts_url().
248 * @global object $authordata The current author's DB object.
250 * @return string An HTML link to the author page.
252 function get_the_author_posts_link() {
254 if ( ! is_object( $authordata ) ) {
258 $link = sprintf( '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
259 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
260 /* translators: %s: author's display name */
261 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
266 * Filters the link to the author page of the author of the current post.
270 * @param string $link HTML link.
272 return apply_filters( 'the_author_posts_link', $link );
276 * Displays an HTML link to the author page of the current post's author.
279 * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link()
281 * @param string $deprecated Unused.
283 function the_author_posts_link( $deprecated = '' ) {
284 if ( ! empty( $deprecated ) ) {
285 _deprecated_argument( __FUNCTION__, '2.1.0' );
287 echo get_the_author_posts_link();
291 * Retrieve the URL to the author page for the user with the ID provided.
295 * @global WP_Rewrite $wp_rewrite
297 * @param int $author_id Author ID.
298 * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
299 * @return string The URL to the author's page.
301 function get_author_posts_url( $author_id, $author_nicename = '' ) {
303 $auth_ID = (int) $author_id;
304 $link = $wp_rewrite->get_author_permastruct();
306 if ( empty($link) ) {
307 $file = home_url( '/' );
308 $link = $file . '?author=' . $auth_ID;
310 if ( '' == $author_nicename ) {
311 $user = get_userdata($author_id);
312 if ( !empty($user->user_nicename) )
313 $author_nicename = $user->user_nicename;
315 $link = str_replace('%author%', $author_nicename, $link);
316 $link = home_url( user_trailingslashit( $link ) );
320 * Filters the URL to the author's page.
324 * @param string $link The URL to the author's page.
325 * @param int $author_id The author's id.
326 * @param string $author_nicename The author's nice name.
328 $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
334 * List all the authors of the site, with several options available.
336 * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
340 * @global wpdb $wpdb WordPress database abstraction object.
342 * @param string|array $args {
343 * Optional. Array or string of default arguments.
345 * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
346 * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
347 * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
348 * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
349 * @type int $number Maximum authors to return or display. Default empty (all authors).
350 * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
351 * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
352 * @type bool $show_fullname Whether to show the author's full name. Default false.
353 * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
354 * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
355 * parameter of the link. Default empty.
356 * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
357 * clickable anchor. Default empty.
358 * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
359 * @type bool $echo Whether to output the result or instead return it. Default true.
360 * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
361 * will be separated by commas.
362 * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
363 * @type array|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty.
364 * @type array|string $include Array or comma/space-separated list of author IDs to include. Default empty.
366 * @return string|void The output, if echo is set to false.
368 function wp_list_authors( $args = '' ) {
372 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
373 'optioncount' => false, 'exclude_admin' => true,
374 'show_fullname' => false, 'hide_empty' => true,
375 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
376 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
379 $args = wp_parse_args( $args, $defaults );
383 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
384 $query_args['fields'] = 'ids';
385 $authors = get_users( $query_args );
387 $author_count = array();
388 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 ) {
389 $author_count[$row->post_author] = $row->count;
391 foreach ( $authors as $author_id ) {
392 $author = get_userdata( $author_id );
394 if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
398 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
400 if ( ! $posts && $args['hide_empty'] ) {
404 if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
405 $name = "$author->first_name $author->last_name";
407 $name = $author->display_name;
410 if ( ! $args['html'] ) {
411 $return .= $name . ', ';
413 continue; // No need to go further to process HTML.
416 if ( 'list' == $args['style'] ) {
420 $link = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
421 get_author_posts_url( $author->ID, $author->user_nicename ),
422 /* translators: %s: author's display name */
423 esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
427 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
429 if ( empty( $args['feed_image'] ) ) {
433 $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
436 if ( ! empty( $args['feed'] ) ) {
437 $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
438 $name = $args['feed'];
443 if ( ! empty( $args['feed_image'] ) ) {
444 $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
451 if ( empty( $args['feed_image'] ) ) {
456 if ( $args['optioncount'] ) {
457 $link .= ' ('. $posts . ')';
461 $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
464 $return = rtrim( $return, ', ' );
466 if ( ! $args['echo'] ) {
473 * Does this site have more than one author
475 * Checks to see if more than one author has published posts.
479 * @global wpdb $wpdb WordPress database abstraction object.
481 * @return bool Whether or not we have more than one author
483 function is_multi_author() {
486 if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
487 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
488 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
489 set_transient( 'is_multi_author', $is_multi_author );
493 * Filters whether the site has more than one author with published posts.
497 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
499 return apply_filters( 'is_multi_author', (bool) $is_multi_author );
503 * Helper function to clear the cache for number of authors.
507 function __clear_multi_author_cache() {
508 delete_transient( 'is_multi_author' );