Title: ユーザー権限の確認
Author: Aki Hamano
Published: 2025年5月9日
Last modified: 2025年5月11日

---

# ユーザー権限の確認

## この投稿内

 * [ユーザーの権限グループと権限](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%81%ae%e6%a8%a9%e9%99%90%e3%82%b0%e3%83%ab%e3%83%bc%e3%83%97%e3%81%a8%e6%a8%a9%e9%99%90)
    - [ヒエラルキー (階層構造)](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e3%83%92%e3%82%a8%e3%83%a9%e3%83%ab%e3%82%ad%e3%83%bc-%e9%9a%8e%e5%b1%a4%e6%a7%8b%e9%80%a0)
 * [例](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e4%be%8b)
    - [制限なし](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e5%88%b6%e9%99%90%e3%81%aa%e3%81%97)
    - [特定の権限に限定](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e7%89%b9%e5%ae%9a%e3%81%ae%e6%a8%a9%e9%99%90%e3%81%ab%e9%99%90%e5%ae%9a)

[↑ ページの先頭へ戻る](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#wp--skip-link--target)

プラグインがユーザーにデータを送信させる場合、— それが管理者側であれ、公開側であれ—「
ユーザー権限」をチェックする必要があります。

## 󠀁[ユーザーの権限グループと権限](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%81%ae%e6%a8%a9%e9%99%90%e3%82%b0%e3%83%ab%e3%83%bc%e3%83%97%e3%81%a8%e6%a8%a9%e9%99%90)󠁿

効率的なセキュリティ・レイヤを構築するうえで、最も重要なステップは、ユーザー・アクセス
許可システムを導入することです。WordPress は、これを[ユーザーの権限グループと権限](https://ja.wordpress.org/team/handbook/plugin-development/users/roles-and-capabilities/)
という形で提供しています。

WordPress にログインしているすべてのユーザーには、ユーザーの権限グループに応じて、
特定のユーザー権限が自動的に割り当てられます。

**ユーザー権限グループ** は、ユーザーがどのグループに属しているかを示す表現です。
各グループは、事前に定義された権限の特定のセットを持っています。

たとえば、Web サイトのメインユーザーは管理者というユーザー権限グループを持ち、他の
ユーザーは編集者や投稿者といった権限グループを持つかもしれません。1つの権限グループ
に複数のユーザーを割り当てることもできます。たとえば、1つの Web サイトに2人の管理者
がいるような場合です。

**ユーザー権限** は、各ユーザーまたはユーザー権限グループに割り当てる、特定のアクセス
許可です。

たとえば、管理者は `manage_options` 権限を持っており、Web サイトのオプションを表示、
編集、保存できます。一方、編集者にはこの権限がないため、オプションを操作できません。

これらの権限は、管理画面のさまざまな場所でチェックされます。ユーザー権限グループ
に割り当てられた権限によって、メニュー、機能、その他の WordPress 体験の側面が追加
または削除される可能性があります。

**プラグインを作成する際には、現在のユーザーが必要な権限を持っている場合にのみ、
コードを実行するようにしてください。**

### 󠀁[ヒエラルキー (階層構造)](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e3%83%92%e3%82%a8%e3%83%a9%e3%83%ab%e3%82%ad%e3%83%bc-%e9%9a%8e%e5%b1%a4%e6%a7%8b%e9%80%a0)󠁿

ユーザー権限グループが高いほど、ユーザーはより多くの権限を持ちます。各ユーザー権限
グループは、ヒエラルキーの直前の権限グループを継承します。

たとえば、「管理者」は、1つのサイト・インストールにおける最高のユーザー権限グループ
であり、「購読者」、「寄稿者」、「投稿者」、「編集者」の権限グループとその権限を
継承します。

## 󠀁[例](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e4%be%8b)󠁿

### 󠀁[制限なし](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e5%88%b6%e9%99%90%e3%81%aa%e3%81%97)󠁿

以下の例では、投稿をゴミ箱に捨てるためのリンクを、フロントエンドに作成しています。
このコードはユーザーの権限をチェックしないため、**サイトへの訪問者なら誰でも、投稿
をゴミ箱に捨てることができます !**

    ```notranslate
    /**
     * Generate a Delete link based on the homepage url.
     *
     * @param string $content   Existing content.
     *
     * @return string|null
     */
    function wporg_generate_delete_link( $content ) {
      // Run only for single post page.
      if ( is_single() && in_the_loop() && is_main_query() ) {
        // Add query arguments: action, post.
        $url = add_query_arg(
          [
            'action' => 'wporg_frontend_delete',
            'post'   => get_the_ID(),
          ], home_url()
        );

        return $content . ' <a href="' . esc_url( $url ) . '">' . esc_html__( 'Delete Post', 'wporg' ) . '</a>';
      }

      return null;
    }

    /**
     * Request handler
     */
    function wporg_delete_post() {
      if ( isset( $_GET['action'] ) && 'wporg_frontend_delete' === $_GET['action'] ) {

        // Verify we have a post id.
        $post_id = ( isset( $_GET['post'] ) ) ? ( $_GET['post'] ) : ( null );

        // Verify there is a post with such a number.
        $post = get_post( (int) $post_id );
        if ( empty( $post ) ) {
          return;
        }

        // Delete the post.
        wp_trash_post( $post_id );

        // Redirect to admin page.
        $redirect = admin_url( 'edit.php' );
        wp_safe_redirect( $redirect );

        // We are done.
        die;
      }
    }

    /**
     * Add the delete link to the end of the post content.
     */
    add_filter( 'the_content', 'wporg_generate_delete_link' );

    /**
     * Register our request handler with the init hook.
     */
    add_action( 'init', 'wporg_delete_post' );
    ```

### 󠀁[特定の権限に限定](https://ja.wordpress.org/team/handbook/plugin-development/security/checking-user-capabilities/?output_format=md#%e7%89%b9%e5%ae%9a%e3%81%ae%e6%a8%a9%e9%99%90%e3%81%ab%e9%99%90%e5%ae%9a)󠁿

上記の例では、サイトの訪問者であれば誰でも「削除」リンクをクリックし、投稿をゴミ箱
に捨てることができます。しかし、「削除」リンクをクリックできるのは、「編集者」以上
に限定したいと思っています。

これを実現するため、「編集者」以上しか持っていない、`edit_others_posts` 権限を現在
のユーザーが持っているかどうかをチェックします:

    ```notranslate
    /**
     * Generate a Delete link based on the homepage url.
     *
     * @param string $content   Existing content.
     *
     * @return string|null
     */
    function wporg_generate_delete_link( $content ) {
      // Run only for single post page.
      if ( is_single() && in_the_loop() && is_main_query() ) {
        // Add query arguments: action, post.
        $url = add_query_arg(
          [
            'action' => 'wporg_frontend_delete',
            'post'   => get_the_ID(),
          ], home_url()
        );

        return $content . ' <a href="' . esc_url( $url ) . '">' . esc_html__( 'Delete Post', 'wporg' ) . '</a>';
      }

      return null;
    }

    /**
     * Request handler
     */
    function wporg_delete_post() {
      if ( isset( $_GET['action'] ) && 'wporg_frontend_delete' === $_GET['action'] ) {

        // Verify we have a post id.
        $post_id = ( isset( $_GET['post'] ) ) ? ( $_GET['post'] ) : ( null );

        // Verify there is a post with such a number.
        $post = get_post( (int) $post_id );
        if ( empty( $post ) ) {
          return;
        }

        // Delete the post.
        wp_trash_post( $post_id );

        // Redirect to admin page.
        $redirect = admin_url( 'edit.php' );
        wp_safe_redirect( $redirect );

        // We are done.
        die;
      }
    }

    /**
     * Add delete post ability
     */
    add_action('plugins_loaded', 'wporg_add_delete_post_ability');

    function wporg_add_delete_post_ability() {    
      if ( current_user_can( 'edit_others_posts' ) ) {
        /**
         * Add the delete link to the end of the post content.
         */
        add_filter( 'the_content', 'wporg_generate_delete_link' );

        /**
         * Register our request handler with the init hook.
         */
        add_action( 'init', 'wporg_delete_post' );
      }
    }
    ```

[原文](https://developer.wordpress.org/plugins/security/checking-user-capabilities/)/
[日本語訳](https://github.com/jawordpressorg/developer-plugins-handbook/blob/main/security/checking-user-capabilities/index.md)

公開日

2025年5月9日

最終更新

2025年5月11日

[  前へ: プラグインのセキュリティ](https://ja.wordpress.org/team/handbook/plugin-development/security/)

[  次へ: データ検証](https://ja.wordpress.org/team/handbook/plugin-development/security/data-validation/)