Shaheer
Shaheer

Reputation: 2147

Wordpress custom author urls

I want to implement custom author urls.

Currently the author urls are like this: http://site.com/author/author-name/

I want to do something like http://site.com/my-custom-url-here

For each individual user. I have tried using author_rewrite_rules filter, using the following code, this converts the url correctly but this gives me a page not found message when i browse the url

add_filter('author_link', 'no_author_base', 1000, 3);  
function no_author_base($link, $author_id) {  
    $link_base = trailingslashit(get_option('home'));  
    $link = preg_replace("|^{$link_base}author/|", '', $link);  
    return $link_base . $link;  
}  
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');  
function no_author_base_rewrite_rules($author_rewrite) {    
    global $wpdb;  
    $author_rewrite = array();  
    $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");  
    foreach($authors as $author) {  
        $author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';  
        $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';  
    }     
    return $author_rewrite;  
}  

Any help would be greatly appreciated!

Thanks. UPDATE

Problem is solved!, basically i did not know about calling the flush_rewrite_rules function.

Upvotes: 3

Views: 1870

Answers (2)

Rajib Bin Alam
Rajib Bin Alam

Reputation: 389

try this one. for get your author url.

<?php the_author_posts_link(); ?>

if post a admin or a author and want to show his/her all post in one page. so, you need to get his/her url automatic. then try this one

Upvotes: 1

Shaheer
Shaheer

Reputation: 2147

Solved!

Here's how i did it:

i did not need the author_link filter so i removed it, my custom urls are stored in usermeta table so i fetched them and passed them into the rewrite array, and most importantly I DID NOT KNOW ABOUT FLUSHING THE REWRITE CACHE AND THAT WAS THE REASON MY ORIGINAL CODE WAS NOT WORKING

Heres the full code:

add_filter('author_rewrite_rules', 'my_author_url_with_custom_url_rewrite_rules');
function my_author_url_with_custom_url_rewrite_rules($author_rewrite) {

  global $wpdb;
  $author_rewrite = array();
  $authors = $wpdb->get_results("SELECT ID, user_nicename AS nicename, meta_value as profile_name
                                    from $wpdb->users 
                                    LEFT JOIN wp_usermeta ON wp_usermeta.user_id = $wpdb->users.ID
                                    WHERE  meta_key = 'profile_name'");    

  foreach ($authors as $author) {
    $author_rewrite["{$author->profile_name}/page/?([0-9]+)/?$"] = 'index.php?author_name=' . $author->nicename . '&paged=$matches[1]';
    $author_rewrite["{$author->profile_name}/?$"] = "index.php?author_name={$author->nicename}";
  }
  return $author_rewrite;
}
flush_rewrite_rules(false);

dont forget to comment the flush_rewrite_rules call after you are done with rewrite rules, it is very expensive!

Upvotes: 2

Related Questions