Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
34099d4
Getting Started with JavaScript
mkaz Dec 7, 2018
8ac0ca1
Reorder add_action/function
mkaz Dec 8, 2018
5b19984
Update prefix to use myguten, instead of just my
mkaz Dec 8, 2018
c3079b8
Update docs/designers-developers/developers/tutorials/javascript/read…
danielbachhuber Dec 8, 2018
5ad961d
Update to block_editor_assets
mkaz Dec 8, 2018
0901f98
Updates per danielbachhuber review.
mkaz Dec 8, 2018
7507870
Updates, and broken up into sections
mkaz Dec 9, 2018
cf6b13a
Add Troubleshooting
mkaz Dec 9, 2018
5041572
Updates with suggestion. Props to @nosolosw
mkaz Dec 11, 2018
6601f91
Update docs/designers-developers/developers/tutorials/javascript/vers…
nosolosw Dec 11, 2018
c45f603
Update docs/designers-developers/developers/tutorials/javascript/vers…
nosolosw Dec 11, 2018
bad714f
Add screenshot with style
mkaz Dec 11, 2018
264aa4f
Reorder TOC, move versions to end
mkaz Dec 11, 2018
a76276e
Update link for browser support
mkaz Dec 11, 2018
71f99fa
Add check for dependency to troubleshooting section
mkaz Dec 12, 2018
8f77bbc
General edits for spelling and flow
mkaz Dec 12, 2018
cf0bf1c
Update Table of Contents and generated manifest
mkaz Dec 14, 2018
b8b13e1
Fix up header tags consistency and spaces
mkaz Dec 14, 2018
01757a7
Edits per @chrisvanpatten review
mkaz Dec 14, 2018
45d962b
Update docs/designers-developers/developers/tutorials/javascript/exte…
chrisvanpatten Dec 14, 2018
bdcb766
Update docs/designers-developers/developers/tutorials/javascript/load…
chrisvanpatten Dec 14, 2018
3e54778
Update docs/designers-developers/developers/tutorials/javascript/load…
chrisvanpatten Dec 14, 2018
481ed44
Update docs/designers-developers/developers/tutorials/javascript/load…
chrisvanpatten Dec 14, 2018
e3aa456
Update docs/designers-developers/developers/tutorials/javascript/plug…
chrisvanpatten Dec 14, 2018
94132aa
Update docs/designers-developers/developers/tutorials/javascript/plug…
chrisvanpatten Dec 14, 2018
867c96e
Move screenshots to assets directory, add additional screenshot for s…
mkaz Dec 14, 2018
7f01078
Move block style variant to assets
mkaz Dec 14, 2018
be107c7
Formating
mkaz Dec 14, 2018
28f6e25
Update images to full URLs
mkaz Dec 14, 2018
081f6c4
Fix titles
mkaz Dec 15, 2018
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
Prev Previous commit
Next Next commit
Updates with suggestion. Props to @nosolosw
  • Loading branch information
mkaz committed Dec 14, 2018
commit 50415722b263df09b570934b205ff9606a75362e
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

This puts all the initial pieces in place for you to start extending the Block Editor.

Let's look at using the [Block Style Variation example](../../../../../docs/designers-developers/developers/filters/block-filters/#block-style-variations).
Let's look at using the [Block Style Variation example](../../../../../docs/designers-developers/developers/filters/block-filters.md#block-style-variations).

Replace the existing `console.log()` code in your `myguten.js` file with:

Expand All @@ -14,22 +14,47 @@ wp.blocks.registerBlockStyle( 'core/quote', {
} );
```

After you add the `wp.blocks.registerBlockStyle` code, save the `myguten.js` file, and then create a new post in the Block Editor.
**Important:** Notice that you are using a function from `wp.blocks` package. This means you must specify it as a dependency when you enqueue the script. Update the `myguten-plugin.php` file to:

Add a quote block, and in the right sidebar under Styles, you will see your new Fancy Quote style listed. You can go back to the JavaScript and change the label to "Fancy Pants" and reload, and you will see the new label.
```php
<?php
/*
Plugin Name: Fancy Quote
*/

