一种更“WordPress方式”是过滤wp_dropdown_users()
函数,而不是基本上重新创建函数。
在主题或插件中,您希望在调用之前立即添加过滤器wp_dropdown_users()
之后立即移除,以防止不必要的副作用。
add_filter( \'wp_dropdown_users\', \'wpse_260893_dropdown_users\' );
wp_dropdown_users( [ \'who\' => \'authors\' ] );
remove_filter( \'wp_dropdown_users\', \'wpse_260893_dropdown_users\' );
然后您可以将其放入插件或主题函数中。php。
//* Filter the output of wp_dropdown_users() function to replace user id with posts url
function wpse_260893_dropdown_users( $content ) {
//* Find all cases where the option value is anything - (.*)
$number_of_matches = preg_match_all( \'/<option value=\\\'(.*)\\\'/\', $content, $matches );
//* If there\'s no matches, return early
if( false === $number_of_matches || 0 === $number_of_matches ){
return $content;
}
//* Get the author posts url for each of the matches found with preg_match_all()
$posts_urls = array_map( function( $user_id ){
return get_author_posts_url( $user_id );
}, $matches[1] );
//* Replace the author ids with their corresponding author posts url
return str_replace( $matches[1], $posts_urls, $content );
}
确保在要更改
wp_dropdown_users()
功能,并在您不再需要它后立即将其删除,以便它不会影响其他任何内容。