X

Comment modifier l’URL de l’auteur

Snippets by IsItWP

Vous cherchez un moyen de modifier l’URL de l’auteur ? Ce snippet changera le slug par défaut, mysite.com/author/name, en mysite.com/profile/name. Cependant, vous pouvez le changer pour ce que vous voulez.

Instructions:

  1. Ajoutez ce code au fichier functions.php de votre thème ou dans un plugin spécifique à votre site.
  2. Remplacez profile à la ligne 4 par le nom de votre choix.
add_action('init', 'cng_author_base');
function cng_author_base() {
    global $wp_rewrite;
    $author_slug = 'profile'; // change slug name
    $wp_rewrite->author_base = $author_slug;
}

Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez consulter notre guide sur la manière de copier/coller correctement des extraits de code dans WordPress, afin de ne pas endommager accidentellement votre site.

Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : 9 meilleurs outils de référencement pour augmenter le trafic de votre site Web, RAPIDEMENT !

Commentaires   laisser une réponse

  1. Neither your code nor mine will work! ! ! ! It’s annoying!……

    // Change author base for author URL.
    function custom_author_base() {
    global $wp_rewrite;
    $author_slug = ‘@’; // Set author base slug to ‘@username’
    $wp_rewrite->author_base = $author_slug;
    }
    add_action( ‘init’, ‘custom_author_base’ );

    // Rewrite author URL to new URL structure.
    function custom_author_rewrite_rules( $author_rewrite ) {
    $author_new_rules = array(
    ‘@([^/]+)/?$’ => ‘index.php?author_name=$matches[1]’
    );
    $author_rewrite = array_merge( $author_new_rules, $author_rewrite );
    return $author_rewrite;
    }
    add_filter( ‘author_rewrite_rules’, ‘custom_author_rewrite_rules’ );

    // Redirect old author URL to new URL structure.
    function custom_author_redirect() {
    if ( is_author() && strpos( $_SERVER[‘REQUEST_URI’], ‘/author/’ ) !== false ) {
    wp_redirect( home_url( str_replace( ‘/author/’, ‘/’, $_SERVER[‘REQUEST_URI’] ) ), 301 );
    exit();
    }
    }
    add_action( ‘template_redirect’, ‘custom_author_redirect’ );

  2. Hi, is it possible to change the author slug only for custom post types using if condition?
    Thanks

  3. These code is to change the author base, not the author slug.

    1. This is not for the individual author names, rather it is to change the /author/ slug to something else, like /profile/. The end result would be /profile/authorname

  4. Thanks!!

  5. Thanks a bunch!!

  6. thank u

  7. thank you

  8. Remember the question from Mike Kennedy?
    How can you just have site.com/author-name/ ?

    If you set author_slug = ” ;

    this will work for the author pages but in my case I now get 404 not found errors for normal pages. It seems that an empty author slug breaks rewriting for normal pages.
    Anyone got a solution?

    1. What you may want to try is go into “permalinks” within settings and then click “save changes” to flush rewrite rules.

  9. How to change author url base on your WordPress site octobre 2, 2013 à 10:30 am

    […] to Kevin Chard for the cool […]

  10. I have been able to change the author_base permalink structure with code similar to the above, but I have created a custom role and I’m wondering if I could have 2 different author_base’s based upon the user role. The default structure for admins, authors, ect (/author/authorname) and another for the new user role (/gift-registry/authorname).

    Ryan

  11. Thank you!

  12. @wpsnipp:disqus is it possible to create custom author slugs based on user roles?

  13. add $wp_rewrite->flush_rules()
    at the end of the function it’ll work without 404 🙂

    1. thanks bro

  14. I placed this inside functions.php and it did change the slug, however I’m getting a 404 when I visit the new slug. Any suggestions? Thanks

    1. Hi Zach, if you continue to have problems you could try this plugin as you should not have any problems.

      http://wordpress.org/extend/plugins/author-slug/

      1. Hi Kevin,

        Thanks for the suggestion. However, I did see that plugin and would love to figure out how to use it without a plugin — I’m building a very large site and I’m trying to cut down the number of plugins.

        Thanks,
        Zach

        1.  Hi Zach you could simply take the code out of the plugin and place it within your functions.php and that will work fine.

        2. For anyone else having the same problem…just flush the permalinks(Going into Settings->permalinks flushes them)

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi le gratuit !