Ask your WordPress questions! Pay money and get answers fast! (more info)

Author archives with sub category URL WordPress

  • SOLVED

Looking for the ability to categorize authors of a blog and use a unique URL for each author using their category tag as a preceding directory in the URL.

Example:
Author Joe Smith tagged "At Sea"

WordPress Default:
http://example.com/author/joe-smith/

Desired Outcome:
http://example.com/atsea/author/joe-smith/

Answers (1)

2013-01-03

Naveen Chand answers:

You can add this to your functions.php file:


function change_author_permalinks() {
global $wp_rewrite;
$author_level = get_the_author_meta('user_level');
if ($author_level = 10) {
$wp_rewrite->author_base = 'tenthlevel';
}
if ($author_level = 9) {
$wp_rewrite->author_base = 'ninthlevel';
}
if ($author_level = 8) {
$wp_rewrite->author_base = 'eigthlevel';
}

}
add_action('init','change_author_permalinks');


Let me explain you how this works so you can customize to suit your needs:

It first queries the author meta and checks the <strong>user_level</strong>. Here you may want to change <strong>user_level</strong> to any of those mentioned in the wordpress codex: [[LINK href="http://codex.wordpress.org/Function_Reference/get_the_author_meta"]]Wordpress Codex - get_the_author_meta[[/LINK]]

Next, depending on the user_level, it rewrites the slug in this line: $wp_rewrite->author_base = 'tenthlevel';


You may want to replace the word "tenthlevel" to your own "Author Category" name. But ensure that this is without special characters and preferably small cases as this is going to be in the url. Its your author's prefixed slug!

That is it, your new author url will be this: http://example.com/<strong>tenthlevel</strong>/joe-smith

Here is where the author template if existing, will be displayed.

Note: this works if your permalink is not the default permalink structure.

Hope this helps!

EDIT:

You can even get this structure: http://example.com/<strong>tenthlevel</strong>/<strong>author</strong>/joe-smith.

Here's how:

in my code edit the rewrite lines like this:
$wp_rewrite->author_base = 'tenthlevel/author';


crbarnett comments:

Naveen thank you for the answer, it seems to be exactly what I need.

A few additional things that didn't work as planned.

Here is the resulting URL after adding and customizing the code you provided.

http://unreasonable.is/atsea/admin/

It is returning a 404 for me.
Am I missing something?


crbarnett comments:

Edit: What is working is the URL redirect to the designated url.
I can tell this by clicking on the author of this post:
http://unreasonable.is/video/wes-selke-social-venture-capital-101/

Where the 404 error is displayed.
Thank you in advance for the assistance. Much appreciated.


Naveen Chand comments:

Wordpress usually caches the rewrite rules. We need to flush them in order to tell wordpress to store the new values. When we create a rewrite rule like the one I have coded, wordpress may not recognize it until the cache is regenerated.

Its actually quite a simple process to tell wordprses to cache new change. This can be done by visiting your permalinks page (Settings > Permalinks) in your dashboard. Usually, a simple visit to this page can regenerate the cache. But you may try to click on SAVE button on this page. You don't need to edit anything as such - just visiting this page or clicking on Save button on this page will do.

Let me know if this works. Meanwhile I will keep looking out for any alternatives.


Naveen Chand comments:

By the way, your previous url is still working despite our rewrite rules. For example: [[LINK href="http://unreasonable.is/author/admin/"]][[/LINK]] still works fine. This is why I say that wordpress has cached your previous settings. Flushing could be the possible solution.


crbarnett comments:

Excellent! This worked for me.
Thank you for the great success and valuable help.

Very reasonable response time was appreciated.


Naveen Chand comments:

Sorry, the url didnt come up in the previous reply. I meant that this url works fine: http://unreasonable.is/author/admin/ and it means wordpress has cached it.


Naveen Chand comments:

You are welcome. Good to know its working!


crbarnett comments:

Hello again,

One last thing:

The author links that aren't changed with my wp_rewrite code do not work any more.
http://unreasonable.is/author/Wendy/

I'd like for only the Administrators/users levels of 10 to have the /atsea/authorname URL formatting.
Can this be done without effecting the other author URLs?

function change_author_permalinks() {

global $wp_rewrite;

$author_level = get_the_author_meta('user_level');

if ($author_level = 10) {

$wp_rewrite->author_base = 'atsea';

}
}

add_action('init','change_author_permalinks');


Naveen Chand comments:

Yes. It should work. Better still try using this:


$author_level = get_the_author_meta('ID',1); // where 1 is the user id of the admin
if ($author_level=1){
$wp_rewrite->author_base = 'atsea';
}



crbarnett comments:

This is odd. Same result, /author/ archives return 404 and all authors are directed to /atsea/authorname.

Any other possible solutions?


Naveen Chand comments:

Well, I guess that this time its the new rewrite rule that got cached. Try visiting the permalinks page once so as to flush the cached rewrite.


