Plugin Basics

プラグインの基本

Getting Started Getting Started

Getting Started

At its simplest, a WordPress plugin is a PHP file with a WordPress plugin header comment. It’s highly recommended that you create a directory to hold your plugin so that all of your plugin’s files are neatly organized in one place.

もっとも単純なプラグインは、WordPressプラグインのヘッダーコメントのPHPファイルです。 作成するプラグインのファイルが一箇所に整理されているような形で、保持するディレクトリを用意することを強く推奨します。

To get started creating a new plugin, follow the steps below.

あたらしいプラグインの作成をはじめるにあたって、下記の手順を踏んでください。

  1. Navigate to your WordPress installation’s wp-content directory.
  2. Open the plugins directory.
  3. Create a new directory and name it after your plugin (e.g. plugin-name).
  4. Open your new plugin’s directory.
  5. Create a new PHP file (it’s also good to name this file after your plugin, e.g. plugin-name.php).
  1. インストールしたWordPressのwp-contentディレクトリに移動します。
  2. pluginsディレクトリを開きます。
  3. 新しくディレクトリを作成し、プラグインの名前で命名します(例:plugin-name)。
  4. 新しいプラグインのディレクトリを開きます。
  5. PHPファイルを作成します(ファイル名は、プラグインの名前で命名すると良いでしょう。例:plugin-name.php

Here’s what the process looks like on the Unix command line:

以下がUnixのコマンドラインでの手順です:

wordpress$ cd wp-content
wp-content$ cd plugins
plugins$ mkdir plugin-name
plugins$ cd plugin-name
plugin-name$ vi plugin-name.php

In the example above, “vi” is the name of the text editor. Use whichever editor that is comfortable for you.

上記の例の「vi」はテキストエディタの名前です。普段利用しているエディターの名前を入れてください。

Now that you’re editing your new plugin’s PHP file, you’ll need to add a plugin header comment.  This is a specially formatted PHP block comment that contains metadata about your plugin, such as its name and author.  At the very least, the plugin header comment must contain the name of your plugin.  Only one file in your plugin’s folder should have the header comment—if your plugin has multiple PHP files, only one of those files should have the comment.

新しいプラグインのファイルを編集する時に、plugin header commentを記述する必要があります。これはプラグインの名前や作成者などのメタデータを含んだ、このためにフォーマットを定められたPHPのブロックコメントです。最低限、plugin header commentにはプラグインのnameを含める必要があります。プラグインのフォルダー内のファイルの一つだけがplugin header commentを持つべきです。プラグインが、複数のPHPファイルで構成されている場合、その中の一つのファイルがplugin header commentを持つべきです。

<?php
/*
Plugin Name: YOUR PLUGIN NAME
*/

After you save the file, you should be able to see your plugin listed in your WordPress site. Log in to your WordPress site, and click Plugins on the left navigation pane of your WordPress Admin. This page displays a listing of all the plugins your WordPress site has. Your new plugin should now be in that list!

ファイルを保存すると、WordPressで作成したサイトにあなたのプラグインがリストに加えられたか確認できます。WordPressで作成したサイトにログインし、WordPressの管理画面の左ナビゲーションのプラグインをクリックします。その画面ではWordPressで作成したサイトに登録されているすべてのプラグインの一覧が表示しています。あなたが作成した新しいプラグインも一覧の中に加えられているでしょう!

Hooks: Actions and Filters Hooks: Actions and Filters

フック: アクションフックとフィルターフック

WordPress hooks allow you to tap into WordPress at specific points to change how WordPress behaves without editing any core files.

WordPressのフックを利用すると、コアファイルを編集することなく、WordPressの特定の動作を変更できます。

There are two types of hooks within WordPress: actions and filters. Actions allow you to add or change WordPress functionality, while filters allow you to alter content as it is loaded and displayed to the website user.

WordPressには、2つのタイプのフックがあります。アクションフィルターです。

Hooks are not just for plugin developers; hooks are used extensively to provide default functionality by WordPress core itself. Other hooks are unused place holders that are simply available for you to tap into when you need to alter how WordPress works. This is what makes WordPress so flexible.

フックはプラグイン開発者だけのためのものではありません。WordPressのコア自体の基本的な機能を提供するために広く使われています。 それ以外のフックは、WordPressの動作を置き換える必要があるときに簡単に利用できる、未使用のプレースホルダーです。

Basic Hooks Basic Hooks

The 3 basic hooks you’ll need when creating a plugin are the register_activation_hook(), register_deactivation_hook() and the register_uninstall_hook().

The activation hook is run when you activate your plugin. You would use this to provide a function to set up your plugin — for example, creating some default settings in the options table.

The deactivation hook is run when you deactivate your plugin. You would use this to provide a function that clears any temporary data stores by your plugin.

These uninstall methods are used to clean up after your plugin is deleted using the WordPress Admin. You would use this to delete all data created by your plugin, such as any options that were added to the options table.

Adding Hooks Adding Hooks

You can add your own, custom hooks with do_action(), which will enable developers to extend your plugin by passing functions through your hooks.

Removing Hooks Removing Hooks

You can also use invoke remove_action() to remove a function that was defined earlier. For example, if your plugin is an add-on to another plugin, you can use remove_action() with the same function callback that was added by the previous plugin with add_action(). The priority of actions is important in these situations, as remove_action() would need to run after the initial add_action().

You should be careful when removing an action from a hook, as well as when altering priorities, because it can be difficult to see how these changes will affect other interactions with the same hook. We highly recommend testing frequently.

You can learn more about creating hooks and interacting with them in the Hooks section of this handbook.

WordPress APIs WordPress APIs

Did you know that WordPress provides a number of Application Programming Interfaces (APIs)? These APIs can greatly simplify the code you need to write in your plugins. You don’t want to reinvent the wheel, especially when so many people have done a lot of the work and testing for you.

The most common one is the Options API, which makes it easy to store data in the database for your plugin. If you’re thinking of using cURL in your plugin, the HTTP API might be of interest to you.

Since we’re talking about plugins, you’ll want to study the Plugin API. It has a variety of functions that will assist you in developing plugins.

How WordPress Loads Plugins How WordPress Loads Plugins

When WordPress loads the list of installed plugins on the Plugins page of the WordPress Admin, it searches through the plugins folder (and its sub-folders) to find PHP files with WordPress plugin header comments. If your entire plugin consists of just a single PHP file, like Hello Dolly, the file could be located directly inside the root of the plugins folder. But more commonly, plugin files will reside in their own folder, named after the plugin.

Sharing your Plugin Sharing your Plugin

Sometimes a plugin you create is just for your site. But many people like to share their plugins with the rest of the WordPress community. Before sharing your plugin, one thing you need to do is choose a license. This lets the user of your plugin know how they are allowed to use your code. To maintain compatibility with WordPress core, it is recommended that you pick a license that works with GNU General Public License (GPLv2+).