Title: セットアップ
Author: Akira Tachibana
Published: 2022年3月5日
Last modified: 2025年9月20日

---

# セットアップ

## この投稿内

 * [プラグインの作成](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%AE%E4%BD%9C%E6%88%90)
 * [ビルドパイプラインのセットアップ](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E3%83%93%E3%83%AB%E3%83%89%E3%83%91%E3%82%A4%E3%83%97%E3%83%A9%E3%82%A4%E3%83%B3%E3%81%AE%E3%82%BB%E3%83%83%E3%83%88%E3%82%A2%E3%83%83%E3%83%97)
 * [動作のテスト](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E5%8B%95%E4%BD%9C%E3%81%AE%E3%83%86%E3%82%B9%E3%83%88)
 * [次は ?](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E6%AC%A1%E3%81%AF)

WordPress のプラグインとしてアプリケーションを構築します。このため、WordPress 本体
のインストールが必要です。方法はさまざまですが、1つの方法として、[コードによるコントリビューション入門](https://ja.wordpress.org/team/handbook/block-editor/contributors/code/getting-started-with-code-contribution)
の指示に従ってください。セットアップの完了後は、このチュートリアルの残りの部分を
進められます。

また、このチュートリアルでは、ステート、アクション、セレクタなど、Redux の概念に
激しく依存します。もしそれらに馴染みがなければ、[Getting Started With Redux](https://redux.js.org/introduction/getting-started)
の復習から始めるとよいでしょう。

## 󠀁[プラグインの作成](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%AE%E4%BD%9C%E6%88%90)󠁿

開発はすべて WordPress のプラグイン内で行います。まず、ローカルの WordPress 環境
に `wp-content/plugins/my-first-gutenberg-app` ディレクトリを作成します。そのディレクトリ
の中に、4つのファイルを作成してください。

 * my-first-gutenberg-app.php – 新しい管理ページの作成
 * src/index.js – メインの JavaScript アプリケーション
 * style.css – 最低限のスタイルシート
 * package.json – ビルドプロセス用

以下のスニペットを使って、これらのファイルを作成してください。

**src/index.js:**

    ```notranslate
    import { createRoot } from 'react-dom';
    import './style.css';

    function MyFirstApp() {
    	return <span>Hello from JavaScript!</span>;
    }

    const root = createRoot( document.getElementById( 'my-first-gutenberg-app' ) );
    window.addEventListener(
    	'load',
    	function () {
    		root.render(
    			<MyFirstApp />,
    		);
    	},
    	false
    );
    ```

**src/style.css:**

    ```notranslate
    .toplevel_page_my-first-gutenberg-app #wpcontent {
    	background: #fff;
    	height: 1000px;
    }
    button .components-spinner {
    	width: 15px;
    	height: 15px;
    	margin-top: 0;
    	margin-bottom: 0;
    	margin-left: 0;
    }
    .form-buttons {
    	display: flex;
    }
    .my-gutenberg-form .form-buttons {
    	margin-top: 20px;
    	margin-left: 1px;
    }
    .form-error {
    	color: #cc1818;
    }
    .form-buttons button {
    	margin-right: 4px;
    }
    .form-buttons .components-spinner {
    	margin-top: 0;
    }
    #my-first-gutenberg-app {
    	max-width: 500px;
    }
    #my-first-gutenberg-app ul,
    #my-first-gutenberg-app ul li {
    	list-style-type: disc;
    }
    #my-first-gutenberg-app ul {
    	padding-left: 20px;
    }
    #my-first-gutenberg-app .components-search-control__input {
    	height: 36px;
    	margin-left: 0;
    }

    #my-first-gutenberg-app .list-controls {
    	display: flex;
    	width: 100%;
    }

    #my-first-gutenberg-app .list-controls .components-search-control {
    	flex-grow: 1;
    	margin-right: 8px;
    }
    ```

**my-first-gutenberg-app.php:**

    ```notranslate
    <?php
    /**
     * Plugin Name: My first Gutenberg App
     *
     */

    function my_admin_menu() {
    	// Create a new admin page for our app.
    	add_menu_page(
    		__( 'My first Gutenberg app', 'gutenberg' ),
    		__( 'My first Gutenberg app', 'gutenberg' ),
    		'manage_options',
    		'my-first-gutenberg-app',
    		function () {
    			echo '
    			<h2>Pages</h2>
    			<div id="my-first-gutenberg-app"></div>
    		';
    		},
    		'dashicons-schedule',
    		3
    	);
    }

    add_action( 'admin_menu', 'my_admin_menu' );

    function load_custom_wp_admin_scripts( $hook ) {
    	// Load only on ?page=my-first-gutenberg-app.
    	if ( 'toplevel_page_my-first-gutenberg-app' !== $hook ) {
    		return;
    	}

    	// Load the required WordPress packages.

    	// Automatically load imported dependencies and assets version.
    	$asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php';

    	// Enqueue CSS dependencies.
    	foreach ( $asset_file['dependencies'] as $style ) {
    		wp_enqueue_style( $style );
    	}

    	// Load our app.js.
    	wp_register_script(
    		'my-first-gutenberg-app',
    		plugins_url( 'build/index.js', __FILE__ ),
    		$asset_file['dependencies'],
    		$asset_file['version']
    	);
    	wp_enqueue_script( 'my-first-gutenberg-app' );

    	// Load our style.css.
    	wp_register_style(
    		'my-first-gutenberg-app',
    		plugins_url( 'build/style-index.css', __FILE__ ),
    		array(),
    		$asset_file['version']
    	);
    	wp_enqueue_style( 'my-first-gutenberg-app' );
    }

    add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_scripts' );
    ```

**package.json:**

    ```notranslate
    {
      "name": "09-code-data-basics-esnext",
      "version": "1.1.0",
      "private": true,
      "description": "My first Gutenberg App",
      "author": "The WordPress Contributors",
      "license": "GPL-2.0-or-later",
      "keywords": [
        "WordPress",
        "block"
      ],
      "homepage": "https://github.com/WordPress/gutenberg-examples/",
      "repository": "git+https://github.com/WordPress/gutenberg-examples.git",
      "bugs": {
        "url": "https://github.com/WordPress/gutenberg-examples/issues"
      },
      "main": "build/index.js",
      "devDependencies": {
        "@wordpress/scripts": "^24.0.0"
      },
      "scripts": {
        "build": "wp-scripts build",
        "format": "wp-scripts format",
        "lint:js": "wp-scripts lint-js",
        "packages-update": "wp-scripts packages-update",
        "start": "wp-scripts start"
      }
    }
    ```

## 󠀁[ビルドパイプラインのセットアップ](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E3%83%93%E3%83%AB%E3%83%89%E3%83%91%E3%82%A4%E3%83%97%E3%83%A9%E3%82%A4%E3%83%B3%E3%81%AE%E3%82%BB%E3%83%83%E3%83%88%E3%82%A2%E3%83%83%E3%83%97)󠁿

このチュートリアルは、読者が ESNext の構文と、 webpack などのビルドツールの概念に
慣れていることを前提とします。もし分からないところがあれば、まず、[JavaScript ビルド環境のセットアップ](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/javascript/js-build-setup/)
を復習してください。

ビルドツールをインストールするには、ターミナルでプラグインのディレクトリに移動し、`
npm install` を実行してください。

依存関係がすべて解決すれば、あとは `npm start` を実行するだけで完成です。ターミナル
で Watcher が実行されます。テキストエディタでコードを書くと、保存するたびに、自動的
にビルドされます。

## 󠀁[動作のテスト](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E5%8B%95%E4%BD%9C%E3%81%AE%E3%83%86%E3%82%B9%E3%83%88)󠁿

管理画面の「プラグイン」を開くと、「**My first Gutenberg App**」プラグインが表示
されているはずです。これを有効化すると、新しいメニュー項目「My first Gutenberg app」
が表示されます。クリックすると、「Hello from JavaScript!」と書かれたページが表示
されます。

![](https://raw.githubusercontent.com/WordPress/gutenberg/HEAD/docs/how-to-guides/
data-basics/media/setup/hello-from-js.jpg)

おめでとうございます ! これでアプリケーション構築の準備ができました !

## 󠀁[次は ?](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/1-data-basics-setup/?output_format=md#%E6%AC%A1%E3%81%AF)󠁿

 * 前のパート: [はじめに](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/)
 * 次のパート: [簡単なページリストの構築](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/2-building-a-list-of-pages)
 * (オプション) block-development-examples リポジトリ内の [完成したアプリ](https://github.com/WordPress/block-development-examples/tree/trunk/plugins/data-basics-59c8f8)
   を参照

[原文](https://github.com/WordPress/gutenberg/blob/trunk/docs/how-to-guides/data-basics/1-data-basics-setup.md)

公開日

2022年3月5日

最終更新

2025年9月20日

[  前へ: Gutenberg Dataを使用したアプリの作成](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/)

[  次へ: ページリストの構築](https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/data-basics/2-building-a-list-of-pages/)