您可以使用add_permastruct()
(带ep_mask
设置为EP_AUTHORS
) 要添加正确的重写规则author_link
筛选以设置正确的作者URL:
add_action( \'init\', function(){
add_permastruct( \'%author_trip_vendor%\', \'operator/%author%\', [
\'ep_mask\' => EP_AUTHORS,
] );
} );
add_filter( \'author_link\', function( $link, $author_id, $author_nicename ){
if ( user_can( $author_id, \'trip_vendor\' ) ) {
$link = \'/operator/\' . $author_nicename;
$link = home_url( user_trailingslashit( $link ) );
}
return $link;
}, 10, 3 );
不要忘记刷新重写规则—只需访问永久链接设置页面。
更新URL上加载了非供应商配置文件example.com/operator/admin
而且还在example.com/author/admin
您可以通过以下选项之一进行修复:
发送“404”标题(“未找到页面”错误)
add_action( \'parse_request\', function( $wp ){
if ( preg_match( \'#^(author|operator)/([^/]+)#\', $wp->request, $matches ) ) {
$user = get_user_by( \'login\', $matches[2] );
if (
( \'author\' === $matches[1] && $user && user_can( $user, \'trip_vendor\' ) ) ||
( \'operator\' === $matches[1] && $user && ! user_can( $user, \'trip_vendor\' ) )
) {
$wp->query_vars = [\'error\' => 404];
}
}
} );
将用户重定向到正确的“操作员”URL
add_action( \'parse_request\', function( $wp ){
if ( preg_match( \'#^(author|operator)/([^/]+)#\', $wp->request, $matches ) ) {
$user = get_user_by( \'login\', $matches[2] );
if (
( \'author\' === $matches[1] && $user && user_can( $user, \'trip_vendor\' ) ) ||
( \'operator\' === $matches[1] && $user && ! user_can( $user, \'trip_vendor\' ) )
) {
$base = ( \'author\' === $matches[1] ) ? \'operator\' : \'author\';
wp_redirect( home_url( \'/\' . $base . \'/\' . $matches[2] ) );
exit;
}
}
} );