# ユーザープロフィールカードにカスタムボタンを追加する方法

**URL:** https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826
**Category:** Development
**Created:** [2024 年 3 月 25 日午後 1:13 UTC](https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826 "2024-03-25T13:13:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![junbetterway](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/junbetterway/32/372464_2.png) [@junbetterway](https://meta.discourse.org/u/junbetterway)
#### Post date: [2024 年 3 月 25 日午後 1:13 UTC](https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826/1 "2024-03-25T13:13:46Z")

</div>

ユーザープロフィール概要とカードビューにカスタムボタンを追加するにはどうすればよいですか？  
（例：リダイレクトなど）何かを行うCTAを追加したいのですが、データ保存は不要です…

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

プロフィールカードでも同様です：

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

以下の私の最初のコードはほぼ達成していますが、ヘッダーセクションにあり、ユーザープロフィール概要とカードでそれを行う方法が見つかりませんでした。

```plaintext
<script type="text/discourse-plugin" version="0.4">
    api.decorateWidget('header-buttons:after', helper => {
        if (Discourse.User.current()) {
            const cta = function() {
                // ここで何かを行う...
            };
            return helper.h('button#create-new', {
                className: "btn fa fa-plus",
                onclick: cta
            }, 'CTA' );
        }
    });
</script>

```

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [2024 年 3 月 25 日午後 1:37 UTC](https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826/2 "2024-03-25T13:37:29Z")

</div>

こんにちは、ようこそ:wave:  
この場所では、[プラグインのアウトレット](https://meta.discourse.org/t/using-plugin-outlet-connectors-from-a-theme-or-plugin/32727)を使用する必要があります。具体的には`user-profile-controls`です。

例はこちらで確認できます。

> [@Chat button on profile page](https://meta.discourse.org/t/chat-button-on-profile-page/298816/7?u=arkshine):
>
> Here is a theme component for that. +1

---

<div class="post-metadata">

### Author: ![junbetterway](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/junbetterway/32/372464_2.png) [@junbetterway](https://meta.discourse.org/u/junbetterway)
#### Post date: [2024 年 3 月 25 日午後 2:06 UTC](https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826/3 "2024-03-25T14:06:33Z")

</div>

@Arkshineありがとうございます。プラグインは初めてなので、ヘッダーボタンと同じようなものだと思っていました。

いずれにしても、`user-profile-controls` はプラグインですか？どのようにすれば実現できるか教えていただけますか？

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [2024 年 3 月 25 日午後 3:03 UTC](https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826/4 "2024-03-25T15:03:46Z")

</div>

[Using Plugin Outlet Connectors from a Theme or Plugin](https://meta.discourse.org/t/using-plugin-outlet-connectors-from-a-theme-or-plugin/32727) を参照することをお勧めします。質問への回答があります。

基本的に、プラグインのアウトレットは、特定の場所にコードを挿入する方法です。

[theme CLI](https://meta.discourse.org/t/install-the-discourse-theme-cli-console-app-to-help-you-build-themes/82950) を使用してテーマコンポーネントを作成し、私のテーマコンポーネントで行ったように[別のファイル](https://meta.discourse.org/t/split-up-theme-javascript-into-multiple-files/119369) を使用することをお勧めします（テスト/変更できるように、私のリポジトリをクローンすることを躊躇しないでください）。

> <https://github.com/Arkshine/discourse-chat-button-in-profile/blob/main/javascripts/discourse/connectors/user-profile-controls/chat-button.hbs>

ただし、`<script>` タグから引き続きテストできます。

> [@Discourse](#):
>
> ℹ テーマ経由でテンプレートを定義する
> 
> コネクタテンプレートには専用のファイルを使用することをお勧めします。ただし、Discourse はテーマの `<head>` セクションの `<script>` タグ経由でテンプレートを定義することも引き続きサポートしています。その場合、定義は次のようになります。
> 
> ```plaintext
> <script type='text/x-handlebars' data-template-name='/connectors/{outlet-name}/{connector-name}'>
> ここにテンプレートコンテンツ
> </script>
> 
> ```

この場合、次のように記述します。

```js
<script type='text/x-handlebars' data-template-name='/connectors/user-profile-controls/my_connector_name'>
  ここにテンプレートコンテンツ
</script>

```

最初の行に注意してください。コネクタ名として `user-profile-controls` を使用し、`my_connector_name` は好きなものにすることができます。

---

<div class="post-metadata">

### Author: ![junbetterway](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/junbetterway/32/372464_2.png) [@junbetterway](https://meta.discourse.org/u/junbetterway)
#### Post date: [2024 年 3 月 25 日午後 3:08 UTC](https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826/5 "2024-03-25T15:08:08Z")

</div>

素晴らしい！これで理解できました。スクリプトタグでテストできるので、コンポーネントを作成する前にまずテストできますね。どうもありがとうございます 🍻

---

<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: [2024 年 4 月 24 日午後 3:08 UTC](https://meta.discourse.org/t/how-to-add-a-custom-button-in-user-profile-card/300826/6 "2024-04-24T15:08:42Z")

</div>

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