Skip to main content

Changing a component

A component of Poly Community (Events, Courses, Jobs, or one a plugin adds) can be changed in four ways. Take the first that does what you need: each one after it is more of your own to keep up when Poly Community changes.

WayWhat it changesThrough updates
In the adminWhat it does, its fields, its words (Settings → Words), its lookKept, nothing to do
In the Site EditorWhich blocks its pages and cards hold, and whereKept by WordPress, the block names are stable
A view in your themeThe markup of one viewSettings says when the view you copied changes
Hooks in a pluginValues, parts of cards, sorts, rulesKept when on the stable list

Whichever you choose, a page keeps moving without a reload and stays current: each way below says what it takes. It is the WordPress Interactivity API all through.

In the admin​

  • On or off: Settings → Components, one switch each.
  • What it does: its own page under Settings → Components, with its settings.
  • Its fields: the fields of its kinds of item, added and ordered under Settings → Components → Forms and fields, drawn on its forms and cards.
  • Its words: any of its texts in words of the site own under Settings → Words, with the choices of words a site makes (what spaces and friends are called).
  • Its look: Settings → Appearance, the design tokens every component draws with.

In the Site Editor​

Every page of the community is a block template, the pages of the components among them ("Community: Events"), and every card a component declares for its lists (cards in its kit.json) is a template part (community-{name}). With a block theme, open them in Appearance → Editor: move, add and remove blocks, or put the blocks of a component on another page. WordPress keeps what you save, and block names are on the stable list.

Page files follow: the templates and parts the Site Editor saved are part of their key, so a change makes them again.

A view in your theme​

The markup inside a block comes from a view: a PHP file of the community (views/members.php) or of a component (kits/events/views/card.php). A copy of it in your theme, under poly-community/ and laid out as the plugin lays out its own, is drawn in its place:

wp-content/themes/your-child-theme/poly-community/
views/members.php in place of the community view members
views/parts/community-footer.php in place of the community view parts/community-footer
kits/events/views/card.php in place of the view card of Events, the event under a post in the feed
kits/example-rooms/views/room.php in place of the view room of a component a plugin adds

The child theme is looked at first, then its parent. Keep your copies in a child theme: an update of a theme replaces its folder, and the copies in it with it, so Settings and view copy warn when your theme is not a child theme. Make the copy with WP-CLI, which also stamps it:

wp poly-community view copy members
wp poly-community view copy card --component=events
wp poly-community view list

view copy writes the view into the child theme with one line added at its top, its stamp, and never writes over a copy that is there:

<?php
// @poly-community-view 1f0c2e9b7a44 copied from events/views/card.php. Keep this line: Settings → Advanced → Overrides says when that view changes.

The stamp is a hash of the view as it was copied. When an update changes that view, Settings → Advanced → Overrides says so, and wp poly-community view list shows it as older. Your copy keeps drawing as it is: compare it with the view as it is now, bring over what changed, and set its stamp to the one shown as the view is now.

Settings, Advanced, Overrides: two overrides, one up to date and one whose view changed since it was copied, with the notice and the command to make a copy
Settings → Advanced → Overrides

What a copy must keep​

A copy is drawn wherever the view is: on a whole load, in a page file, and for a row the browser draws after an event. So it follows the same rules as the view (Building for instant pages and live updates):

  • Draw from what it is given, $args, and the state the view used. Read nothing of the moment, and nothing of the reader: a page file is drawn once for every reader of a kind.
  • Show what differs by reader through a fact, as the view does (data-wp-bind--hidden="!state.reader.…"), never by a test in PHP.
  • Keep the directives and classes the view has. The scripts of Poly Community and of the component find their elements by them.
  • A row view draws the same for everyone, as an event takes it to every member.

Page files follow without a step of yours: every override file the theme holds is part of their key, so a copy added, changed or removed makes them again, and a move draws what a whole load does.

A copy made before its view was drawn from data keeps drawing as it was, and pages stay right with it. Where a page file used to leave something to the data of the move (the reply box under a post shown by whether the reader may reply, the summary of a space shown by its data), a copy of that view puts it back into what the file is drawn for, and readers or spaces of another kind move by HTML. Where the copy draws something of one space or one member, the two drawings of the file differ and the page moves by its HTML. Bring a copy up to date to get its pages back to their files: Settings → Advanced → Overrides says which views changed since you copied them.

