wp/wp-includes/author-template.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     9  * @package WordPress
     9  * @package WordPress
    10  * @subpackage Template
    10  * @subpackage Template
    11  */
    11  */
    12 
    12 
    13 /**
    13 /**
    14  * Retrieve the author of the current post.
    14  * Retrieves the author of the current post.
    15  *
    15  *
    16  * @since 1.5.0
    16  * @since 1.5.0
    17  *
    17  *
    18  * @global WP_User $authordata The current author's data.
    18  * @global WP_User $authordata The current author's data.
    19  *
    19  *
    36 	 */
    36 	 */
    37 	return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
    37 	return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
    38 }
    38 }
    39 
    39 
    40 /**
    40 /**
    41  * Display the name of the author of the current post.
    41  * Displays the name of the author of the current post.
    42  *
    42  *
    43  * The behavior of this function is based off of old functionality predating
    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
    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
    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().
    46  * still use the old behavior will also pass the value from get_the_author().
    80 
    80 
    81 	return get_the_author();
    81 	return get_the_author();
    82 }
    82 }
    83 
    83 
    84 /**
    84 /**
    85  * Retrieve the author who last edited the current post.
    85  * Retrieves the author who last edited the current post.
    86  *
    86  *
    87  * @since 2.8.0
    87  * @since 2.8.0
    88  *
    88  *
    89  * @return string|void The author's display name.
    89  * @return string|void The author's display name, empty string if unknown.
    90  */
    90  */
    91 function get_the_modified_author() {
    91 function get_the_modified_author() {
    92 	$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
    92 	$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
    93 
    93 
    94 	if ( $last_id ) {
    94 	if ( $last_id ) {
    97 		/**
    97 		/**
    98 		 * Filters the display name of the author who last edited the current post.
    98 		 * Filters the display name of the author who last edited the current post.
    99 		 *
    99 		 *
   100 		 * @since 2.8.0
   100 		 * @since 2.8.0
   101 		 *
   101 		 *
   102 		 * @param string $display_name The author's display name.
   102 		 * @param string $display_name The author's display name, empty string if unknown.
   103 		 */
   103 		 */
   104 		return apply_filters( 'the_modified_author', $last_user->display_name );
   104 		return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' );
   105 	}
   105 	}
   106 }
   106 }
   107 
   107 
   108 /**
   108 /**
   109  * Display the name of the author who last edited the current post,
   109  * Displays the name of the author who last edited the current post,
   110  * if the author's ID is available.
   110  * if the author's ID is available.
   111  *
   111  *
   112  * @since 2.8.0
   112  * @since 2.8.0
   113  *
   113  *
   114  * @see get_the_author()
   114  * @see get_the_author()
   202  */
   202  */
   203 function the_author_meta( $field = '', $user_id = false ) {
   203 function the_author_meta( $field = '', $user_id = false ) {
   204 	$author_meta = get_the_author_meta( $field, $user_id );
   204 	$author_meta = get_the_author_meta( $field, $user_id );
   205 
   205 
   206 	/**
   206 	/**
   207 	 * The value of the requested user metadata.
   207 	 * Filters the value of the requested user metadata.
   208 	 *
   208 	 *
   209 	 * The filter name is dynamic and depends on the $field parameter of the function.
   209 	 * The filter name is dynamic and depends on the $field parameter of the function.
   210 	 *
   210 	 *
   211 	 * @since 2.8.0
   211 	 * @since 2.8.0
   212 	 *
   212 	 *
   215 	 */
   215 	 */
   216 	echo apply_filters( "the_author_{$field}", $author_meta, $user_id );
   216 	echo apply_filters( "the_author_{$field}", $author_meta, $user_id );
   217 }
   217 }
   218 
   218 
   219 /**
   219 /**
   220  * Retrieve either author's link or author's name.
   220  * Retrieves either author's link or author's name.
   221  *
   221  *
   222  * If the author has a home page set, return an HTML link, otherwise just return the
   222  * If the author has a home page set, return an HTML link, otherwise just return the
   223  * author's name.
   223  * author's name.
   224  *
   224  *
   225  * @since 3.0.0
   225  * @since 3.0.0
   226  *
   226  *
       
   227  * @global WP_User $authordata The current author's data.
       
   228  *
   227  * @return string|null An HTML link if the author's url exist in user meta,
   229  * @return string|null An HTML link if the author's url exist in user meta,
   228  *                     else the result of get_the_author().
   230  *                     else the result of get_the_author().
   229  */
   231  */
   230 function get_the_author_link() {
   232 function get_the_author_link() {
   231 	if ( get_the_author_meta( 'url' ) ) {
   233 	if ( get_the_author_meta( 'url' ) ) {
   232 		return sprintf(
   234 		global $authordata;
       
   235 
       
   236 		$author_url          = get_the_author_meta( 'url' );
       
   237 		$author_display_name = get_the_author();
       
   238 
       
   239 		$link = sprintf(
   233 			'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
   240 			'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
   234 			esc_url( get_the_author_meta( 'url' ) ),
   241 			esc_url( $author_url ),
   235 			/* translators: %s: Author's display name. */
   242 			/* translators: %s: Author's display name. */
   236 			esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
   243 			esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), $author_display_name ) ),
   237 			get_the_author()
   244 			$author_display_name
   238 		);
   245 		);
       
   246 
       
   247 		/**
       
   248 		 * Filters the author URL link HTML.
       
   249 		 *
       
   250 		 * @since 6.0.0
       
   251 		 *
       
   252 		 * @param string  $link       The default rendered author HTML link.
       
   253 		 * @param string  $author_url Author's URL.
       
   254 		 * @param WP_User $authordata Author user data.
       
   255 		 */
       
   256 		return apply_filters( 'the_author_link', $link, $author_url, $authordata );
   239 	} else {
   257 	} else {
   240 		return get_the_author();
   258 		return get_the_author();
   241 	}
   259 	}
   242 }
   260 }
   243 
   261 
   244 /**
   262 /**
   245  * Display either author's link or author's name.
   263  * Displays either author's link or author's name.
   246  *
   264  *
   247  * If the author has a home page set, echo an HTML link, otherwise just echo the
   265  * If the author has a home page set, echo an HTML link, otherwise just echo the
   248  * author's name.
   266  * author's name.
   249  *
   267  *
   250  * @link https://developer.wordpress.org/reference/functions/the_author_link/
   268  * @link https://developer.wordpress.org/reference/functions/the_author_link/
   254 function the_author_link() {
   272 function the_author_link() {
   255 	echo get_the_author_link();
   273 	echo get_the_author_link();
   256 }
   274 }
   257 
   275 
   258 /**
   276 /**
   259  * Retrieve the number of posts by the author of the current post.
   277  * Retrieves the number of posts by the author of the current post.
   260  *
   278  *
   261  * @since 1.5.0
   279  * @since 1.5.0
   262  *
   280  *
   263  * @return int The number of posts by the author.
   281  * @return int The number of posts by the author.
   264  */
   282  */
   269 	}
   287 	}
   270 	return count_user_posts( $post->post_author, $post->post_type );
   288 	return count_user_posts( $post->post_author, $post->post_type );
   271 }
   289 }
   272 
   290 
   273 /**
   291 /**
   274  * Display the number of posts by the author of the current post.
   292  * Displays the number of posts by the author of the current post.
   275  *
   293  *
   276  * @link https://developer.wordpress.org/reference/functions/the_author_posts/
   294  * @link https://developer.wordpress.org/reference/functions/the_author_posts/
   277  * @since 0.71
   295  * @since 0.71
   278  */
   296  */
   279 function the_author_posts() {
   297 function the_author_posts() {
   329 	}
   347 	}
   330 	echo get_the_author_posts_link();
   348 	echo get_the_author_posts_link();
   331 }
   349 }
   332 
   350 
   333 /**
   351 /**
   334  * Retrieve the URL to the author page for the user with the ID provided.
   352  * Retrieves the URL to the author page for the user with the ID provided.
   335  *
   353  *
   336  * @since 2.1.0
   354  * @since 2.1.0
   337  *
   355  *
   338  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   356  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   339  *
   357  *
   341  * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
   359  * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
   342  * @return string The URL to the author's page.
   360  * @return string The URL to the author's page.
   343  */
   361  */
   344 function get_author_posts_url( $author_id, $author_nicename = '' ) {
   362 function get_author_posts_url( $author_id, $author_nicename = '' ) {
   345 	global $wp_rewrite;
   363 	global $wp_rewrite;
   346 	$auth_ID = (int) $author_id;
   364 
   347 	$link    = $wp_rewrite->get_author_permastruct();
   365 	$author_id = (int) $author_id;
       
   366 	$link      = $wp_rewrite->get_author_permastruct();
   348 
   367 
   349 	if ( empty( $link ) ) {
   368 	if ( empty( $link ) ) {
   350 		$file = home_url( '/' );
   369 		$file = home_url( '/' );
   351 		$link = $file . '?author=' . $auth_ID;
   370 		$link = $file . '?author=' . $author_id;
   352 	} else {
   371 	} else {
   353 		if ( '' === $author_nicename ) {
   372 		if ( '' === $author_nicename ) {
   354 			$user = get_userdata( $author_id );
   373 			$user = get_userdata( $author_id );
   355 			if ( ! empty( $user->user_nicename ) ) {
   374 			if ( ! empty( $user->user_nicename ) ) {
   356 				$author_nicename = $user->user_nicename;
   375 				$author_nicename = $user->user_nicename;
   373 
   392 
   374 	return $link;
   393 	return $link;
   375 }
   394 }
   376 
   395 
   377 /**
   396 /**
   378  * List all the authors of the site, with several options available.
   397  * Lists all the authors of the site, with several options available.
   379  *
   398  *
   380  * @link https://developer.wordpress.org/reference/functions/wp_list_authors/
   399  * @link https://developer.wordpress.org/reference/functions/wp_list_authors/
   381  *
   400  *
   382  * @since 1.2.0
   401  * @since 1.2.0
   383  *
   402  *