crbarnett comments:

Thanks for patience and persistance, this is still not working after visiting and saving permalink settings.

Cached rewrite has been flushed. I'll keep trying a few things and try not to bother any more.
Any further guidance and solutions still greatly appreciated.

Can you explain why all author's URLs might be effected by the code that exclusively calls on a specific author's ID?


Naveen Chand comments:

Can you insert this code immediately after defining <strong>global $wp_rewrite;</strong>:


$wp_rewrite->flush_rules();


This to just to reinforce flushing.


crbarnett comments:

Hey Naveen,
Again, the continued assistance is greatly needed and appreciated.

Adding the flush_rules() immediately after global $wp_rewrite; disables the /atsea/ rewrite and this URL no longer displays the author bio. http://unreasonable.is/atsea/daniel/

http://unreasonable.is/author/daniel/ is now working again.
http://unreasonable.is/author/Wendy/ All author links seem to be working correctly.

This is the code I currently have:

function change_author_permalinks() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
$author_level = get_the_author_meta('user_level');

if ($author_level = 10) {

$wp_rewrite->author_base = 'atsea';

}
}

add_action('init','change_author_permalinks');

Would expect that all authors with a level of 10 (admins) would have the /atsea/ url rewrite.
Thank you. Thank you. Thank you.


Naveen Chand comments:

If possible, can you kindly send across author.php file to my email [email protected]?


crbarnett comments:

Sent you an email from [email protected] with my authors.php file.


Naveen Chand comments:

Chris,

Please download the file (custom-author-page.php) that I have sent just now and follow these step-by-step instructions.

Step 1: You should place this file (custom-author-page.php) in your theme directory.
Step 2: Remove all of the previous code. Its not necessary now. (I really wished that it worked, but unfortunately we get the rewrite flush issues. So please remove that entire code from your functions.php) Do not leave behind any part of that code.
Step 3: Copy and paste this new code in your functions.php:

function new_change_author_permalinks() {
if (is_author('1')) {
header("Location: " .site_url() ."/atsea/admin/?id=1");
}
}

add_action('wp_head', 'new_change_author_permalinks');

//allow redirection, even if the theme starts to send output to the browser
add_action('init', 'do_output_buffer');
function do_output_buffer() {
ob_start();
}


Step 4: Go to your Dashboard -> Pages -> Add New and create a new page with the title <strong>atsea</strong> and click Publish.
Step 5: Again click Add New to create another new page with the title <strong>admin</strong>. On the right, you will find <strong>Page Attributes</strong> box. Under <strong>Parent</strong> select our recently created <strong>atsea</strong> page. Under <strong>Template</strong> select <strong>Custom Author Page</strong>. This is the most important step. Note that Custom Author Page is the template that I have customized from the author.php file that you've sent me.
Step 6: After ensuring you have selected the Parent page and Template, you can click on Publish. IMPORTANT: Do not try to view this page.
Step 7: Go to your website, click on the Admin's link or go to this url: http://unreasonable.is/author/admin

You will now be redirected to a page URL which will be in this format: http://unreasonable.is/atse/admin/?id=1

This process affects only for admin as in our function we have created our redirect only for admin. For other's it won't affect. If you are able to reach till this point successfully, then let me know. I will tell you how to generate siimilar URLs for others, if needed.


crbarnett comments:

Bravo Naveen,
I believe I have that working.

When visiting http://unreasonable.is/author/admin
I am redirected here: http://unreasonable.is/atsea/admin/

All well and fine. Next step is, as you said... creating these redirects for multiple authors, who will be contributors.

One of our authors, Daniel, is about to go on the At Sea voyage.
http://unreasonable.is/author/daniel/ - his author archive page.

How to have that redirect to http://unreasonable.is/atsea/daniel/ is the next question.
Ideally, on that page would display with the author's archives, a list of their posts.

You are a miracle worker, blessings.


Naveen Chand comments:

Thanks for your blessings, Chris.

Before we send Daniel on the At Sea voyage, we still need to fix something here. I wanted to know from you if you have done Step 5 correctly. In particular have you done this: <blockquote>Under Template select Custom Author Page. This is the most important step.</blockquote>

The reason why I am asking is, as per the coding I have done in the custom-author-page.php the admin page should show up all the previously loaded content. So kindly confirm two things:

1. Have you downloaded custom-author-page.php from my email attachments to you and have you subsequently uploaded this to your theme folder, just in the same location as author.php file.

2. After having done the above, have you selected Custom Author Page under <strong>Template</strong> dropdown on the right side of the new post created with title 'admin'

Thanks,
Naveen


crbarnett comments:

Hey Naveen,
Yes, and yes. confirmed that all steps (especially the important ones) have been followed.

The new page I set up called "admin" with permalink http://unreasonable.is/atsea/admin/ has a parent page of 'atsea' and a template 'Custom Author Page'.

The attached screen shot is from the edit page for the new 'admin' page I created.