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.
17 * @uses $authordata The current author's DB object.
18 * @uses apply_filters() Calls 'the_author' hook on the author display name.
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 http://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.
72 * @uses $post The current post's DB object.
73 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
74 * @uses get_userdata() Retrieves the author's DB object.
75 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
76 * @return string The author's display name.
78 function get_the_modified_author() {
79 if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
80 $last_user = get_userdata($last_id);
83 * Filter the display name of the author who last edited the current post.
87 * @param string $last_user->display_name The author's display name.
89 return apply_filters('the_modified_author', $last_user->display_name);
94 * Display the name of the author who last edited the current post.
97 * @see get_the_author()
98 * @return string The author's display name, from get_the_modified_author().
100 function the_modified_author() {
101 echo get_the_modified_author();
105 * Retrieve the requested data of the author of the current post.
106 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
108 * @uses $authordata The current author's DB object (if $user_id not specified).
109 * @param string $field selects the field of the users record.
110 * @param int $user_id Optional. User ID.
111 * @return string The author's field from the current author's DB object.
113 function get_the_author_meta( $field = '', $user_id = false ) {
116 $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
118 $authordata = get_userdata( $user_id );
121 if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
122 $field = 'user_' . $field;
124 $value = isset( $authordata->$field ) ? $authordata->$field : '';
127 * Filter the value of the requested user metadata.
129 * The filter name is dynamic and depends on the $field parameter of the function.
133 * @param string $value The value of the metadata.
134 * @param int $user_id The user ID.
136 return apply_filters( 'get_the_author_' . $field, $value, $user_id );
140 * Retrieve the requested data of the author of the current post.
141 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
143 * @param string $field selects the field of the users record.
144 * @param int $user_id Optional. User ID.
145 * @echo string The author's field from the current author's DB object.
147 function the_author_meta( $field = '', $user_id = false ) {
148 $author_meta = get_the_author_meta( $field, $user_id );
151 * The value of the requested user metadata.
153 * The filter name is dynamic and depends on the $field parameter of the function.
157 * @param string $author_meta The value of the metadata.
158 * @param int $user_id The user ID.
160 echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
164 * Retrieve either author's link or author's name.
166 * If the author has a home page set, return an HTML link, otherwise just return the
169 * @uses get_the_author_meta()
170 * @uses get_the_author()
172 function get_the_author_link() {
173 if ( get_the_author_meta('url') ) {
174 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>';
176 return get_the_author();
181 * Display either author's link or author's name.
183 * If the author has a home page set, echo an HTML link, otherwise just echo the
186 * @link http://codex.wordpress.org/Template_Tags/the_author_link
188 * @uses get_the_author_link()
190 function the_author_link() {
191 echo get_the_author_link();
195 * Retrieve the number of posts by the author of the current post.
198 * @uses $post The current post in the Loop's DB object.
199 * @uses count_user_posts()
200 * @return int The number of posts by the author.
202 function get_the_author_posts() {
203 return count_user_posts( get_post()->post_author );
207 * Display the number of posts by the author of the current post.
209 * @link http://codex.wordpress.org/Template_Tags/the_author_posts
211 * @uses get_the_author_posts() Echoes returned value from function.
213 function the_author_posts() {
214 echo get_the_author_posts();
218 * Display an HTML link to the author page of the author of the current post.
220 * Does just echo get_author_posts_url() function, like the others do. The
221 * reason for this, is that another function is used to help in printing the
222 * link to the author's posts.
224 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
226 * @uses $authordata The current author's DB object.
227 * @uses get_author_posts_url()
228 * @uses get_the_author()
229 * @param string $deprecated Deprecated.
231 function the_author_posts_link($deprecated = '') {
232 if ( !empty( $deprecated ) )
233 _deprecated_argument( __FUNCTION__, '2.1' );
236 if ( !is_object( $authordata ) )
239 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
240 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
241 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
246 * Filter the link to the author page of the author of the current post.
250 * @param string $link HTML link.
252 echo apply_filters( 'the_author_posts_link', $link );
256 * Retrieve the URL to the author page for the user with the ID provided.
259 * @uses $wp_rewrite WP_Rewrite
260 * @return string The URL to the author's page.
262 function get_author_posts_url($author_id, $author_nicename = '') {
264 $auth_ID = (int) $author_id;
265 $link = $wp_rewrite->get_author_permastruct();
267 if ( empty($link) ) {
268 $file = home_url( '/' );
269 $link = $file . '?author=' . $auth_ID;
271 if ( '' == $author_nicename ) {
272 $user = get_userdata($author_id);
273 if ( !empty($user->user_nicename) )
274 $author_nicename = $user->user_nicename;
276 $link = str_replace('%author%', $author_nicename, $link);
277 $link = home_url( user_trailingslashit( $link ) );
281 * Filter the URL to the author's page.
285 * @param string $link The URL to the author's page.
286 * @param int $author_id The author's id.
287 * @param string $author_nicename The author's nice name.
289 $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
295 * List all the authors of the blog, with several options available.
298 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
299 * author's name.</li>
300 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
301 * installed by default.</li>
302 * <li>show_fullname (boolean) (false): Show their full names.</li>
303 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
304 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
305 * <li>feed_image (string) (''): If isn't empty, use this image to link to
307 * <li>echo (boolean) (true): Set to false to return the output, instead of
309 * <li>style (string) ('list'): Whether to display list of authors in list form
310 * or as a string.</li>
311 * <li>html (bool) (true): Whether to list the items in html form or plaintext.
315 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
317 * @param array $args The argument array.
318 * @return null|string The output, if echo is set to false.
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
331 $args = wp_parse_args( $args, $defaults );
332 extract( $args, EXTR_SKIP );
336 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
337 $query_args['fields'] = 'ids';
338 $authors = get_users( $query_args );
340 $author_count = array();
341 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 )
342 $author_count[$row->post_author] = $row->count;
344 foreach ( $authors as $author_id ) {
345 $author = get_userdata( $author_id );
347 if ( $exclude_admin && 'admin' == $author->display_name )
350 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
352 if ( !$posts && $hide_empty )
357 if ( $show_fullname && $author->first_name && $author->last_name )
358 $name = "$author->first_name $author->last_name";
360 $name = $author->display_name;
363 $return .= $name . ', ';
365 continue; // No need to go further to process HTML.
368 if ( 'list' == $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( $feed_image ) || !empty( $feed ) ) {
376 if ( empty( $feed_image ) ) {
380 $link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
383 if ( !empty( $feed ) ) {
384 $title = ' title="' . esc_attr( $feed ) . '"';
385 $alt = ' alt="' . esc_attr( $feed ) . '"';
392 if ( !empty( $feed_image ) )
393 $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
399 if ( empty( $feed_image ) )
404 $link .= ' ('. $posts . ')';
407 $return .= ( 'list' == $style ) ? '</li>' : ', ';
410 $return = rtrim($return, ', ');
419 * Does this site have more than one author
421 * Checks to see if more than one author has published posts.
424 * @return bool Whether or not we have more than one author
426 function is_multi_author() {
429 if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
430 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
431 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
432 set_transient( 'is_multi_author', $is_multi_author );
436 * Filter whether the site has more than one author with published posts.
440 * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
442 return apply_filters( 'is_multi_author', (bool) $is_multi_author );
446 * Helper function to clear the cache for number of authors.
450 function __clear_multi_author_cache() {
451 delete_transient( 'is_multi_author' );
453 add_action('transition_post_status', '__clear_multi_author_cache');