admin管理员组文章数量:1130349
Hi to the community,
is it possible to change the default username slug to nickname if is available?
By default the url is something like: /author/(admin) ,
is it possible to rewrite and change to /author/(nickname) so if a user change his nickname from the profile page the slug it will change also to the new name given by the user?
thanks a lot!
Philip
Hi to the community,
is it possible to change the default username slug to nickname if is available?
By default the url is something like: http://domain.tld/author/(admin) ,
is it possible to rewrite and change to http://domain.tld/author/(nickname) so if a user change his nickname from the profile page the slug it will change also to the new name given by the user?
thanks a lot!
Philip
- I don't think you realistically can, there's no query_var that will find posts based on a user's nickname, therefore no appropriate variable to map the nickname to in a rewrite rule. You'd have to add your own query var handling to deal with nickname queries alongside any rewrite code(it's possible in theory, but i don't think it would be elegant in practice). – t31os Commented Dec 22, 2010 at 9:27
3 Answers
Reset to default 18I see two ways to solve this problem: changing the data that forms the author URL, or changing the author URL. You probably should handle redirects too, so old URLs to user archives keep working when a user changes their nickname.
Changing the author URL
There are two parts to this question: handle incoming links with the author nickname instead of the author slug, and generate author post urls with the nickname instead of the standard slug.
The first part is solved by hooking into the request filter, checking whether it is an author request, and looking up the author by nickname instead of slug. If we find an author, we change the query parameters to use the author ID.
add_filter( 'request', 'wpse5742_request' );
function wpse5742_request( $query_vars )
{
if ( array_key_exists( 'author_name', $query_vars ) ) {
global $wpdb;
$author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) );
if ( $author_id ) {
$query_vars['author'] = $author_id;
unset( $query_vars['author_name'] );
}
}
return $query_vars;
}
The second part is done by hooking into the author_link filter and replacing the standard author part (indicated by $author_nicename) with the nickname.
add_filter( 'author_link', 'wpse5742_author_link', 10, 3 );
function wpse5742_author_link( $link, $author_id, $author_nicename )
{
$author_nickname = get_user_meta( $author_id, 'nickname', true );
if ( $author_nickname ) {
$link = str_replace( $author_nicename, $author_nickname, $link );
}
return $link;
}
Changing the data that forms the author URL
A maybe easier way would be to update the otherwise unused user_nicename field in the database. I think it is generated from the user login and never changed after that. But I'm not an expert in user management, so use it at your own risk.
add_action( 'user_profile_update_errors', 'wpse5742_set_user_nicename_to_nickname', 10, 3 );
function wpse5742_set_user_nicename_to_nickname( &$errors, $update, &$user )
{
if ( ! empty( $user->nickname ) ) {
$user->user_nicename = sanitize_title( $user->nickname, $user->display_name );
}
}
Use this plugin: http://wordpress/extend/plugins/display-name-author-permalink/
Though it's not tested for 3.2.1. I've been using it without a problem.
If you receive a header error when activating the plugin, you'll find a fix here: http://wordpresscloaker/blog/how-to-fix-wordpress-plugin-does-not-have-a-valid-header-error.html
a easy way is the plugin Author Slug
Also you can use small code:
add_action('init', 'set_new_author_base');
function set_new_author_base() {
global $wp_rewrite;
$author_slug = 'new_slug';
$wp_rewrite->author_base = $author_slug;
}
Hi to the community,
is it possible to change the default username slug to nickname if is available?
By default the url is something like: /author/(admin) ,
is it possible to rewrite and change to /author/(nickname) so if a user change his nickname from the profile page the slug it will change also to the new name given by the user?
thanks a lot!
Philip
Hi to the community,
is it possible to change the default username slug to nickname if is available?
By default the url is something like: http://domain.tld/author/(admin) ,
is it possible to rewrite and change to http://domain.tld/author/(nickname) so if a user change his nickname from the profile page the slug it will change also to the new name given by the user?
thanks a lot!
Philip
- I don't think you realistically can, there's no query_var that will find posts based on a user's nickname, therefore no appropriate variable to map the nickname to in a rewrite rule. You'd have to add your own query var handling to deal with nickname queries alongside any rewrite code(it's possible in theory, but i don't think it would be elegant in practice). – t31os Commented Dec 22, 2010 at 9:27
3 Answers
Reset to default 18I see two ways to solve this problem: changing the data that forms the author URL, or changing the author URL. You probably should handle redirects too, so old URLs to user archives keep working when a user changes their nickname.
Changing the author URL
There are two parts to this question: handle incoming links with the author nickname instead of the author slug, and generate author post urls with the nickname instead of the standard slug.
The first part is solved by hooking into the request filter, checking whether it is an author request, and looking up the author by nickname instead of slug. If we find an author, we change the query parameters to use the author ID.
add_filter( 'request', 'wpse5742_request' );
function wpse5742_request( $query_vars )
{
if ( array_key_exists( 'author_name', $query_vars ) ) {
global $wpdb;
$author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) );
if ( $author_id ) {
$query_vars['author'] = $author_id;
unset( $query_vars['author_name'] );
}
}
return $query_vars;
}
The second part is done by hooking into the author_link filter and replacing the standard author part (indicated by $author_nicename) with the nickname.
add_filter( 'author_link', 'wpse5742_author_link', 10, 3 );
function wpse5742_author_link( $link, $author_id, $author_nicename )
{
$author_nickname = get_user_meta( $author_id, 'nickname', true );
if ( $author_nickname ) {
$link = str_replace( $author_nicename, $author_nickname, $link );
}
return $link;
}
Changing the data that forms the author URL
A maybe easier way would be to update the otherwise unused user_nicename field in the database. I think it is generated from the user login and never changed after that. But I'm not an expert in user management, so use it at your own risk.
add_action( 'user_profile_update_errors', 'wpse5742_set_user_nicename_to_nickname', 10, 3 );
function wpse5742_set_user_nicename_to_nickname( &$errors, $update, &$user )
{
if ( ! empty( $user->nickname ) ) {
$user->user_nicename = sanitize_title( $user->nickname, $user->display_name );
}
}
Use this plugin: http://wordpress/extend/plugins/display-name-author-permalink/
Though it's not tested for 3.2.1. I've been using it without a problem.
If you receive a header error when activating the plugin, you'll find a fix here: http://wordpresscloaker/blog/how-to-fix-wordpress-plugin-does-not-have-a-valid-header-error.html
a easy way is the plugin Author Slug
Also you can use small code:
add_action('init', 'set_new_author_base');
function set_new_author_base() {
global $wp_rewrite;
$author_slug = 'new_slug';
$wp_rewrite->author_base = $author_slug;
}
本文标签: usersChange the Author Slug from Username to Nickname
版权声明:本文标题:users - Change the Author Slug from Username to Nickname 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749191452a2330242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论