# Modifier l'URL de l'auteur dans WP vers le profil Discourse

**URL:** https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687
**Category:** WordPress
**Created:** [Avril 10, 2019, 6:39 UTC](https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687 "2019-04-10T18:39:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![davidkingham](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidkingham/32/119528_2.png) [@davidkingham](https://meta.discourse.org/u/davidkingham)
#### Post date: [Avril 10, 2019, 6:39 UTC](https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687/1 "2019-04-10T18:39:34Z")

</div>

I would like to change the author link in wordpress posts:

 ![image](https://global.discourse-cdn.com/meta/original/3X/f/4/f44c05e437e77d9bd175f86d6fcbc62dda786b4d.png)

To link to the authors profile in Discourse. I found this snippet which is similar to what I want to achieve, I modified it slightly.

```plaintext
  add_filter( 'author_link', 'change_author_link', 10, 1 );

  function change_author_link($link) {
   $username=get_the_author_meta('user_nicename');

  $link = 'https://community.naturephotographers.network/u/' . $username;
   return $link;
 }

```

But I don’t know how to retrieve the discourse username from wordpress, the `get_the_author_meta('user_nicename');` is what’s tripping me up. Thanks!

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [Avril 10, 2019, 8:49 UTC](https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687/2 "2019-04-10T20:49:11Z")

</div>

> [@davidkingham](#):
>
> But I don’t know how to retrieve the discourse username from wordpress,

For users who have set their Discourse username on WordPress, you can get the username with:

```php
get_user_meta( $user_id, 'discourse_username', true );

```

`$user_id` needs to be set to the user’s WordPress user ID. Since it’s possible that not all authors will have set their Discourse username, you should probably check that the value returned isn’t `empty` before creating the link.

---

<div class="post-metadata">

### Author: ![davidkingham](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidkingham/32/119528_2.png) [@davidkingham](https://meta.discourse.org/u/davidkingham)
#### Post date: [Avril 11, 2019, 12:27 UTC](https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687/3 "2019-04-11T00:27:01Z")

</div>

Thanks Simon, any idea why this would not be working?

```plaintext
add_filter( 'author_link', 'change_author_link', 10, 1 );

$user_id=the_author_meta('ID');

function change_author_link($link) {
   $username = get_user_meta( $user_id, 'discourse_username', true );
   $link = 'https://community.naturephotographers.network/u/' .$username;
   return $link;
 }

```

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [Avril 11, 2019, 12:50 UTC](https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687/4 "2019-04-11T00:50:19Z")

</div>

> [@davidkingham](#):
>
> any idea why this would not be working?

Yes, `$user_id` isn’t available inside of your function. You need to pass the author’s id to the function as a parameter. Try this code. It also checks if the Discourse username has been set. If it’s not set, the default author link will be returned.

```php
add_filter( 'author_link', 'wpdc_modify_author_link', 10, 2 );
function wpdc_modify_author_link( $link, $author_id ) {
    $discourse_username = get_user_meta( $author_id, 'discourse_username', true );
    if ( ! empty( $discourse_username ) ) {
        return "https://community.naturephotographers.network/u/$discourse_username";
    }

	return $link;
}

```

---

<div class="post-metadata">

### Author: ![davidkingham](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidkingham/32/119528_2.png) [@davidkingham](https://meta.discourse.org/u/davidkingham)
#### Post date: [Avril 11, 2019, 1:19 UTC](https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687/5 "2019-04-11T01:19:44Z")

</div>

Brilliant, thank you Simon!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [Mai 11, 2019, 1:29 UTC](https://meta.discourse.org/t/change-author-url-in-wp-to-discourse-profile/114687/6 "2019-05-11T01:29:13Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
