WordPress 每个用户都有一个用户名,它是用户在注册帐户时指定的唯一名称,默认情况下会将每个用户的用户名存储在数据库中,并用于识别用户身份和权限,荒岛本次带来 WordPress 替换用户链接的教程,只要将下面的代码添加到主题的「functions.php」文件即可。
// 荒岛一座藏有宝藏的小岛
add_filter( 'request', 'my_author' );
function my_author( $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='first_name' AND meta_value = %s", $query_vars['author_name'] ) );
if ( $author_id ) {
$query_vars['author'] = $author_id;
unset( $query_vars['author_name'] );
}
}
return $query_vars;
}
add_filter( 'author_link', 'my_author_link', 10, 3 );
function my_author_link( $link, $author_id, $author_nicename ) {
$my_name = get_user_meta( $author_id, 'first_name', true );
if ( $my_name ) {
$link = str_replace( $author_nicename, $my_name, $link );
}
return $link;
}
add_filter( 'author_link', 'author_id', 10, 2 );
function author_id( $link, $author_id) {
global $wp_rewrite;
$author_id = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty($link) ) {
$file = home_url( '/' );
$link = $file . '?author=' . $author_id;
} else {
$link = str_replace('%author%', $author_id, $link);
$link = home_url( user_trailingslashit( $link ) );
}
return $link;
}
add_filter( 'request', 'my_author' );
function my_author( $query_vars ) {
if ( array_key_exists( 'author_name', $query_vars ) ) {
global $wpdb;
$author_id=$query_vars['author_name'];
if ( $author_id ) {
$query_vars['author'] = $author_id;
unset( $query_vars['author_name'] );
}
}
return $query_vars;
}
宝藏内容来自荒岛-https://x-imagine.com/wordpress-replace-user-link.html宝藏内容来自荒岛-https://x-imagine.com/wordpress-replace-user-link.html 请按「Ctrl+D」收藏荒岛分享给好友,如您发现本文教程或宝藏链接已失效,请联系站长修正!
荒岛公众号
扫一扫关注
HDsoft27
公众号ID已复制,前往微信关注...
荒岛哔哩姬
扫一扫关注
ximagine
哔哩姬ID已复制,前往B站关注...











