Building a component
A component is a folder with a manifest, kit.json, and a class in kit.php. Its pages, menu items, tabs on profiles and spaces, kinds of item, tables, blocks and settings are all declared there. Switched off, the whole component is gone from the site, and its data waits in its tables until it is switched on again. The components of Poly Community itself are made this way, in kits/ of the plugin, and a plugin adds its own the same way.
The example here is a small one, Recipes: a page of its own, an item in the community menu, a tab on every profile, an item in the menu under the picture of each member, and a setting on its own page under Settings. From there, the last section says where each next step is.
Download the starter plugin (Recipes for Poly Community, a zip to upload under Plugins → Add New Plugin → Upload Plugin). The code on this page is all of it. The plugin tests install it as a site owner would and check each step of this page on it (examples/poly-community-recipes in the plugin, checked by tests/specs/component-guide.spec.ts).
Drawn from data, from the start
Every page of Poly Community is one static file of its type, the same for every reader, and one JSON answer a move asks for. The browser draws the page from the two, which is what keeps a site fast as it grows. A component keeps it page by page:
- What every reader sees is blocks, in its templates, the same in the file for everyone.
- What one reader sees or may do comes as data with the move, a part or a fact, never as HTML the server draws for that reader and never as a test of the reader in PHP. Pages drawn from data has the rules, the API and a recipe.
wp poly-community page-data check <page>says for a visitor, a member and an administrator whether the page is one drawing, or the first reason it is not and what to change (Checking a page).
Recipes is drawn from data as it is: its page and its tab are blocks, and its menu items are items of the registry with a who rule. The plugin tests check its page and a profile with its tab:
wp poly-community page-data check kit-example-recipes # kit-example-recipes: one drawing for every reader
wp poly-community page-data check profile # profile: one drawing for every reader
Run the check on every page you add or change, as each step below does.
1. The plugin
A plugin adds its component on poly_community_register_kits, by the folder that holds it:
<?php
/**
* Plugin Name: Recipes for Poly Community
* Requires Plugins: poly-community
* Text Domain: poly-community-recipes
*/
defined( 'ABSPATH' ) || exit;
// The component is the folder beside this file: Poly Community reads its kit.json and runs its kit.php while it is on.
function poly_community_recipes_register( $kits ) {
$kits->add( __DIR__ . '/example-recipes' );
}
add_action( 'poly_community_register_kits', 'poly_community_recipes_register' );
Requires Plugins makes WordPress ask for Poly Community first. Every hook and callback of a component is a named function or a public method of its class (array( $this, 'ui' )). Poly Community calls it from outside the class, an error names it, and another plugin can take a hook off again with remove_action() only when its callback has a name.
The folder ends up like this:
poly-community-recipes/
poly-community-recipes.php the plugin
example-recipes/
kit.json the manifest
kit.php its class, extending Poly_Community_Kit
templates/example-recipes.html its page, as blocks
templates/profile-recipes.html its tab on every profile, as blocks
A bigger component adds schema.php (its CREATE TABLE statements), blocks/<name>/block.json (one folder a block), views/*.php (printed with $this->view() and $this->the_view()) and templates/card-<name>.html (the default blocks of each of its cards).
2. The manifest
{
"slug": "example-recipes",
"title": "Recipes",
"description": "A page of recipes, an item in the community menu and a Recipes tab on every profile.",
"summary": "A page of recipes",
"icon": "book",
"group": "content",
"defaultOn": true,
"textDomain": "poly-community-recipes",
"needs": { "kits": [], "foundations": [], "sdk": [], "plugins": [], "cloud": false },
"views": {
"example-recipes": { "path": "recipes", "width": "list", "title": "Recipes" }
},
"nav": [ { "view": "example-recipes", "label": "Recipes", "icon": "book", "after": "members" } ],
"tabs": {
"profile": [ { "slug": "recipes", "label": "Recipes", "after": "about" } ],
"space": []
}
}
slugnames the component everywhere.title,description,summary,iconandgroupare how it shows under Settings → Components:groupiscontent,people,money,spacesorsafety, andicona name from Icons.defaultOnswitches it on once the plugin is active. Without it, the owner switches it on.textDomainis the text domain the words of the manifest are translated with: the title, the description and the labels of its pages, menu items and tabs. Without it they are looked up in that of Poly Community.needsnames the components that come on with it, the parts of Poly Community and of the WordPlus SDK it uses, and the plugins it works with.viewsare its pages under the community home.pathmay hold{id}(an item of the kinditemnames, checked to be visible) or{name}.widthis how wide the page is drawn (list,feedand the others its views use).navputs its pages in the community menu, here after Members.tabsadds a tab to every profile or space (A tab from a component).kinds,spaceTypes,notificationsandtreeslist what goes away with the component when it is off, andtablesthe tables it keeps.actionsnames its own actions, each held back like one of the community (post,reply,react,join,upload), so approval, profile requirements and verification hold them too.
3. Its pages and its tab
A page of a component is a block template, templates/{view}.html. <!-- poly-community:page-head --> puts the head of the page there, its title from the manifest:
<!-- poly-community:page-head -->
<!-- wp:paragraph {"className":"example-recipes-intro"} -->
<p class="example-recipes-intro">Recipes the members of this community cook and share.</p>
<!-- /wp:paragraph -->
A tab on every profile is a template of its own, templates/profile-{slug}.html (space-{slug}.html for a space):
<!-- wp:paragraph {"className":"example-recipes-tab"} -->
<p class="example-recipes-tab">The recipes of this member show here.</p>
<!-- /wp:paragraph -->
Any block goes in them: those of WordPress, of Poly Community, or of your plugin. With a block theme, the site owner can change them in Appearance → Editor like any other page of the community. Pages of core blocks are kept as page files too, so a move to the page from anywhere in the community draws it with no page asked of the server.
4. The class
The class is named after the slug (Poly_Community_Kit_ and the slug in words), or named by class in the manifest. boot() adds its hooks once a request, only while the component is on. Here it adds a setting of its own and puts one item in the member menu, under the picture of the member, through the registry of buttons, menus and tabs:
final class Poly_Community_Kit_Example_Recipes extends Poly_Community_Kit {
public function boot() {
add_action( 'poly_community_register_settings_rows', array( $this, 'settings' ) );
add_action( 'poly_community_register_ui', array( $this, 'ui' ) );
}
// An item in the member menu, linking to the page of the component. A visitor sees the items of that menu as links
// beside Log in, so it says it is for members.
public function ui( $ui ) {
if ( ! $this->setting( 'memberMenu' ) ) {
return;
}
$ui->add( 'account', 'example-recipes-add', array(
'label' => __( 'Add a recipe', 'poly-community-recipes' ),
'icon' => 'book',
'url' => Poly_Community()->kits->view_url( 'example-recipes', 'example-recipes' ),
'who' => 'members',
) );
}
}
boot() runs before init, so anything that translates a text or reads a setting goes in a hook that runs later, as ui() does here, or in after_init(). Every component gets setting(), view(), the_view(), register_job() and item_of() from Poly_Community_Kit.
The address of the page comes from Poly_Community()->kits->view_url(). A site may run the community at its root, and the owner may give a page another word under Settings → Pages and addresses, so an address written by hand breaks. Who sees the item is its who rule, which Poly Community settles for each reader, as a page is kept as a file for every reader of a kind (Who sees it). A test of the reader in PHP would draw the file for the first reader only.
5. Its settings
A component keeps its settings on its own page under Settings → Components, features/{slug}. It adds them on poly_community_register_settings_rows, a card with add_section() and its rows with add(), and reads each with $this->setting( $key ), which gives the default until the owner saves another:
// Its settings: a card on its own page under Settings, Components, whose values $this->setting() reads.
public function settings( $rows ) {
$rows->add_section( array(
'id' => 'example-recipes-menu',
'page' => 'features/example-recipes',
'owner' => 'example-recipes',
'title' => __( 'The member menu', 'poly-community-recipes' ),
) );
$rows->add( array(
'key' => 'memberMenu',
'page' => 'features/example-recipes',
'section' => 'example-recipes-menu',
'owner' => 'example-recipes',
'type' => 'toggle',
'label' => __( 'Add a recipe in the member menu', 'poly-community-recipes' ),
'description' => __( 'An item under the picture of each member that opens the page of recipes.', 'poly-community-recipes' ),
'default' => true,
) );
}

owner is the slug of the component, so the row shows and saves only while the component is on, and its value is kept with the component. Settings rows has every type of row and how to put one on another page of Settings.
6. Trying it
Activate the plugin on a copy of your site. Recipes is then under Settings → Components with the others, switched on, with its switch and its card of settings on its page there. On the site:
- Recipes is in the community menu after Members, and its page answers at
/community/recipes/(or wherever the community and the word for the page are). - Every profile has a Recipes tab after About.
- A member finds Add a recipe in the menu under their picture. A visitor does not see it among the links beside Log in. With Add a recipe in the member menu switched off on its page, the item is gone from the menu.
- Switched off under Settings → Components, its page answers 404, and its menu item and tab are gone.
The same with WP-CLI:
wp plugin activate poly-community-recipes
wp eval 'echo Poly_Community()->kits->view_url( "example-recipes", "example-recipes" );'
curl -s -o /dev/null -w '%{http_code}\n' "$(wp eval 'echo Poly_Community()->kits->view_url( "example-recipes", "example-recipes" );')"
The first wp eval prints the address of its page, and curl answers 200 while the component is on, 404 once it is off. Then the check of Drawn from data, from the start on its page and on a profile.
Where to go from here
| Next | Where |
|---|---|
| A kind of item with fields of its own, as events and jobs have | Kinds of item |
| What one reader sees, as a part or a fact | Pages drawn from data |
| Its own table and blocks, drawn from data and updated live | Building for instant pages and live updates, with the Rooms example plugin |
| More tabs, buttons and menu items, for some readers alone | Tabs and menus, Buttons and menus |
| Who may do what | What you can build, item 3 |
| How wide its pages are drawn and the widgets beside them | Templates and themes |
| Its words in other languages | Translating |
| Changing a component that is there | Changing a component |
A block of a component is a WordPress block: a block.json with "supports": { "interactivity": true }, its markup drawn with the directives of the Interactivity API, and its script a script module. See Built on the Interactivity API.
See also What you can build and the Reference.