WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] customizing the author permalink structure (15 posts)

  1. Jonathan Dingman
    Member
    Posted 5 years ago #

    I'm trying to figure out how to modify the author permalink structure without modifying the core files.

    I want to move from this:

    domain.com/author/{username}

    to this:

    domain.com/author/{firstname}-{lastname}/

    Even domain.com/author/{authorID}/{firstname}-{lastname}/ if that's the only way for it to work.

    I've tried using mod_rewrite hacks like the following,

    RewriteRule ^author/([0-9])?$ ^index.php?author=$1 [L]

    or

    RewriteRule ^author/([0-9])/([A-Za-z])(/)?$ ^index.php?author=$1 [L]

    but neither have worked yet.

    I tried customizing the_author_posts_link() function inside functions.php, but that didn't work either as it never accepted the new rewrite rules.

    Anyone have any suggestions for a solution that does not involve editing core files?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

  3. Jonathan Dingman
    Member
    Posted 5 years ago #

    Yes, thank you. I've seen that, but I do not want to edit the core files.

    I want to be able to upgrade with ease without having go re-edit all the core files...

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    I've seen that, but I do not want to edit the core files.

    Who said you have to? Just edit the variables in the global $wp_rewrite object.

    In a plugin, make a function that does something like this:

    function whatever() {
    global $wp_rewrite;
    $wp_rewrite->author_structure = 'whatever';
    }
    add_action('init','whatever');

    That might work. Or use a filter, there's several:
    http://codex.wordpress.org/Function_Reference/WP_Rewrite

  5. Jonathan Dingman
    Member
    Posted 5 years ago #

    yeah, I don't know how to do that....

    I was looking at the function reference for WP rewrite already, but I couldn't figure out how to use it properly.

    I was trying to use it to get the nickname variable, but I couldn't get it to work like I wanted it to

    I even set my nickname as firstname-lastname in an effort to set the rewrite properly

    any suggestions on how to get it working like I want it to? (as described above)

    I'm not a developer, but I know bits and pieces of PHP

  6. Jonathan Dingman
    Member
    Posted 5 years ago #

    I'll give it a shot though.

  7. Jonathan Dingman
    Member
    Posted 5 years ago #

    ok great, got it working, sort of.

    I can modify the structure, but I can't figure out what other variables I can use aside from %author%

    I tried %author_name% and %author_nickname% but neither worked. any thoughts on what other variables I can use there?

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    http://codex.wordpress.org/Using_Permalinks#Structure_Tags

    And you're probably going to have to do more than just modify the structure. I was only pointing out that the wp-rewrite stuff is externally accessible. You can modify it from a plugin and change it however you see fit, more or less.

    Most likely, you're going to have to make a filter for author_rewrite_rules.

  9. Jonathan Dingman
    Member
    Posted 5 years ago #

    Ah thanks.

    I got this far

    function custom_author_rewrite() {
        global $wdbp, $wp_rewrite, $post;
    $authorNick = get_the_author_nickname();
    //  $authorNick = $authordata->nickname;
        $wp_rewrite->author_structure = '/author/$authorNick';
    }
    
    add_action ('init','custom_author_rewrite');

    but I can't seem to pass the $authorNikc properly. I was able to import the $authodata properly, but it's not wanting to utilize the nickname as the permalink.

    Maybe I'll try to use sanitize_title for the $authordata->name or something like that.

    thanks for the tips otto, appreciate it

  10. Jonathan Dingman
    Member
    Posted 5 years ago #

    hmm dangit, I'm more confused than when I started.

    I don't understand how to use filters yet. any pages I can read about how to properly use filters and apply them as needed?

  11. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

  12. Jonathan Dingman
    Member
    Posted 5 years ago #

    Argh, I'm still stuck.

    I tried following those steps, my mind just can't wrap around filtering and how to apply it to the author permalink

    based on the code above or brand new code, can anyone suggest how I can accomplish what I asked in the first post?

    (thank you for your help Otto)

  13. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    This code:

    function test($rules) {
    var_dump($rules);
    return $rules;
    }
    add_filter('author_rewrite_rules','test');

    Will almost certainly break your website, but it will also dump the author rewrite rules, allowing you to look at them. They almost certainly look like this:

    ["author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
      string(50) "index.php?author_name=$matches[1]&feed=$matches[2]"
      ["author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=>
      string(50) "index.php?author_name=$matches[1]&feed=$matches[2]"
      ["author/([^/]+)/page/?([0-9]{1,})/?$"]=>
      string(51) "index.php?author_name=$matches[1]&paged=$matches[2]"
      ["author/([^/]+)/?$"]=>
      string(33) "index.php?author_name=$matches[1]"

    Those are the author rules. Lets examine the last one closer up:

    author/([^/]+)/?$
    That's the matching rule. It's using a regular expression to catch anything with "author" at the start of it.

    index.php?author_name=$matches[1]
    That's the replacement expression. The bit in parentheses (the author name) becomes the $matches[1] in this case.

    The filter gets those rules, has the chance to change them, and then returns them. Do you see how a filter works now?

    My point is that what you are asking for is not trivial. It is a major change in the rewrite system, and while you can absolutely do it as a plugin, I'm not sure that YOU can do it. Get me? You need a fair amount of PHP programming knowledge, understanding of regular expressions, and how WordPress works at a pretty deep level.

    It's not going to be something you knock off in an hour or two, is my point.

  14. Jonathan Dingman
    Member
    Posted 5 years ago #

    Hmmm

    I do see how it's difficult.

    Otto, with all that in mind, is there *any way* change the permalink away from being the username? Maybe first name, last name, display name, or anything OTHER than the username?

  15. Jonathan Dingman
    Member
    Posted 5 years ago #

    Ok, so basically, I ended up using this solution:

    http://hackwordpress.com/change-author-archive-permalink/

    It's not automatic, but manual, but it gets the job done.

Topic Closed

This topic has been closed to new replies.

About this Topic