3 * Author Template functions for use in themes.
5 * These functions must be used within the WordPress Loop.
7 * @link http://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.
19 * @uses apply_filters() Calls 'the_author' hook on the author display name.
21 * @param string $deprecated Deprecated.
22 * @return string The author's display name.
24 function get_the_author($deprecated = '') {
27 if ( !empty( $deprecated ) )
28 _deprecated_argument( __FUNCTION__, '2.1' );
31 * Filter the display name of the current post's author.
35 * @param string $authordata->display_name The author's display name.
37 return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
41 * Display the name of the author of the current post.
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().
48 * The normal, expected behavior of this function is to echo the author and not
49 * return it. However, backwards compatibility has to be maintained.
52 * @see get_the_author()
53 * @link http://codex.wordpress.org/Template_Tags/the_author
55 * @param string $deprecated Deprecated.
56 * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
57 * @return string The author's display name, from get_the_author().
59 function the_author( $deprecated = '', $deprecated_echo = true ) {
60 if ( !empty( $deprecated ) )
61 _deprecated_argument( __FUNCTION__, '2.1' );
62 if ( $deprecated_echo !== true )
63 _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
64 if ( $deprecated_echo )
65 echo get_the_author();
66 return get_the_author();
70 * Retrieve the author who last edited the current post.
74 * @uses $post The current post's DB object.
75 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
76 * @uses get_userdata() Retrieves the author's DB object.
77 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
78 * @return string The author's display name.
80 function get_the_modified_author() {
81 if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
82 $last_user = get_userdata($last_id);
85 * Filter the display name of the author who last edited the current post.
89 * @param string $last_user->display_name The author's display name.
91 return apply_filters('the_modified_author', $last_user->display_name);
96 * Display the name of the author who last edited the current post.
100 * @see get_the_author()
101 * @return string The author's display name, from get_the_modified_author().
103 function the_modified_author() {
104 echo get_the_modified_author();
108 * Retrieve the requested data of the author of the current post.
109 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
111 * @uses $authordata The current author's DB object (if $user_id not specified).
112 * @param string $field selects the field of the users record.
113 * @param int $user_id Optional. User ID.
114 * @return string The author's field from the current author's DB object.
116 function get_the_author_meta( $field = '', $user_id = false ) {
119 $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
121 $authordata = get_userdata( $user_id );
124 if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
125 $field = 'user_' . $field;
127 $value = isset( $authordata->$field ) ? $authordata->$field : '';
130 * Filter the value of the requested user metadata.
132 * The filter name is dynamic and depends on the $field parameter of the function.
136 * @param string $value The value of the metadata.
137 * @param int $user_id The user ID.
139 return apply_filters( 'get_the_author_' . $field, $value, $user_id );
143 * Retrieve the requested data of the author of the current post.
144 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
146 * @param string $field selects the field of the users record.
147 * @param int $user_id Optional. User ID.
148 * @echo string The author's field from the current author's DB object.
150 function the_author_meta( $field = '', $user_id = false ) {
151 $author_meta = get_the_author_meta( $field, $user_id );
154 * The value of the requested user metadata.
156 * The filter name is dynamic and depends on the $field parameter of the function.
160 * @param string $author_meta The value of the metadata.
161 * @param int $user_id The user ID.
163 echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
167 * Retrieve either author's link or author's name.
169 * If the author has a home page set, return an HTML link, otherwise just return the
172 * @uses get_the_author_meta()
173 * @uses get_the_author()
175 function get_the_author_link() {
176 if ( get_the_author_meta('url') ) {
177 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>';
179 return get_the_author();
184 * Display either author's link or author's name.
186 * If the author has a home page set, echo an HTML link, otherwise just echo the
189 * @link http://codex.wordpress.org/Template_Tags/the_author_link
193 * @uses get_the_author_link()
195 function the_author_link() {
196 echo get_the_author_link();
200 * Retrieve the number of posts by the author of the current post.
204 * @uses $post The current post in the Loop's DB object.
205 * @uses count_user_posts()
206 * @return int The number of posts by the author.
208 function get_the_author_posts() {
213 return count_user_posts( $post->post_author );
217 * Display the number of posts by the author of the current post.
219 * @link http://codex.wordpress.org/Template_Tags/the_author_posts
221 * @uses get_the_author_posts() Echoes returned value from function.
223 function the_author_posts() {
224 echo get_the_author_posts();
228 * Display an HTML link to the author page of the author of the current post.
230 * Does just echo get_author_posts_url() function, like the others do. The
231 * reason for this, is that another function is used to help in printing the
232 * link to the author's posts.
234 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
236 * @uses $authordata The current author's DB object.
237 * @uses get_author_posts_url()
238 * @uses get_the_author()
239 * @param string $deprecated Deprecated.
241 function the_author_posts_link($deprecated = '') {
242 if ( !empty( $deprecated ) )
243 _deprecated_argument( __FUNCTION__, '2.1' );
246 if ( !is_object( $authordata ) )
249 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
250 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
251 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
256 * Filter the link to the author page of the author of the current post.
260 * @param string $link HTML link.
262 echo apply_filters( 'the_author_posts_link', $link );
266 * Retrieve the URL to the author page for the user with the ID provided.
269 * @uses $wp_rewrite WP_Rewrite
270 * @return string The URL to the author's page.
272 function get_author_posts_url($author_id, $author_nicename = '') {
274 $auth_ID = (int) $author_id;
275 $link = $wp_rewrite->get_author_permastruct();
277 if ( empty($link) ) {
278 $file = home_url( '/' );
279 $link = $file . '?author=' . $auth_ID;
281 if ( '' == $author_nicename ) {
282 $user = get_userdata($author_id);
283 if ( !empty($user->user_nicename) )
284 $author_nicename = $user->user_nicename;
286 $link = str_replace('%author%', $author_nicename, $link);
287 $link = home_url( user_trailingslashit( $link ) );
291 * Filter the URL to the author's page.
295 * @param string $link The URL to the author's page.
296 * @param int $author_id The author's id.
297 * @param string $author_nicename The author's nice name.
299 $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
305 * List all the authors of the blog, with several options available.
307 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
311 * @param string|array $args {
312 * Optional. Array or string of default arguments.
314 * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
315 * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
316 * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
317 * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
318 * @type int $number Maximum authors to return or display. Default empty (all authors).
319 * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
320 * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
321 * @type bool $show_fullname Whether to show the author's full name. Default false.
322 * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
323 * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
324 * parameter of the link. Default empty.
325 * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
326 * clickable anchor. Default empty.
327 * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
328 * @type bool $echo Whether to output the result or instead return it. Default true.
329 * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
330 * will be separated by commas.
331 * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
332 * @type string $exclude An array, comma-, or space-separated list of author IDs to exclude. Default empty.
333 * @type string $exclude An array, comma-, or space-separated list of author IDs to include. Default empty.
335 * @return null|string The output, if echo is set to false. Otherwise null.
337 function wp_list_authors( $args = '' ) {
341 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
342 'optioncount' => false, 'exclude_admin' => true,
343 'show_fullname' => false, 'hide_empty' => true,
344 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
345 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
348 $args = wp_parse_args( $args, $defaults );
352 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
353 $query_args['fields'] = 'ids';
354 $authors = get_users( $query_args );
356 $author_count = array();
357 foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author" ) as $row ) {
358 $author_count[$row->post_author] = $row->count;
360 foreach ( $authors as $author_id ) {
361 $author = get_userdata( $author_id );
363 if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
367 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
369 if ( ! $posts && $args['hide_empty'] ) {
373 if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
374 $name = "$author->first_name $author->last_name";
376 $name = $author->display_name;
379 if ( ! $args['html'] ) {
380 $return .= $name . ', ';
382 continue; // No need to go further to process HTML.
385 if ( 'list' == $args['style'] ) {
389 $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
391 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
393 if ( empty( $args['feed_image'] ) ) {
397 $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
400 if ( ! empty( $args['feed'] ) ) {
401 $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
402 $name = $args['feed'];
407 if ( ! empty( $args['feed_image'] ) ) {
408 $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
415 if ( empty( $args['feed_image'] ) ) {
420 if ( $args['optioncount'] ) {
421 $link .= ' ('. $posts . ')';
425 $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
428 $return = rtrim( $return, ', ' );
430 if ( ! $args['echo'] ) {
437 * Does this site have more than one author
439 * Checks to see if more than one author has published posts.
442 * @return bool Whether or not we have more than one author
444 function is_multi_author() {
447 if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
448 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
449 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
450 set_transient( 'is_multi_author', $is_multi_author );
454 * Filter whether the site has more than one author with published posts.
458 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
460 return apply_filters( 'is_multi_author', (bool) $is_multi_author );
464 * Helper function to clear the cache for number of authors.
468 function __clear_multi_author_cache() {
469 delete_transient( 'is_multi_author' );
471 add_action('transition_post_status', '__clear_multi_author_cache');