0

I am trying to modify the default author link url structure. In the URL, I would also like to change the string author to operator add some author meta along with the username.

So far, I have successfully changed the string author to operator but I would also like to add the country (author meta) he/she belongs in the url. So the modified url should be like this: example.com/country-name/operator/newuser

Can someone help me on this?

0

This is just an example / idea and I haven't tested if this really works. But I think it could be something along these lines.

function custom_author_base() {
    global $wp_rewrite;
    global $wp_query;
    // On author pages you get current author for example with $wp_query
    $curauth = $wp_query->get_queried_object();
    // Get the required data
    $country = get_user_meta($curauth->ID, 'country_meta_key', true);
    // Turn the country into url friendly format and prepend it to operator
    $author_slug = sanitize_title($country) . '/operator';
    // Set the custom string as author base
    $wp_rewrite->author_base = $author_slug;
}
add_action('init', 'custom_author_base');

This example will propably need some tweaking. You might also need extra functions to handle other cases than the auhtor page where the author page url is shown. E.g. get_author_posts_url might need some tweaking I guess.

I hope this helps you achieve the result you're looking for. Or perhaps others can build on this and help you find a more complete solution.

  • Thanks for the help but as the global $wp_query; is not available yet because the function is hooked on init. So, it can't be achieved that way. – saurav.rox Feb 18 at 9:49
  • Ah, indeed. I'll think of another possible solution. – Antti Koskinen Feb 18 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.