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のチュートリアルを読んでいると、テーマのfunctions.phpファイルやサイト固有のプラグインにカスタマイザーコードを追加するよう求められることがよくあります。

問題は、わずかなミスでもサイトが壊れてしまうことだ。

この投稿では、サイトを壊すことなくWordPressにカスタムコードを追加する簡単な方法を紹介する。

How to easily add custom code in WordPress

カスタム・コード・スニペットの問題点(そして修正方法)

WordPressのチュートリアルには、テーマのfunctions.phpファイルやサイト固有のプラグインに追加するためのコードスニペットが記載されていることがよくあります。

最大の問題は、カスタムコードスニペットにほんの小さなミスがあっただけでも、WordPressサイトが壊れてアクセスできなくなることだ。

言うまでもなく、WordPressテーマを更新または変更すると、カスタマイザーはすべて削除されます。

もうひとつの問題は、サイト固有のプラグインに複数のコード・スニペットを追加すると、ファイルの管理が難しくなることだ。

幸いなことに、ユーザーがWordPressでカスタム・コード・スニペットを追加・管理するための簡単な方法があります。

WPCodeは、200万以上のWordPressサイトで使用されている最も人気のあるコードスニペットプラグインです。テーマのfunctions.phpファイルを編集することなく、WordPressにコードスニペットを簡単に追加することができます。

WPCode - Best WordPress Code Snippets Plugin

WPCodeはまた、GoogleアナリティクスFacebookピクセルGoogle Adsenseなどのトラッキングコードをサイトのヘッダーやフッターエリアに簡単に追加することができます。

スマートなコードスニペット検証により、一般的なコードエラーを防ぐことができるため、サイトが壊れる心配はありません。

さらに、WPCodeには、REST APIの無効化、コメントを無効化、Gutenbergの無効化、SVGファイルのアップロードの許可など、WordPressで最も人気のあるコードスニペットをすべて見つけることができるビルトインライブラリが付属しています。これにより、機能リクエストごとにプラグインをインストーラする必要がなくなります。

一番の利点は、すべてのコード・スニペットを1つの画面で管理でき、タグを追加して整理できることだ。

WPCodeの無料版もあり、6つのコードタイプのサポート、スマートな条件ロジックなど、WordPressでカスタマイザーコードを追加するために必要な基本的な機能をすべて備えています。

ということで、WPCodeを使ってWordPressにカスタム・コード・スニペットを簡単に追加する方法を見てみましょう。

動画チュートリアル

Subscribe to WPBeginner

文章での説明がお望みなら、このまま読み進めてほしい。

WordPressにカスタムコードスニペットを追加する

最初に行う必要があるのは、あなたのサイトに無料のWPCodeプラグインをインストールして有効化することです。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

注:無料のWPCodeプラグインは、WordPressでカスタムコードを追加するために必要なすべてを備えています。スケジュールスニペット、コードリビジョン、eコマースコンバージョントラッキングなどの高度な機能が必要な場合は、WPCode Proにアップグレードすることができます。

プラグインを有効化すると、WordPress管理バーに「Code Snippets」という新しいメニュー項目が追加されます。それをクリックすると、サイトに保存しているすべてのカスタムコードスニペットのリストが表示されます。

プラグインをインストールしたばかりなので、リストは空になります。

新規追加」ボタンをクリックして、WordPressに最初のカスタムコードスニペットを追加してください。

Click 'Add New' in WPCode to create a new custom snippet

スニペットの追加」ページが表示されます。ここでは、あらかじめ用意されているライブラリからコード・スニペットを選択するか、カスタマイザー・コードを追加することができます。

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

Add custom code in WPCode

カスタム・コード・スニペットのタイトルを入力することから始める必要があります。これは、コードを識別するのに役立つものであれば何でも構いません。

その後、コード・プレビュー・ボックスにコード・スニペットをコピー&ペーストしてください。右側のドロップダウンメニューから正しいコードタイプを選択してください。

Adding your first code snippet

上のスクリーンショットでは、カスタムコードスニペットを追加して、テストサイトからWordPressのバージョン番号を削除しています。

function wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');

コードボックスの下に、挿入オプションが表示されます。主な挿入オプションは2つあります:自動挿入とショートコードです。

Choose insertion option for code snippet

自動挿入」を選択した場合、スニペットは自動的に挿入され、サイト上で実行されます。

WordPressの管理エリア、サイトのフロントエンド、またはあらゆる場所でスニペットを自動的に実行することができます。投稿の前後や投稿コンテンツ内など、ページ固有の場所にスニペットを挿入することもできます。

Automatically insert code snippets in page-specific locations

わからない場合は、初期設定の「Run snippet everywhere」を選択してください。

ショートコード」メソッドでは、スニペットは自動的に挿入されません。スニペットを保存すると、そのスニペット専用のショートコードが作成され、サイト上の任意の場所で使用できるようになります。

さらに下にスクロールすると、端末の種類によってスニペットを読み込む場所を制限することができます。初期設定では、スニペットはすべての端末タイプに読み込まれますが、デスクトップのみ、またはモバイルのみに読み込むように設定することもできます。

Choose device type for your custom code snippets

次に、強力な「スマート条件ロジック」セクションを使用して、一連のルールに基づいて自動インサートされたスニペットを表示または非表示にすることができます。

例えば、ログイン中のユーザーだけにコード・スニペットを表示したり、特定のページURLだけにコード・スニペットを読み込んだりすることができます。

Use smart conditional logic to show or hide snippets

最後に、「基本情報」エリアが表示されます。このコードが何をするのか、どこで見つけたのか、なぜサイトに追加するのかを理解するのに役立つものであれば、何でもここに追加することができます。

Add code description and tags

コード・スニペットにタグを割り当てることもできます。これにより、トピックや機能ごとにコード・スニペットを並べ替えることができます。

priorityフィールドは、同じ場所に複数のスニペットを表示したい場合に、スニペットの実行順序をコントロールすることができます。初期設定では、すべてのスニペットの優先順位は10です。あるスニペットを他のスニペットよりも早く表示させたい場合は、スニペットの優先順位を5など低い数値に設定するだけです。

オプション設定を終えたら、右上のスイッチを「非有効化」から「有効化」に切り替え、「スニペットを保存」ボタンをクリックする。

Save and activate code snippet

コード・スニペットを保存して有効化しない場合は、「スニペットを保存」ボタンをクリックするだけです。

コードスニペットを保存して有効化すると、選択した挿入方法であれば自動的にサイトに追加されるか、ショートコードとして表示されます。

カスタムコードのエラー処理

サイト固有のプラグインやテーマファイルにカスタムコードを追加する際にミスをすると、すぐにサイトにアクセスできなくなってしまうことがよくある。

サイトに構文エラーや 500内部サーバーエラーが表示されるようになります。これを修正するには、FTPクライアントを使用して手動でコードを元に戻す必要があります。

WPCodeプラグインの優れた点は、コードの構文エラーを自動的に検出し、即座に無効化することだ。

Error handling in your custom code snippet

エラーメッセージも表示されるので、エラーをデバッグすることができる。

WPCodeのスマートコードスニペット検証は、カスタムコードを追加する際にエラーを検出します。

Smart code snippet validation to find code errors

エラーの上にマウスオーバーすると、エラーの修正方法が表示されます。

カスタム・コード・スニペットの管理

WPCodeプラグインは、WordPressでカスタムコードスニペットを管理するための簡単なユーザーインターフェースを提供します。

サイト上で有効化せずにコードスニペットを保存し、いつでも好きなときにスニペットを有効化または無効化することができます。また、コードタイプや場所によってコードスニペットをフィルターしたり、タグを使ってコードスニペットを簡単に整理することも可能です。

WPCode - WordPress Snippets Organized by Tags

また、特定のコード・スニペットをエクスポートしたり、すべてを一括エクスポートすることもできます。

Code Snippets Toolsに行き、’Export’タブをクリックするだけです。

Export your custom code snippets

サイトを別のサーバーに移動する場合、コードスニペットを新しいサイトに簡単にインポートできます。

Code Snippets ” ToolsImportページにアクセスし、エクスポートファイルをアップロードしてください。

Import code snippets with WPCode

この投稿が、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.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 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

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

  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. Moinuddin Waheed says

    I have always had reservation for installing plugins for every functionality even very small functionality.
    inserting code snippets through wpcode has revolunized it as we don’t need to have separate plugins for every small functionality that we need to have.
    The better part is that wpcode comes with most used general code snippets which we have to just use and activate.

  3. Jiří Vaněk says

    I also use WP code for inserting snippets on the website, and I agree that it’s one of the indispensable tools in website creation if someone wants to save on installing numerous plugins, tasks that snippets can often handle well. What’s also good is that artificial intelligence can now suggest tailored snippets for many purposes.

  4. ebere akadonye says

    I have tried using the plugin, but whenever I am trying to activate my code, it returns an error in Line 0, I don’t know what to do next, because I can’t find any line 0. except it is the line with the <?php

  5. Elena Ocone says

    Hi and thank you so much for all your work and support to the WordPress community.

    I used Code Snippets plugin and it was just perfect until after last update, when all code disappeared, and it became impossible to save any change: the result is a 404 error page.

  6. Carol says

    I have a long Authors page with affiliate links and want to have users be able to pull up those books for certain topics.

    For instance, say that they are books covering Gardening, Cooking, and Sewing. I want the user to be able to pull up all the books on Sewing without having to see all the other titles. Plus some titles do not tell you immediately the subjects so browsing the entire list might not catch them all.

    I want to be able to code the individual titles so they would appear on one or more of the predetermined subject lists and then provide a link to the user to click to get a dynamic list of just the titles on. that particular subject Dynamic such that any new titles I add and code will show up for them.

    I am NOT a programmer, don’t want to be, I need a plugin that will do that. Seems that I am probably not the first person wanting to do this. Is there a plugin that will do this?

  7. ton says

    Hi
    i am trying to have a function code added to the checkout page only , so how do I use the snippet to do so?

  8. THadryan says

    I tried using this plug in because everyone seems to love it, but it crashed my whole site before I could even do anything with it.

    This article convinced me to try it again, because everyone seems to like it, but EVEN JUST ACTIVATING it wrecks my whole site and I get the 500 error you refer to.

    Do you have any idea what might be going on?

    I would assume it’s my error but all I am doing is installing it and activating it.

  9. PseudoGeek says

    Would the code entered into this plugin persist through theme updates? Here’s what I’d like this plugin to do: In order to have the year update automatically in the copyright, many developers place a snippet of php code into a child theme to accomplish this. I really don’t want to make a child theme for every website just for that tiny change, but the alternative is to have my changes disappear with each theme update. But adding a child theme for something so minor seems like building a whole new fence when all you want to do is change the color of the latch on the gate. Maybe this plugin is the easy way to do this?

  10. Al Klein says

    I’m an old PHP developer, but this makes managing snippets a lot easier. (And, since I’m human, I made an error in adding a snippet – and it was just deactivated, Doing it manually would have meant making sure that the editor was still open when I tried the site, so I could go back and try to find what would have been a 500 error.

    Thank you for this post.

  11. Amrita says

    Thanks for sharing this.I am also afraid of adding custom code.But this looks easy.If I understood correctly if the code snippet has an error the plugin automatically decativates it .Right?

返信を残す

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