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

WordPressのタイトルに禁止ワードのリストを作成する方法

最近、あるユーザーからWordPressの投稿タイトルに禁止ワードのリストを追加する方法を尋ねられました。

これはニッチな機能のように聞こえるかもしれないが、複数著者のWordPressブログを管理する人にとっては非常に便利な機能である。禁止語句のリストを作成することで、投稿者が承認しないタイトルの投稿を公開するのを防ぐことができます。

この投稿では、WordPressのタイトルに禁止ワードのリストを作成し、コンテンツをよりコントロールできるようにする方法を紹介します。

Forbidden words list for WordPress post titles

なぜWordPressで投稿タイトルの禁句リストを作成するのか?

複数の投稿者がいるサイトで、すべての投稿者にあなたの編集スタイルや方針を伝え続けるのは簡単ではありません。WordPressのプラグインを使って編集コメントを残したり、メモを追加したり、カスタマイステータスを追加することはできますが、投稿タイトルを監視することはできません。

投稿者に公開権がある場合、不要な言葉があなたのサイトで公開される可能性があります。ユーザーから公開権を奪うことでこれを防ぐことはできますが、投稿を自分で確認して公開しなければならないため、あなたの仕事が増えることになります。

それでは、WordPressの投稿タイトルに禁止ワードのリストを簡単に追加する方法を見ていきましょう。

WordPressの投稿タイトルに禁止語リストを追加する

この方法では、WordPressサイトに手動でコードを追加する必要があります。ウェブ上のコード・スニペットをWordPressに貼り付ける方法を知っているユーザーにお勧めします。

重要:WordPressファイルにコードスニペットを追加する際は、必ずWordPressサイトをバックアップしてください。

以下のコードをテーマのfunctions.phpファイル、サイト固有のプラグイン、またはコード・スニペット・プラグインに追加するだけです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function wpb_forbidden_title($title){
global $post;
$title = $post->post_title;
  
// Add restricted words or phrases separated by a semicolon
  
$restricted_words = "word1;word2;word3";
  
$restricted_words = explode(";", $restricted_words);
foreach($restricted_words as $restricted_word){
if (stristr( $title, $restricted_word))
wp_die( __('Error: You have used a forbidden word "'. $restricted_word .'" in post title') );
}
}
add_action('publish_post', 'wpb_forbidden_title', 10, 1);

注:禁止したい単語を$restricted_words変数に追加するのを忘れないでください。セミコロンで区切ってください。

WordPressコードスニペットプラグインのWPCodeを使用して、このコードスニペットを追加することをお勧めします。

WPCode

WPCodeは、テーマのfunctions.phpファイルを編集することなく、WordPressでカスタムコードを安全かつ簡単に追加することができます。一般的なコードスニペット、スマートコードバリデーション、条件ロジックなどのライブラリが付属しています。

まず、無料のWPCodeプラグインをインストールし、有効化する必要があります。手順については、WordPressプラグインのインストール方法をご覧ください。

プラグインを有効化したら、WordPressダッシュボードからCode Snippets ” + Add Snippetページにアクセスします。

次に、「カスタムコードを追加(新規スニペット)」設定の下にある「スニペットを使用」ボタンをクリックします。

Add a new custom code snippet in WPCode

次に、スニペットのタイトルを入力し、上記のコードを「コードプレビュー」ボックスに貼り付けます。

右側のドロップダウンメニューから、コードタイプとして「PHP Snippet」を選択することを忘れないでください。

Paste code snippet into WPCode and choose the code type

その後、スイッチを’Inactive’から’Active’に切り替え、’Save Snippet’ボタンをクリックするだけです。

Activate and save your custom code snippet

このコードでは、ユーザーが投稿を公開しようとしたときに、投稿タイトルに制限のある単語が含まれていないかチェックする関数をトリガーします。投稿タイトルに制限のある単語が見つかった場合、ユーザーに次のようなエラーを表示します:

Error shown when a user tries to publish a post with a forbidden word in title

WordPressの投稿タイトルに禁止ワードのリストを追加する方法について、この投稿がお役に立てれば幸いです。また、WordPressで投稿日: にアイキャッチ画像を必須とする方法や、WordPressで人気の投稿プラグインをエキスパートが厳選した記事もご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

アバター

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.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

3件のコメントLeave a Reply

  1. petri

    Excellent, simple example. How should I modify this if I would want to make same for post content. More better if I could define post type

  2. charles langat

    Is possible to modify the code above use it to show censored words in the wordpress posts in the text area?

  3. venkat

    Hi ..in my wordpress site..i uploaded .sql files through ftp

    when i open that .sql file in separate page,files are forbidden…getting an error like 403 forbideen error…how can i access those files…

    pls help me out

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.