An example​

Download the example child theme (a zip to upload under Appearance → Themes → Add New Theme → Upload Theme). It is a child theme of Twenty Twenty-Five with a copy of the view members, the members directory, and a line under it. Set Template in its style.css to the folder of your own theme. The tests of Poly Community run it and check that its stamp is the view as it is now.

A child theme has Site Editor templates of its own: the templates you saved for its parent are not used while it is active, so save again under it what you changed there.

The Rooms example plugin of Building for instant pages and live updates draws each room from its view room. To show a line under each name, copy the view and change the copy:

wp poly-community view copy room --component=example-rooms
<?php
// @poly-community-view 1dc7f4779599 copied from example-rooms/views/room.php. Keep this line: Settings → Advanced → Overrides says when that view changes.
defined( 'ABSPATH' ) || exit;

$room = $args['room'];
?>
<article class="example-room poly-card" data-example-room="<?php echo (int) $room->id; ?>">
<h3 class="example-room-name"><?php echo esc_html( (string) $room->name ); ?></h3>
<p class="example-room-note">Ask at the desk for the key.</p>
…the buttons of the view, kept as they are…
</article>

A view in a plugin​

A plugin changes a view through poly_community_view_file, which has the last word over the theme copy:

function my_plugin_view_file( $file, $name ) {
// A community view is named by its path under views/, a component view as kits/{folder}/{name}.
if ( 'kits/events/card' === $name ) {
return __DIR__ . '/views/event-card.php';
}
return $file;
}
add_filter( 'poly_community_view_file', 'my_plugin_view_file', 10, 2 );

The file gets the same $args as the view, and $kit for a component view. Page files count it once a page is drawn with it.

The file may draw the view itself around what it adds. The example plugin of Tabs and menus draws a line above the members directory this way:

function poly_community_tweaks_view_file( $file, $name ) {
return 'members' === $name ? __DIR__ . '/views/members.php' : $file;
}
add_filter( 'poly_community_view_file', 'poly_community_tweaks_view_file', 10, 2 );
<?php
// views/members.php of the plugin: its line, then the view of the community with the same $args.
defined( 'ABSPATH' ) || exit;
?>
<p class="tweaks-members-note">Say hello to a neighbour: everyone here lives within a walk of the river.</p>
<?php
include Poly_Community()->path . 'views/members.php';

Hooks in a plugin​

Most changes need no copy of a view at all. Each common one has its place in the core, for every component alike:

To changeUse
What a card showspoly_community_item_row_data adds values to the row of an item (under kit), and poly_community_card_part_html changes the HTML of each part of a card as it is drawn.
The fields of a formThe fields of a kind, under Settings → Components → Forms and fields, or in code with Poly_Community()->thing_fields.
How a list is sortedpoly_community_item_sorts adds sorts of your own to the lists of a kind: a label, and an ORDER BY on its rows.
The tabs of profiles and spaces, and the menusThe places profile.tabs, space.tabs, nav and account of the registry, on poly_community_register_ui, shown in Tabs and menus.
The buttons of a profile, a space and a post, and their ⋯ menusBlock Hooks for a button in a head, and the places profile.menu, space.menu, post.menu and post.actions of the registry, shown in Buttons and menus.
Who may do whatAccess rules (poly_community_register_access_rules), what a member must do first (poly_community_requirements), and the actions of a component, which approval, profile requirements and verification hold back.

The components have hooks of their own besides, each listed with where it runs in Hooks of the components.

Why not copy a component​

A component cannot run twice under two names by copying its folder. Its class, its blocks (poly-community/…), its kinds of item, its rows, lists and routes and its tables are all named, and a second component with any of those names is refused or clashes with the first. A copy that renames all of them is a fork of its own, with none of the updates of the component and none of its data.

Change it in one of the ways above, or add a component of your own beside it that does what it lacks, as Building a component shows: it can need the component (needs.kits in its kit.json), use its hooks, and put its blocks on its pages.

See also What you can build and Built on the Interactivity API.