function myguten_enqueue() {
wp_enqueue_script( 'myguten-script',
plugins_url( 'myguten.js', __FILE__ ),
array( 'wp-blocks')
);
}
add_action( 'enqueue_block_editor_assets', 'myguten_enqueue' );
```

The last argument in the `wp_enqueue_script()` function is an array of dependencies. WordPress makes available its packages under the wp namespace. In the example, you use `wp.blocks` to access the items the blocks package exports, in this case the `registerBlockStyle()` function.

See [Packages](../../../../../docs/designers-developers/developers/packages.md) for list of available packages and what objects they export.

Previewing or Publishing the post, you will not see a visible change. However, if you look at the source, you will see `is-style-fancy-quote` class name attached to your quote.
After you have updated both JavaScript and PHP file, go to the Block Editor and create a new post.

Go ahead and create a `style.css` file with:
Add a quote block, and in the right sidebar under Styles, you will see your new Fancy Quote style listed. You can go back to the JavaScript and change the label to "Fancy Pants" and reload, and you will see the new label. Click to select and apply that style to your quote block.


![Fancy Quote Style in Inspector](../../../../../docs/designers-developers/developers/tutorials/javascript/fancy-quote-in-inspector.png)


If you Preview or Publish the post, you will not see a visible change. However, if you look at the source, you will see the `is-style-fancy-quote` class name attached to your quote.

Let's add some style. Go ahead and create a `style.css` file with:

```css
.is-style-fancy-quote {
font-size: 64px;
color: tomato;
}

```

and enqueue the CSS by adding the following to your `myguten-plugin.php`:
You enqueue the CSS by adding the following to your `myguten-plugin.php`:

```php
function myguten_stylesheet() {
Expand All @@ -38,4 +63,5 @@ function myguten_stylesheet() {
add_action( 'enqueue_block_assets', 'myguten_stylesheet' );
```

And then when you view the page, you will see it in a very large font.
Now a quote block with your fancy quote style applied, when you viewed in the editor and published, you will see it in a beautiful tomato color.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ Add the following code to your `myguten-plugin.php` file:
```php
function myguten_enqueue() {
wp_enqueue_script( 'myguten-script',
plugins_url( 'myguten.js', __FILE__ ),
array( 'wp-blocks', 'wp-element' )
plugins_url( 'myguten.js', __FILE__ )
);
}
add_action( 'enqueue_block_editor_assets', 'myguten_enqueue' );
```

The script enqueuing uses the `enqueue_block_editor_assets` hook which loads the JavaScript file `myguten.js` when the block editor loads.

Create a file called `myguten.js` and add:

```js
Expand All @@ -23,18 +24,13 @@ console.log( "I'm loaded!" );

Now start a new post in the block editor, and check your browser Developer Tools. You should see the message in your console log. See [Mozilla's What are browser developer tools?](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_are_browser_developer_tools) if you need more information, the area to become most familiar with is [The JavaScript console](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_are_browser_developer_tools#The_JavaScript_console).

The script enqueuing uses the `enqueue_block_editor_assets` hook which only loads the JavaScript when the block editor loads. So if you navigate to any front-end page or article on your site, you will not see the message.

The last argument in the `wp_enqueue_script()` function is an array of dependencies. All of the Gutenberg packages are registered and can be loaded by specifying them in the array, the blocks and elements packages are shown above, as they are two common ones used.


**Note for Theme Developers:** The above method of enqueing is used for plugins, if you are extending Gutenberg for your theme there is a minor difference, you will use the `get_template_directory_uri()` function instead of `plugins_url()`. So for a theme, the enqueue example is:

```php
function myguten_enqueue() {
wp_enqueue_script( 'myguten-script',
get_template_directory_uri() . '/myguten.js',
array( 'wp-blocks', 'wp-element' )
get_template_directory_uri() . '/myguten.js'
);
}
add_action( 'enqueue_block_editor_assets', 'myguten_enqueue' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Inside of this new directory, create a file called `myguten-plugin.php` which wi
```php
<?php
/*
Plugin Name: My Guten Plugin
Plugin Name: Fancy Quote
*/
```

Expand Down