Advertisement
mbis

Change author base

Aug 16th, 2020
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. function pm_detect_author_permalink($request) {
  2.     global $wp;
  3.  
  4.     // Parse the URI
  5.     // URL Format: blog/%author%
  6.     preg_match('/blog\/([^\/]+)(?:\/page\/([\d]+))?/', $wp->request, $parts);
  7.  
  8.     if(!empty($parts[1])) {
  9.         $author_slug = sanitize_title($parts[1]);
  10.         $author = get_user_by('slug', $author_slug);
  11.  
  12.         if(!empty($author->user_login)) {
  13.             $request = array(
  14.                 'author_name' => $author->user_login
  15.             );
  16.         }
  17.  
  18.         // Support pagination
  19.         if(!empty($parts[2])) {
  20.             $request['paged'] = (int) $parts[2];
  21.         }
  22.     }
  23.  
  24.     return $request;
  25. }
  26. add_filter('request', 'pm_detect_author_permalink');
  27.  
  28. function pm_change_author_base() {
  29.     global $wp_rewrite;
  30.  
  31.     $wp_rewrite->author_base = 'blog';
  32.     $wp_rewrite->author_structure = '/' . $wp_rewrite->author_base . '/%author%';
  33. }
  34. add_action('init', 'pm_change_author_base', 999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement