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() {
209 return count_user_posts( get_post()->post_author );
213 * Display the number of posts by the author of the current post.
215 * @link http://codex.wordpress.org/Template_Tags/the_author_posts
217 * @uses get_the_author_posts() Echoes returned value from function.
219 function the_author_posts() {
220 echo get_the_author_posts();
224 * Display an HTML link to the author page of the author of the current post.
226 * Does just echo get_author_posts_url() function, like the others do. The
227 * reason for this, is that another function is used to help in printing the
228 * link to the author's posts.
230 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
232 * @uses $authordata The current author's DB object.
233 * @uses get_author_posts_url()
234 * @uses get_the_author()
235 * @param string $deprecated Deprecated.
237 function the_author_posts_link($deprecated = '') {
238 if ( !empty( $deprecated ) )
239 _deprecated_argument( __FUNCTION__, '2.1' );
242 if ( !is_object( $authordata ) )
245 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
246 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
247 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
252 * Filter the link to the author page of the author of the current post.
256 * @param string $link HTML link.
258 echo apply_filters( 'the_author_posts_link', $link );
262 * Retrieve the URL to the author page for the user with the ID provided.
265 * @uses $wp_rewrite WP_Rewrite
266 * @return string The URL to the author's page.
268 function get_author_posts_url($author_id, $author_nicename = '') {
270 $auth_ID = (int) $author_id;
271 $link = $wp_rewrite->get_author_permastruct();
273 if ( empty($link) ) {
274 $file = home_url( '/' );
275 $link = $file . '?author=' . $auth_ID;
277 if ( '' == $author_nicename ) {
278 $user = get_userdata($author_id);
279 if ( !empty($user->user_nicename) )
280 $author_nicename = $user->user_nicename;
282 $link = str_replace('%author%', $author_nicename, $link);
283 $link = home_url( user_trailingslashit( $link ) );
287 * Filter the URL to the author's page.
291 * @param string $link The URL to the author's page.
292 * @param int $author_id The author's id.
293 * @param string $author_nicename The author's nice name.
295 $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
301 * List all the authors of the blog, with several options available.
304 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
305 * author's name.</li>
306 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
307 * installed by default.</li>
308 * <li>show_fullname (boolean) (false): Show their full names.</li>
309 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
310 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
311 * <li>feed_image (string) (''): If isn't empty, use this image to link to
313 * <li>echo (boolean) (true): Set to false to return the output, instead of
315 * <li>style (string) ('list'): Whether to display list of authors in list form
316 * or as a string.</li>
317 * <li>html (bool) (true): Whether to list the items in html form or plaintext.
319 * <li>exclude (array): Array of user IDs to explicitly exclude.</li>
320 * <li>include (array): Array of user IDs to explicitly include.</li>
323 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
325 * @param array $args The argument array.
326 * @return null|string The output, if echo is set to false.
328 function wp_list_authors($args = '') {
332 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
333 'optioncount' => false, 'exclude_admin' => true,
334 'show_fullname' => false, 'hide_empty' => true,
335 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
336 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
339 $args = wp_parse_args( $args, $defaults );
340 extract( $args, EXTR_SKIP );
344 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
345 $query_args['fields'] = 'ids';
346 $authors = get_users( $query_args );
348 $author_count = array();
349 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 )
350 $author_count[$row->post_author] = $row->count;
352 foreach ( $authors as $author_id ) {
353 $author = get_userdata( $author_id );
355 if ( $exclude_admin && 'admin' == $author->display_name )
358 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
360 if ( !$posts && $hide_empty )
365 if ( $show_fullname && $author->first_name && $author->last_name )
366 $name = "$author->first_name $author->last_name";
368 $name = $author->display_name;
371 $return .= $name . ', ';
373 continue; // No need to go further to process HTML.
376 if ( 'list' == $style ) {
380 $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
382 if ( !empty( $feed_image ) || !empty( $feed ) ) {
384 if ( empty( $feed_image ) ) {
388 $link .= '<a href="' . get_author_feed_link( $author->ID, $feed_type ) . '"';
391 if ( !empty( $feed ) ) {
392 $alt = ' alt="' . esc_attr( $feed ) . '"';
398 if ( !empty( $feed_image ) )
399 $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . ' />';
405 if ( empty( $feed_image ) )
410 $link .= ' ('. $posts . ')';
413 $return .= ( 'list' == $style ) ? '</li>' : ', ';
416 $return = rtrim($return, ', ');
425 * Does this site have more than one author
427 * Checks to see if more than one author has published posts.
430 * @return bool Whether or not we have more than one author
432 function is_multi_author() {
435 if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
436 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
437 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
438 set_transient( 'is_multi_author', $is_multi_author );
442 * Filter whether the site has more than one author with published posts.
446 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
448 return apply_filters( 'is_multi_author', (bool) $is_multi_author );
452 * Helper function to clear the cache for number of authors.
456 function __clear_multi_author_cache() {
457 delete_transient( 'is_multi_author' );
459 add_action('transition_post_status', '__clear_multi_author_cache');