WordPress.org

Ready to get started?Download WordPress

Forums

Assistance Changing Author Permalink / URL (13 posts)

  1. Travis Tubbs
    Member
    Posted 6 years ago #

    This is one that I've been fiddling with, but since I have no idea what I'm doing, I'm not getting very far.

    By default, the Author page is located at http://www.mydomain.com/author/author username]

    Unfortunately, when using E-mail Addresses as your WordPress username, the author name gets a little long ([firstname][lastname][mydomaincom]). What I would like to do is instead use a combination of the First and Last Name of the Author to construct the permalink/url or use the Author Nickname field instead.

    From a previous post, I can see where you can modify the slug used for authors in rewrite.php (located in wp-includes)...

    class WP_Rewrite {
    	var $permalink_structure;
    	var $use_trailing_slashes;
    	var $category_base;
    	var $tag_base;
    	var $category_structure;
    	var $tag_structure;
    	var $author_base = 'author';
    	var $author_structure;
    ...

    Sadly, that just results in 404 errors after making changes to that slug, so we'll move on. Next, I can see where the author permalink is created in that same file...

    function get_author_permastruct() {
    	if (isset($this->author_structure)) {
    		return $this->author_structure;
    	}
    
    	if (empty($this->permalink_structure)) {
    		$this->author_structure = '';
    		return false;
    	}

    ... but I can't really can't make anything out of that.

    Finally, I noticed another file called author-template.php (also in wp-includes) that gave a little more insight on how the permalink is constructed...

    function get_author_posts_url($author_id, $author_nicename = '') {
    	global $wpdb, $wp_rewrite, $post;
    	$auth_ID = (int) $author_id;
    	$link = $wp_rewrite->get_author_permastruct();
    
    	if ( empty($link) ) {
    		$file = get_option('home') . '/';
    		$link = $file . '?author=' . $auth_ID;
    	} else {
    		if ( '' == $author_nicename ) {
    			$user = get_userdata($author_id);
    			if ( !empty($user->user_nicename) )
    				$author_nicename = $user->user_nicename;
    		}
    		$link = str_replace('%author%', $author_nicename, $link);
    		$link = get_option('home') . trailingslashit($link);
    	}
    
    	$link = apply_filters('author_link', $link, $author_id, $author_nicename);
    
    	return $link;
    }

    ... but once again, I have no idea what I'm looking at. Any thoughts and suggestions into this would be grateful. Thanks in advance!

  2. jcow
    Member
    Posted 6 years ago #

    Not sure if this will help, but make sure that if you make any changes to the rewrite.php file, that you go into your admin area and update your permalink structure. This will make sure that your new rules end up in actual use.

    Example. I changed "var $author_base = 'author';" to "var $author_base = 'blogs';" (thanks for the heads up on taht). After I changed this, it broke and gave me a 404, but when i updated the permalink structure, everything works perfectly.

    Options -> Permalinks -> Update Permalink Structure

  3. jcow
    Member
    Posted 6 years ago #

    Also. It isn't the user_login that is technically used for the url of the page, it is the user_nicename in the database. So after signing up a new user, you could go change their nicename.

    Not the most dynamic method, but maybe it will help.

  4. craigchilds
    Member
    Posted 6 years ago #

    just to add to the rewrite.php edit:
    if you want to completely remove any base ie: blog.com/name instead of blog.com/author/name
    then only one extra change needs to be made:
    leave the author_base blank
    and then find this code below:

    function get_author_permastruct() {
    if (isset($this->author_structure)) {
    return $this->author_structure;
    }

    if (empty($this->permalink_structure)) {
    $this->author_structure = '';
    return false;
    }

    $this->author_structure = $this->front . $this->author_base . '/%author%';

    return $this->author_structure;
    }

    and remove the forward slash from here:
    $this->author_base . '/%author%';

    so

    $this->author_base . '%author%';

    and you're sweet. no author base, no double foward slash.

  5. alexleonard
    Member
    Posted 6 years ago #

    And would it be simple to change the "author_base" value?

    Eg I want it to display

    url/artist/$author_nicename/

    instead of

    url/author/$author_nicename/

  6. Doodlebee
    Member
    Posted 5 years ago #

    sweet! I know this is an old thread, but Alex's last post wasn't answered. I needed Craig's little advice there - and it works like a charm. Take jcow's advice as well and redo your permalink structure (just save it again) and it works like a charm. Awesome tidbit! (Running 2.5.1, by the way...)

  7. alexleonard
    Member
    Posted 5 years ago #

    I'd love if someone could point that one out to me - ideally without messing around with wp install files.. I try to avoid doing that if at all possible. Managing 20 WP sites and trying to remember which ones have hacks and which ones don't is a bad idea. Everything's got to be altered via a plugin or functions.php in my mind..!

  8. Jeremy Boggs
    Member
    Posted 5 years ago #

    Alex - I've used this code in a plugin, and it works OK for me:

    function change_author_permalinks() {
    
        global $wp_rewrite;
    
        // Change the value of the author permalink base to whatever you want here
        $wp_rewrite->author_base = 'artist';
    
        $wp_rewrite->flush_rules();
    }
    
    add_action('init','change_author_permalinks');

    This does a couple of things:

    1) allows you to redefine the $author_base property in rewrite.php without actually editing that file.
    2) flushes the rewrite rules so they reflect the change in teh author base, instead of you having to save your permalink structure again through the admin.

    You can change 'artist' to whatever you want the author permalink base to be.

    Hope this helps.

  9. Ddesbiens
    Member
    Posted 5 years ago #

    jeremyboggs, many thanks for this hack. I was installed Advanced Permalinks plugin just to change the author permalink. I haved many bugs in my permalinks with it.

    With your hack, all work fine !

  10. amitsuman
    Member
    Posted 5 years ago #

    Hi All
    I have use it but i need to
    blog.com/name instead of blog.com/author/name
    So i use craigchilds comment but it don't work.
    So if any one have any idea about theis then please reply.
    Thanks

  11. alexleonard
    Member
    Posted 5 years ago #

    @jeremyboggs - that looks great! I'll test that out now :)

  12. alexleonard
    Member
    Posted 5 years ago #

    Score! That worked beautifully. Thanks Jeremy.

  13. Prasad Thombre
    Member
    Posted 5 years ago #

    amitsuman

    I have tested craigchilds comment and its working fine with my website...http://www.myloverswish.com.

    craigchilds Many Thanks for your valuable input.

Topic Closed

This topic has been closed to new replies.

About this Topic