• I am looking for a way to create 2 different url structures for 2 different user roles. That is I want authors to use the regular author url structure https://www.site.com/author/name. I have also created a custom role and I would like that role to user a different url structure https://www.site.com/gift-registry/name. The below code changes the url structure, but it changes it for every role, does anybody know how I could get this only to apply to a specific user role and keep the /author/name structure for other roles?

    function change_registry_permalinks() {
        global $wp_rewrite;
        $wp_rewrite->author_base = 'gift-registry';
        $wp_rewrite->author_structure = '/' . $wp_rewrite->author_base. '/%author%';
    }
    add_action('init','change_registry_permalinks');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Rather than touch $wp_rewrite directly, I would use WordPress’s helper functions:

    /**
     * Setup the new rewrite tags, rules, and permalink structure
     */
    function foo() {
    
    	// Setup some variables
    	$rewrite_tag = 'some_unique_id';
    	$root_slug   = 'role-name';
    
    	// Add the custom query variable
    	add_rewrite_tag( '%' . $rewrite_tag . '%', '([^/]+)' );
    
    	// Add the custom rewrite rule
    	add_rewrite_rule( $root_slug . '/([^/]+)/?$', 'index.php?' . $rewrite_tag  . '=$matches[1]', 'top' );
    
    	// Add the custom permalink structure
    	add_permastruct( $rewrite_tag, $root_slug . '/%' . $rewrite_tag . '%', array(
    		'with_front'  => false,
    		'ep_mask'     => EP_NONE,
    		'paged'       => true,  // maybe false for you?
    		'feed'        => false, // maybe true for you?
    		'forcomments' => false,
    		'walk_dirs'   => true,
    		'endpoints'   => false
    	) );
    }
    add_action( 'init', 'foo' );
    
    /**
     * Parse the query for your specific rules
     */
    function bar( $posts_query ) {
    
    	// Bail if $posts_query is not the main loop
    	if ( ! $posts_query->is_main_query() )
    		return;
    
    	// Bail if filters are suppressed on this query
    	if ( true === $posts_query->get( 'suppress_filters' ) )
    		return;
    
    	// Bail if in admin
    	if ( is_admin() )
    		return;
    
    	// Look for the user query variable match
    	$our_user = $posts_query->get( 'some_unique_id' );
    
    	// User match?
    	if ( !empty( $our_user ) ) }
    
    		// Do your thing
    		var_dump( $our_user );
    
    		// Set a variable to use later
    		$posts_query->our_user = $our_user;
    	}
    }
    add_action( 'parse_query', 'bar' );
    
    /**
     * Filter template_include, and maybe pull in a custom user role template.
     */
    function foo_template_include( $template = '' ) {
    	global $wp_query;
    
    	if ( !empty( $wp_query->our_user ) ) {
    		$template = get_query_template( 'some-file' );
    	}
    
    	return $template;
    }
    add_filter( 'template_include', 'foo_template_include' );

    The above snippet isn’t tested, but it’s similar to what bbPress does, and what WordPress core already does. In the “Do your thing” section, you may want to setup other query variables if you have other decisions you need to make.

    Hi Guys,

    Sorry John – I’m having a bad day – I don’t quite follow how I can link those 3 functions work together to create two different structures.

    I’m clearly missing something.

    Thanks

    Had an epiphany and think I’ve sorted myself out.

    In my instance I found that I didn’t need the add_rewrite_rule() in the foo() function.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom URL Base for Custom User Role’ is closed to new replies.