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 * @uses $authordata The current author's DB object.
20 * @param string $deprecated Deprecated.
21 * @return string The author's display name.
23 function get_the_author($deprecated = '') {
26 if ( !empty( $deprecated ) )
27 _deprecated_argument( __FUNCTION__, '2.1' );
30 * Filter 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, backwards 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 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' );
61 if ( $deprecated_echo !== true )
62 _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
63 if ( $deprecated_echo )
64 echo get_the_author();
65 return get_the_author();
69 * Retrieve the author who last edited the current post.
73 * @return string The author's display name.
75 function get_the_modified_author() {
76 if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
77 $last_user = get_userdata($last_id);
80 * Filter the display name of the author who last edited the current post.
84 * @param string $last_user->display_name The author's display name.
86 return apply_filters('the_modified_author', $last_user->display_name);
91 * Display the name of the author who last edited the current post.
95 * @see get_the_author()
96 * @return string The author's display name, from get_the_modified_author().
98 function the_modified_author() {
99 echo get_the_modified_author();
103 * Retrieve the requested data of the author of the current post.
104 * @link https://codex.wordpress.org/Template_Tags/the_author_meta
106 * @param string $field selects the field of the users record.
107 * @param int $user_id Optional. User ID.
108 * @return string The author's field from the current author's DB object.
110 function get_the_author_meta( $field = '', $user_id = false ) {
113 $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
115 $authordata = get_userdata( $user_id );
118 if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
119 $field = 'user_' . $field;
121 $value = isset( $authordata->$field ) ? $authordata->$field : '';
124 * Filter the value of the requested user metadata.
126 * The filter name is dynamic and depends on the $field parameter of the function.
130 * @param string $value The value of the metadata.
131 * @param int $user_id The user ID.
133 return apply_filters( 'get_the_author_' . $field, $value, $user_id );
137 * Retrieve the requested data of the author of the current post.
138 * @link https://codex.wordpress.org/Template_Tags/the_author_meta
140 * @param string $field selects the field of the users record.
141 * @param int $user_id Optional. User ID.
142 * @echo string The author's field from the current author's DB object.
144 function the_author_meta( $field = '', $user_id = false ) {
145 $author_meta = get_the_author_meta( $field, $user_id );
148 * The value of the requested user metadata.
150 * The filter name is dynamic and depends on the $field parameter of the function.
154 * @param string $author_meta The value of the metadata.
155 * @param int $user_id The user ID.
157 echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
161 * Retrieve either author's link or author's name.
163 * If the author has a home page set, return an HTML link, otherwise just return the
166 function get_the_author_link() {
167 if ( get_the_author_meta('url') ) {
168 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>';
170 return get_the_author();
175 * Display either author's link or author's name.
177 * If the author has a home page set, echo an HTML link, otherwise just echo the
180 * @link https://codex.wordpress.org/Template_Tags/the_author_link
184 function the_author_link() {
185 echo get_the_author_link();
189 * Retrieve the number of posts by the author of the current post.
193 * @return int The number of posts by the author.
195 function get_the_author_posts() {
200 return count_user_posts( $post->post_author, $post->post_type );
204 * Display the number of posts by the author of the current post.
206 * @link https://codex.wordpress.org/Template_Tags/the_author_posts
209 function the_author_posts() {
210 echo get_the_author_posts();
214 * Display an HTML link to the author page of the author of the current post.
216 * Does just echo get_author_posts_url() function, like the others do. The
217 * reason for this, is that another function is used to help in printing the
218 * link to the author's posts.
220 * @link https://codex.wordpress.org/Template_Tags/the_author_posts_link
222 * @param string $deprecated Deprecated.
224 function the_author_posts_link($deprecated = '') {
225 if ( !empty( $deprecated ) )
226 _deprecated_argument( __FUNCTION__, '2.1' );
229 if ( !is_object( $authordata ) )
232 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
233 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
234 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
239 * Filter the link to the author page of the author of the current post.
243 * @param string $link HTML link.
245 echo apply_filters( 'the_author_posts_link', $link );
249 * Retrieve the URL to the author page for the user with the ID provided.
252 * @uses $wp_rewrite WP_Rewrite
253 * @return string The URL to the author's page.
255 function get_author_posts_url($author_id, $author_nicename = '') {
257 $auth_ID = (int) $author_id;
258 $link = $wp_rewrite->get_author_permastruct();
260 if ( empty($link) ) {
261 $file = home_url( '/' );
262 $link = $file . '?author=' . $auth_ID;
264 if ( '' == $author_nicename ) {
265 $user = get_userdata($author_id);
266 if ( !empty($user->user_nicename) )
267 $author_nicename = $user->user_nicename;
269 $link = str_replace('%author%', $author_nicename, $link);
270 $link = home_url( user_trailingslashit( $link ) );
274 * Filter the URL to the author's page.
278 * @param string $link The URL to the author's page.
279 * @param int $author_id The author's id.
280 * @param string $author_nicename The author's nice name.
282 $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
288 * List all the authors of the blog, with several options available.
290 * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
294 * @param string|array $args {
295 * Optional. Array or string of default arguments.
297 * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
298 * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
299 * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
300 * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
301 * @type int $number Maximum authors to return or display. Default empty (all authors).
302 * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
303 * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
304 * @type bool $show_fullname Whether to show the author's full name. Default false.
305 * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
306 * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
307 * parameter of the link. Default empty.
308 * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
309 * clickable anchor. Default empty.
310 * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
311 * @type bool $echo Whether to output the result or instead return it. Default true.
312 * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
313 * will be separated by commas.
314 * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
315 * @type string $exclude An array, comma-, or space-separated list of author IDs to exclude. Default empty.
316 * @type string $exclude An array, comma-, or space-separated list of author IDs to include. Default empty.
318 * @return null|string The output, if echo is set to false. Otherwise null.
320 function wp_list_authors( $args = '' ) {
324 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
325 'optioncount' => false, 'exclude_admin' => true,
326 'show_fullname' => false, 'hide_empty' => true,
327 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
328 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
331 $args = wp_parse_args( $args, $defaults );
335 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
336 $query_args['fields'] = 'ids';
337 $authors = get_users( $query_args );
339 $author_count = array();
340 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 ) {
341 $author_count[$row->post_author] = $row->count;
343 foreach ( $authors as $author_id ) {
344 $author = get_userdata( $author_id );
346 if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
350 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
352 if ( ! $posts && $args['hide_empty'] ) {
356 if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
357 $name = "$author->first_name $author->last_name";
359 $name = $author->display_name;
362 if ( ! $args['html'] ) {
363 $return .= $name . ', ';
365 continue; // No need to go further to process HTML.
368 if ( 'list' == $args['style'] ) {
372 $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
374 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
376 if ( empty( $args['feed_image'] ) ) {
380 $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
383 if ( ! empty( $args['feed'] ) ) {
384 $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
385 $name = $args['feed'];
390 if ( ! empty( $args['feed_image'] ) ) {
391 $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
398 if ( empty( $args['feed_image'] ) ) {
403 if ( $args['optioncount'] ) {
404 $link .= ' ('. $posts . ')';
408 $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
411 $return = rtrim( $return, ', ' );
413 if ( ! $args['echo'] ) {
420 * Does this site have more than one author
422 * Checks to see if more than one author has published posts.
425 * @return bool Whether or not we have more than one author
427 function is_multi_author() {
430 if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
431 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
432 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
433 set_transient( 'is_multi_author', $is_multi_author );
437 * Filter whether the site has more than one author with published posts.
441 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
443 return apply_filters( 'is_multi_author', (bool) $is_multi_author );
447 * Helper function to clear the cache for number of authors.
451 function __clear_multi_author_cache() {
452 delete_transient( 'is_multi_author' );