Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial: Create your First App with Gutenberg Data #38250

Merged
merged 52 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
209751a
First draft
adamziel Jan 26, 2022
cf2478c
Explain we need to create a few pages first
adamziel Jan 26, 2022
f680f68
Update the asset loading order
adamziel Jan 26, 2022
e2e4893
explain that core-data builds on top of the data in order to facilita…
adamziel Jan 26, 2022
3f867d0
Replace @TODO with Coming soon:
adamziel Jan 26, 2022
07cd084
Use both JSX and uncompiled snippets
adamziel Jan 31, 2022
6ccef39
Don't use wp.components inside JSX syntax
adamziel Jan 31, 2022
8ab44c5
Explain the JSX syntax vs compiled syntax
adamziel Jan 31, 2022
2c7bcd0
Explain all the dependencies of our app.js
adamziel Jan 31, 2022
a45dbd4
Grammar improvements
adamziel Jan 31, 2022
ba2b0fa
Link to last part and next part
adamziel Jan 31, 2022
c36f2b3
Improve cropping of the screenshots
adamziel Jan 31, 2022
2cfc656
Add the full example to the repository
adamziel Jan 31, 2022
9856c49
Rename full to finished
adamziel Jan 31, 2022
8112526
Rename first-gutenberg-app to my-first-gutenberg-app
adamziel Jan 31, 2022
2942090
Update docs/how-to-guides/data-basics/1-setup.md
adamziel Feb 1, 2022
96d4f41
Update docs/how-to-guides/data-basics/1-setup.md
adamziel Feb 1, 2022
f7c4ecb
Update docs/how-to-guides/data-basics/README.md
adamziel Feb 3, 2022
9c3569a
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
2b79ae2
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
6655e97
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
335d4ea
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
9da2b5d
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
15d0ae2
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
2d615a5
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
2df6ec3
Update docs/how-to-guides/data-basics/1-setup.md
adamziel Feb 3, 2022
7388b7c
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
0662b19
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
6f0f9ad
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
a45fc7a
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
c67f5f1
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
4f8cb3f
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
aaaf632
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
53225b7
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
6f73b43
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
3c5166f
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
fb28f44
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
e51a9ac
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
2ecc2ae
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 3, 2022
ae22627
"how to use it" -> "how can we use it"
adamziel Feb 3, 2022
ca728b5
Migrate the tutorial to the ESNext syntax
adamziel Feb 3, 2022
6fbea81
Refactor selector arguments into a separate variable
adamziel Feb 3, 2022
e2b1ca1
Fix 404 link
adamziel Feb 4, 2022
a17461e
Migrate from wp.data to module imports
adamziel Feb 4, 2022
52b00cd
Refer to WordPress packages by their name
adamziel Feb 4, 2022
91246da
Link package names to the repository
adamziel Feb 4, 2022
7948a68
Remove "Coming Soon" from README.md
adamziel Feb 4, 2022
f51f853
Rely on the code in gutenberg-examples repo instead of including the …
adamziel Feb 4, 2022
825cd9b
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 4, 2022
bb09fe5
Update docs/how-to-guides/data-basics/2-building-a-list-of-pages.md
adamziel Feb 4, 2022
db7214e
Be more explicit about the consequences of passing different argument…
adamziel Feb 4, 2022
2edf6d4
Merge branch 'doc/first-app-with-gutenberg-data' of github.com:WordPr…
adamziel Feb 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions docs/how-to-guides/data-basics/1-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
## Setup

We will build the application as a WordPress plugin, which means you need to have WordPress itself installed. One way to do this is by following the instructions on the [Getting Started](/docs/contributors/code/getting-started-with-code-contribution.md) page. Once your setup is complete, you can follow along with the rest of this tutorial.

## Creating a plugin

We'll do all the development inside of a WordPress plugin. Let's start by creating a `wp-content/plugins/my-first-gutenberg-app` directory in your local WordPress environment. We will need to create three files inside that directory:

* my-first-gutenberg-app.php – to create a new admin page
* src/index.js – for our JavaScript application
* style.css – for the minimal stylesheet
* package.json – for the build process

Go ahead and create these files using the following snippets:

**src/index.js:**
```js
import { render } from '@wordpress/element';

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

window.addEventListener( 'load', function() {
render(
<MyFirstApp />,
document.querySelector( '#my-first-gutenberg-app' )
);
}, false );
```

**style.css:**
```css
.toplevel_page_my-first-gutenberg-app #wpcontent {
background: #FFF;
}
#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.php:**
```php
<?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( 'style.css', __FILE__ )
);
wp_enqueue_style( 'my-first-gutenberg-app' );
}

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

**package.json:**

```json
{
"name": "05-recipe-card-esnext",
"version": "1.1.0",
"private": true,
"description": "Example: Recipe Card (ESNext).",
"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": "^18.0.1"
},
"scripts": {
"build": "wp-scripts build",
"format:js": "wp-scripts format-js",
"lint:js": "wp-scripts lint-js",
"packages-update": "wp-scripts packages-update",
"start": "wp-scripts start"
}
}
```

## Setting up the build pipeline

This tutorial will proceed assuming the reader is familiar with ESNext syntax and the concept of build tools (like webpack). If that sounds confusing, you may want to review the [Getting started with JavaScript Build Setup](/how-to-guides/javascript/js-build-setup.md) first.

To install the build tool, navigate to the plugin directory using your terminal and run `npm install`.

Once all the dependencies are in place, all that's left is to run `npm start` and voila! A watcher will run in the terminal. You can then edit away in your text editor; after each save, it will automatically build.

## Testing if it worked

If you now go to the Plugins page, you should see a plugin called **My first Gutenberg App**. Go ahead and activate it. A new menu item labeled _My first Gutenberg app_ should show up. Once you click it, you will see a page that says _Hello from JavaScript!_:

![](./media/setup/hello-from-js.jpg)

Congratulations! You are now ready to start building the app!

## What's next?

* Previous part: [Introduction](./README.md)
* Next part: [Building a basic list of pages](./2-building-a-list-of-pages.md)
* (optional) Review the [finished app](https://github.com/WordPress/gutenberg-examples/tree/trunk/09-code-data-basics-esnext) in the gutenberg-examples repository
Loading