I am trying to change author slug for a custom role. A role called trip_vendor is added by my plugin but I want to change the author slug only for this role. I have a function but this changes author slug for every roles so that the new url would be example/operator/user.
add_filter( 'init', array( $this, 'wpte_vendor_profile_url' ) );
function wpte_vendor_profile_url()
{
global $wp_rewrite;
$author_slug = 'operator';
$wp_rewrite->author_structure = '/' . $author_slug . '/%author%';
}
I tried as suggested here but with no help.
Any help would be more than appreciable.
I am trying to change author slug for a custom role. A role called trip_vendor is added by my plugin but I want to change the author slug only for this role. I have a function but this changes author slug for every roles so that the new url would be example/operator/user.
add_filter( 'init', array( $this, 'wpte_vendor_profile_url' ) );
function wpte_vendor_profile_url()
{
global $wp_rewrite;
$author_slug = 'operator';
$wp_rewrite->author_structure = '/' . $author_slug . '/%author%';
}
I tried as suggested here but with no help.
Any help would be more than appreciable.
You can use add_permastruct()
(with ep_mask
set to EP_AUTHORS
) to add the proper rewrite rules, and the author_link
filter to set the proper author 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 );
Don't forget to flush the rewrite rules — just visit the permalink settings page.
non-vendors profile are loaded on URL
example/operator/admin
and also onexample/author/admin
You can fix it via one of these options:
Send a "404" header ("Page not found" error)
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];
}
}
} );
Redirect the user to the proper "operator" 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;
}
}
} );