$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Changing author slug for a custom role without using plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Changing author slug for a custom role without using plugin

matteradmin8PV0评论

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.

Share Improve this question asked Mar 6, 2019 at 7:14 saurav.roxsaurav.rox 2051 silver badge13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

UPDATE

non-vendors profile are loaded on URL example/operator/admin and also on example/author/admin

You can fix it via one of these options:

  1. 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];
            }
        }
    } );
    
  2. 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;
            }
        }
    } );
    
Post a comment

comment list (0)

  1. No comments so far