• Resolved ab16

    (@ab16)


    Ideally I’d like to set up rules where specific authors marked as scholars in the CMS will get the /scholar/nicename url and everyone else fall under the /profile/nicename url. I am not sure if this is doable so instead I am trying to make both URLs useable for all authors then write a function to build author URLs so throughout the site every where a scholar link shows up it gets the /scholar/ permalink and everyone else gets /profile/. In addition I will also add logic on the author template to redirect to the appropriate URL.

    I also tried to establish multiple author bases but below code did not work, I tried some other variations but nothing seemed to work.
    $wp_rewrite->author_base('profile|scholar');

    Below is my filter on the author rewrites, this is causing many problems and the site’s permalinks to completely break and points everything back to the homepage. Any help with the below code to achieve my goals or a completely different strategy to solve the problem would be much appreciated.

    
    add_filter('author_rewrite_rules', 'new_author_base');
    public function new_author_base(){
        	$author_rewrite = array();
    	$author_rewrite["profile/([0-9a-zA-Z-]+)/?$"] = 'index.php?author_name=$matches[1]';
    	$author_rewrite["scholar/([0-9a-zA-Z-]+)/?$"] = 'index.php?author_name=$matches[1]';
    	return $author_rewrite;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Is your filter callback inside a class declaration? If not, drop the “public” keyword. If so, you’re adding your method incorrectly. Be sure to click “Save Changes” in permalink settings after you make code changes related to permalinks.

    With the above considerations, your code works fine in my installation. FWIW, you could add a single rule to cover both like this:
    $author_rewrite["(profile|scholar)/([0-9a-zA-Z-]+)/?$"] = 'index.php?author_name=$matches[2]';

    Thread Starter ab16

    (@ab16)

    Yes, it is within a class. I am also using your suggested code, that does make sense. After refreshing the permalinks everything redirects back to home page, including any posts or pages. Do you know why this would cause every link to break?

    Moderator bcworkz

    (@bcworkz)

    If in a class, add filters like this:
    add_filter('author_rewrite_rules', array( $this, 'new_author_base'));

    or if the add_filter() is outside the class and you’re adding a class method, replace $this with an instantiated object.

    Maybe that’ll help with the odd redirects. Try defining WP_DEBUG as true, then address any warnings or notices that come up. It’s possible for less than fatal errors to still break things. If everything checks out, comment out the add_filter() call to disable the rewrite, save permalinks again, then retest. You don’t need to change anything on the permalinks screen, just click the button. This recompiles the rewrite rules.

    If you have any caching plugins, deactivate it during testing. If you have caching external to WP, either disable or flush the cache after saving permalinks.

    If you still get bad redirects with add_filter() commented out, the problem is elsewhere. If the commenting out fixes the redirect, then there’s a problem with the author rewrite rule, though I can’t imagine why. Like I said, it works for me.

    Thread Starter ab16

    (@ab16)

    I believe the fix was how I was calling the add_filter as you mentioned first.

    THanks!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Creating multiple permalinks for Authors’ is closed to new replies.