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

Add a developer tutorial for displaying notices #13703

Merged
merged 6 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
77 changes: 77 additions & 0 deletions docs/designers-developers/developers/tutorials/notices/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Displaying Notices from Your Plugin or Theme

Notices are informational UI displayed near the top of admin pages. WordPress, themes, and plugins all use notices to indicate the result of an action, or to draw the user's attention to necessary information.
chrisvanpatten marked this conversation as resolved.
Show resolved Hide resolved

In the Classic Editor, notices hooked onto the `admin_notices` action can render whatever HTML they'd like. In the Block Editor, notices are restricted to a more formal API.

## Notices in the Classic Editor

In the Classic Editor, here's an example of the "Post draft updated" notice:

![Post draft updated in the Classic Editor](https://raw.githubusercontent.com/WordPress/gutenberg/master/docs/designers-developers/developers/tutorials/notices/classic-editor-notice.png)

Producing an equivalent "Post draft updated" notice would require code like this:

```php
/**
* Hook into the 'admin_notices' action to render
* a generic HTML notice.
*/
add_action( 'admin_notices', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So personally I have no issues with using closures inside actions (in fact I love them myself and almost hate that I'm commenting about this)… but I think, at least until the day core officially drops 5.2, this should be written with a full function name and all that nonsense.

Sigh.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FINGERS ALL THE WAY CROSSED

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to a function in 07e8a60

$screen = get_current_screen();
// Only render this notice in the post editor.
if ( ! $screen || 'post' !== $screen->base ) {
return;
}
// Render the notice's HTML.
// Each notice should be wrapped in a <div>
// with a 'notice' class.
echo '<div class="notice notice-success is-dismissible"><p>';
echo sprintf( __( 'Post draft updated. <a href="%s" target="_blank">Preview post</a>' ), get_preview_post_link() );
echo '</p></div>';
} );
```

Importantly, the `admin_notices` hook allows a developer to render whatever HTML they'd like. One advantage is that the developer has a great amount of flexibility. The key disadvantage is that arbitrary HTML makes future iterations on notices more difficult, if not possible, because the iterations need to accommodate for arbitrary HTML. This is why the Block Editor has a formal API.

## Notices in the Block Editor

In the Block Editor, here's an example of the "Post published" notice:

![Post published in the Block Editor](https://raw.githubusercontent.com/WordPress/gutenberg/master/docs/designers-developers/developers/tutorials/notices/block-editor-notice.png)

Producing an equivalent "Post published" notice would require code like this:

```js
( function( wp ) {
wp.data.dispatch('core/notices').createNotice(
'success', // Can be one of: success, info, warning, error.
'Post published.', // Text string to display.
{
isDismissible: true, // Whether the user can dismiss the notice.
// Any actions the user can perform.
actions: [
{
url: '#',
label: 'View post'
}
]
}
);
} )( window.wp );
```

You'll want to use this _Notices Data API_ when producing a notice from within the JavaScript application lifecycle.

To better understand the specific code example above:

* `wp` is WordPress global window variable.
* `wp.data` is an object provided by the Block Editor for accessing the Block Editor data store.
* `wp.data.dispatch('core/notices')` accesses functionality registered to the Block Editor data store by the Notices package.
* `createNotice()` is a function offered by the Notices package to register a new notice. The Block Editor reads from the notice data store in order to know which notices to display.

## Learn More

The Block Editor offers a complete API for generating notices. The official documentation is a great place to review what's possible.

For a full list of the available actions and selectors, refer to the [Notices Data Handbook](/docs//designers-developers/developers/data/data-core-notices/) page.
mkaz marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/designers-developers/developers/tutorials/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* See the [Meta Boxes Tutorial](/docs/designers-developers/developers/tutorials/metabox/readme.md) for new ways of extending the editor storing and using post meta data.

* Check out the [Notices Tutorial](/docs/designers-developers/developers/tutorials/notices/README.md) to learn how to display informational UI at the top of the editor.

* The [Sidebar Tutorial](/docs/designers-developers/developers/tutorials/sidebar-tutorial/plugin-sidebar-0.md) will walk you through the steps of creating a sidebar to update data from the `post_meta` table.

* Learn how to add customized buttons to the toolbar with the [Format API tutorial](/docs/designers-developers/developers/tutorials/format-api/).
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@
"markdown_source": "https://raw.githubusercontent.com/WordPress/gutenberg/master/docs/designers-developers/developers/tutorials/metabox/meta-block-5-finishing.md",
"parent": "metabox"
},
{
"title": "Displaying Notices from Your Plugin or Theme",
"slug": "notices",
"markdown_source": "https://raw.githubusercontent.com/WordPress/gutenberg/master/docs/designers-developers/developers/tutorials/notices/README.md",
"parent": "tutorials"
},
{
"title": "Creating a sidebar for your plugin",
"slug": "plugin-sidebar-0",
Expand Down
1 change: 1 addition & 0 deletions docs/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
{"docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md": []},
{"docs/designers-developers/developers/tutorials/metabox/meta-block-5-finishing.md": []}
]},
{"docs/designers-developers/developers/tutorials/notices/README.md": []},
{"docs/designers-developers/developers/tutorials/sidebar-tutorial/plugin-sidebar-0.md": [
{"docs/designers-developers/developers/tutorials/sidebar-tutorial/plugin-sidebar-1-up-and-running.md": []},
{"docs/designers-developers/developers/tutorials/sidebar-tutorial/plugin-sidebar-2-styles-and-controls.md": []},
Expand Down