Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressでメディアライブラリへのアクセスをユーザー自身のアップロードに制限する方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressの初期設定では、投稿者はサイトのメディアライブラリにあるすべての画像を見ることができます。これは、多くのゲスト投稿者を招待している場合に問題となる可能性があります。この投稿では、WordPressメディアライブラリへのアクセスをユーザー自身のアップロードに制限する方法を紹介します。

Restrict WordPress media library access to user's own uploads

なぜメディアライブラリへのアクセスをユーザー自身のアップロードに制限するのか?

WordPressでは、投稿者はメディアライブラリのすべてのファイルを見ることができます。また、管理者やエディター、他の投稿者がアップロードした画像も見ることができます。

さらに詳しく知りたい方は、WordPressのユーザー権限グループの投稿をご覧ください。

例えば、近日発売予定の商品やキャンペーンを告知するために新しい投稿を作成するとします。あなたのサイトの投稿者やゲスト投稿者は、あなたがその投稿にアップロードした画像をメディアライブラリで見ることができます。

あなたがアップロードした画像は、ユーザーが自分の記事に画像を追加する際に表示される「メディアを追加」ポップアップにも表示されます。

多くのサイトにとって、これは大きな問題ではないかもしれません。しかし、複数著者のサイトを運営している場合は、この設定を変更した方がよいでしょう。

メディアライブラリへのアクセスをユーザー自身のアップロードに簡単に制限する方法を見てみましょう。

方法1:プラグインを使ってメディアライブラリへのアクセスを制限する

この方法は簡単で、すべてのユーザーにお勧めします。

まず最初に、Restrict Media Library Accessプラグインをインストールして有効化します。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

このプラグインは箱から出してすぐに動作し、設定することはありません。

有効化すると、メディアライブラリのクエリーで現在のユーザーが管理者かエディターかをフィルターします。ユーザーの権限グループがどちらにも一致しない場合、ユーザー自身のアップロードのみを表示します。

管理者またはエディターのユーザー権限を持つユーザーは、通常通りすべてのメディアアップロードを見ることができます。

方法2:メディアライブラリへのアクセスを手動で制限する

メディアライブラリへのアクセスを制限し、管理者とエディターだけがすべてのメディアアップロードを表示できるようにする方法です。

しかし、カスタマイザーグループを追加したい場合、またはプラグインを使いたくない場合は、代わりにこの方法を試すことができます。プラグインで使用されているのと同じコードを使用しますが、あなたのニーズに合わせて変更することができます。

この方法では、WordPressファイルにコードを追加する必要があります。WordPressでコードをコピー&ペーストする方法をご覧ください。

WordPressのfunctions.phpファイルまたはサイト固有のプラグインに以下のコードを追加する必要があります。

[cbk1]

このコードは、current_user_can関数を使用して、ユーザーがプラグインを有効化したり、他のユーザーの投稿を編集したりする機能を持っているかどうかをチェックします。もしそうでなければ、メディアファイルを表示するクエリーを変更し、ユーザーのIDに限定します。

この投稿が、WordPressのメディアライブラリへのアクセスをユーザー自身のアップロードに制限する方法を学ぶのに役立てば幸いです。また、WordPressの管理エリアで投稿者を自分の投稿に制限したい場合もあるでしょう。

この投稿が気に入った場合は、WordPressの動画チュートリアルをYouTubeチャンネルに登録してください。Twitterや Facebookでもご覧いただけます。

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

14件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Noel Williams says

    Anyone looking for an update might want to consider the following which takes care of the list and grid issues

    // For list view
    add_action( ‘pre_get_posts’, ‘wpb_show_current_user_attachments_list_view’ );
    function wpb_show_current_user_attachments_list_view( $query ) {
    if ( is_admin() && $query->is_main_query() && $query->get(‘post_type’) === ‘attachment’ ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can(‘administrator’) ) {
    $query->set(‘author’, $user_id);
    }
    }
    }

    // For grid view
    add_filter( ‘ajax_query_attachments_args’, ‘wpb_show_current_user_attachments_grid_view’ );
    function wpb_show_current_user_attachments_grid_view( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can(‘administrator’) ) {
    $query[‘author’] = $user_id;
    }
    return $query;
    }

  3. Bruno says

    The plugin works, but If you switch to ‘upload.php?mode=list’ it’s possible to see all medias again. It works only on the mode=grid

  4. nathan says

    Amazing ! Its working !

    But if using wordpress app installed from mobile, all users still can access whole media library. Any solutions?

    Your help is much appreciated !

    • WPBeginner Support says

      We do not have a solution for the app at the moment but we will certainly keep an eye out.

      管理者

  5. Daniel says

    Am looking for away i can make user upload directly from their pc instead of going to media page

  6. kim says

    This may not be the post I need to be asking this on but….

    What if you create a role for say, teacher. Is there a way or plugin that would filter so that one teacher can’t see another teacher media files? If that makes sense? :)

    Teacher-username1- media (only see username1 media files)
    Teacher-username2-media (only see username2 media files)

  7. Hugh says

    Instead of:

    !current_user_can(‘activate_plugins’)
    &&
    !current_user_can(‘edit_others_posts’)

    USE:

    !current_user_can(‘administrator’)

    Because if for example you use the ‘User Role Editor’ plugin, you might want to allow the current user to be able to activate plugins AND/OR edit others posts.

    Just a thought, works for me :)

    Thanks for the post!

  8. Peter says

    Nice post.

    And is there a way to disallow uploading files directly to the media library and force users (except admins) to use Add Media button within post/page editor?

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。