Skip to main content

Buttons and menus

A profile and a space each have buttons in their head (Follow, Message, Join, Share) and a ⋯ menu beside them. A post has a ⋯ menu beside its author and a row of buttons under it (Like, Reply, Share). A plugin adds its own to each of them, moves those of Poly Community and takes them out.

Download the example plugin (Tweaks for Poly Community, the plugin of Tabs and menus). Everything on this page is in it, and the tests of Poly Community run it as a site owner installs it. In each picture, a red box marks the change it shows. Changes the example makes elsewhere on the same screen, such as its tabs, are left unboxed.

The head of a member profile with a Projects button, boxed in red, before its ⋯ menu
A Projects button added to the head of every profile

One rule for all of it​

A profile page and the cards of a list are drawn once and kept as a page file, which pages move to without a reload. A file serves every reader of a kind. So an item of a menu says who sees it with a rule, who, which Poly Community settles wherever the item is drawn, and a button of your own in a head checks its reader only as Who sees it says.

  • Name the member by their address. In an item, {url} is the address of the profile, the space or the post it is for, and {url:enc} the same address encoded for the query of a link. In a block of your own, Poly_Community()->routes->profile_url( $profile ). The file the member pages share holds those for each member in turn. A number of your own in an attribute or a query would carry the first member to all the others.
  • Never name the reader in a label. A page whose file names its reader is not kept as a file (many an administrator is called admin), and it moves by its HTML.

Each hook below takes a named function, never an anonymous function, so another plugin can take it off again with remove_action() or remove_filter().

A button in the head of a profile or a space​

The buttons of a head are blocks of its template:

HeadIts blocks, in their order
A profileedit-profile-button, follow-button, message-button, call-button (video, then audio), profile-menu (the ⋯)
A spacejoin-button, space-invite-button, space-manage-button, space-chat-button, share-button, space-menu (the ⋯)

Each is poly-community/ and its name. With a block theme, a site owner moves or removes them in Appearance → Editor, in the templates of the community.

A plugin adds a button as a block of its own, and puts it next to one of these with Block Hooks, blockHooks in its block.json. It lands there under a block theme and under a classic theme alike:

{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "poly-community-tweaks/projects-button",
"title": "Projects button",
"category": "widgets",
"supports": { "html": false },
"blockHooks": { "poly-community/profile-menu": "before" },
"textdomain": "poly-community-tweaks",
"render": "file:./render.php"
}
<?php
// render.php: the Projects tab of the member shown, by the address of their profile.
defined( 'ABSPATH' ) || exit;

$poly_tweaks_profile = Poly_Community()->routes->current()['profile'] ?? null;
if ( ! $poly_tweaks_profile ) {
return;
}
?>
<a class="poly-btn poly-btn-secondary tweaks-projects-button" href="<?php echo esc_url( Poly_Community()->routes->profile_url( $poly_tweaks_profile, 'projects' ) ); ?>"><?php esc_html_e( 'Projects', 'poly-community-tweaks' ); ?></a>
function poly_community_tweaks_blocks() {
register_block_type( __DIR__ . '/blocks/projects-button' );
}
add_action( 'init', 'poly_community_tweaks_blocks' );

Poly_Community()->routes->current() gives the page shown, its profile or its space. The classes poly-btn and poly-btn-secondary draw it as the buttons beside it.

List it for page files. A block another plugin draws with its own code keeps a page out of its file, and the page moves by its HTML, still without a reload but slower. A block that draws only from the profile or the space shown, the same for every reader, says so:

function poly_community_tweaks_move_blocks( $blocks ) {
$blocks[] = 'poly-community-tweaks/projects-button';
return $blocks;
}
add_filter( 'poly_community_json_move_blocks', 'poly_community_tweaks_move_blocks' );

Take one out by drawing its block as nothing:

// The Share button of a space head, whose link the ⋯ menu copies instead.
function poly_community_tweaks_no_share_button( $html, $block ) {
return '';
}
add_filter( 'render_block_poly-community/share-button', 'poly_community_tweaks_no_share_button', 10, 2 );

An item in a ⋯ menu​

Every ⋯ menu and every row of buttons is a place of one registry. A plugin adds its items there, and moves or takes out those of Poly Community, on poly_community_register_ui:

PlaceWhereIds of Poly Community, in their order
profile.menuThe ⋯ of a profilecopy-link, report, mute, block, and under Moderation (the group moderation) suspend, edit-member, wordpress-user
space.menuThe ⋯ of a spaceinvite, members, delete, leave, each where its reader may
post.menuThe ⋯ of a postedit, pin, save, hide-pin, the items of the components (below), copy-link, mute, report, hold, delete
post.actionsThe buttons under a postlike, quote, reply-to, reply, share, solution, reactions (who reacted), views. quote, reply-to and solution are in a topic of the forum.
// Projects after Copy profile link, Report moved to the end, Mute taken out.
function poly_community_tweaks_profile_menu( $ui ) {
$ui->add( 'profile.menu', 'tweaks-projects', array(
'label' => __( 'Projects', 'poly-community-tweaks' ),
'icon' => 'grid',
'url' => '{url}projects/',
'after' => 'copy-link',
) );
$ui->change( 'profile.menu', 'report', array( 'last' => true ) );
$ui->remove( 'profile.menu', 'mute' );
}
add_action( 'poly_community_register_ui', 'poly_community_tweaks_profile_menu' );
The ⋯ menu of a profile: Copy profile link, Projects, Block, Report, with Projects and Report boxed in red
The ⋯ of a profile: Projects added, Report moved to the end, Mute taken out
MethodWhat it does
$ui->add( $place, $id, $item )Adds an item. Prefix its id with your plugin, as hooks are. An id there already is refused: change it instead.
$ui->change( $place, $id, $keys )Changes keys of an item, yours or one of another plugin. Of an item of Poly Community: where it stands, its group, and who, which can hide it from more readers and never shows it to readers Poly Community leaves out.
$ui->remove( $place, $id )Takes an item out.
$ui->get( $place, $id )The item as registered, or null.

The components of Poly Community register theirs first, so a plugin at the default priority finds every item. A key an item does not have, an id there already or an item with no label is refused with a notice.

An item says what it is:

KeyWhat
labelIts words.
iconA name from Icons (link, grid, external and the others), a name of your own, or a whole SVG (Icons below).
urlA link, which may hold the address of the profile, space or post it is for, {url}, and its {id}, {handle} (a profile) or {slug} (a space), each with :enc for the query of a link ({url:enc}).
forA named function given the profile, space or post, where those are not enough: it answers what the item is for it, an array of label, url, and data values for the names the item has.
target_blank opens the link in a new tab.
actionAn Interactivity API action of your own store run on a press, my-plugin::actions.open. The item says what it is for in data-poly-subject (profile:12, space:4, item:310), data-poly-place and data-poly-ui (its id), and a menu closes as it is pressed.
commandA command of Poly Community by its name: copy-link copies the url of the item and says so.
dataIts data- attributes, name ⇒ a string, which may hold the same as url.
first, last, after, beforeWhere it stands: first, last, or next to another by its id.
groupmoderation puts it under Moderation in the ⋯ of a profile.
whoWho sees it (Who sees it). Everyone when left out.
whenA named function given the profile, space or post: whether the item is there for it (a post with a price, a space of a type). It reads that alone, never the reader.
dangerDrawn in red, for what cannot be undone.

Where an item stands is worked out once for every reader, before any item is left out, so it stays next to the one it names even where that one is not shown. first and last work as if each item were put there in turn: a first added later stands before one added earlier. An id that is not there (its plugin turned off) counts as none given.

The example copies the address of a space with a command of Poly Community, first in the menu, with an icon of its own (Icons):

function poly_community_tweaks_space_menu( $ui ) {
$ui->add( 'space.menu', 'tweaks-copy-link', array(
'label' => __( 'Copy space link', 'poly-community-tweaks' ),
'icon' => poly_community_tweaks_link_svg(),
'command' => 'copy-link',
'url' => '{url}',
'first' => true,
) );
}
add_action( 'poly_community_register_ui', 'poly_community_tweaks_space_menu' );

The items the components add to the ⋯ of a post stand after hide-pin, each by its id: life (Renew and End now), feature, close (a question), tip, live-end, writer (a post of an organization), album-edit and rating. Move or take out any of them as one of Poly Community.

Icons​

The icon of an item or of a button under a post is one of three, each drawn 18 px in the colour of its words:

A name of Poly Community. Icons shows every one with its name. Most come from the icons file every page keeps, so they cost a page nothing.

A name of your own, added on poly_community_register_icons with $icons->add(): the shapes of an icon on a grid of 24 by 24, drawn as lines, with a label and a group of the icon pickers of Settings. A shape drawn filled takes fill="currentColor". It joins the icons file, and every item and button takes it by its name. A whole SVG may have a name too:

function poly_community_tweaks_icons( $icons ) {
$icons->add( 'tweaks-notes', '<rect x="5" y="3.5" width="14" height="17" rx="2"/><path d="M8.5 8.5h7M8.5 12h7M8.5 15.5h4"/>', array(
'label' => __( 'Notes', 'poly-community-tweaks' ),
'group' => 'work',
) );
$icons->add( 'tweaks-link', poly_community_tweaks_link_svg(), array( 'label' => __( 'Link', 'poly-community-tweaks' ) ) );
}
add_action( 'poly_community_register_icons', 'poly_community_tweaks_icons' );

A whole SVG, as an icon set gives it, any viewBox. It is drawn in place, its shapes kept (path, circle, ellipse, rect, line, polyline, polygon and g) and anything else taken out, such as a script, a style or a link. It fills in the colour of the words unless it says otherwise:

function poly_community_tweaks_link_svg() {
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round">'
. '<path d="M6.6 9.4a2.8 2.8 0 0 0 4 0l2.4-2.4a2.8 2.8 0 0 0-4-4l-.9.9"/><path d="M9.6 6.6a2.8 2.8 0 0 0-4 0L3 9a2.8 2.8 0 0 0 4 4l.9-.9"/></svg>';
}

A name of your own is the lighter choice for an icon shown on many items at once, a whole SVG the quicker one for an icon from another set. A name there is none of draws no icon. In a block of your own, such as a button in a head, write the SVG in its markup, or print a name with poly_the_icon( 'tweaks-notes', 18 ). Icons says what a name of your own may hold.

The ⋯ of a post, and the buttons under it​

A post is drawn by itself on its page, and from its row in the cards of a list (the feed, a space, a profile), where the menu and the buttons are a template drawn once for every post of the page. One registration serves both. A url with {url}, and what for and when answer, are worked out for each post on the server and travel with its row. who is settled for each reader in the browser.

// Copy link first, then Open in a new tab, for members alone.
function poly_community_tweaks_post_menu( $ui ) {
$ui->change( 'post.menu', 'copy-link', array( 'first' => true ) );
$ui->add( 'post.menu', 'tweaks-open', array(
'label' => __( 'Open in a new tab', 'poly-community-tweaks' ),
'icon' => 'external',
'url' => '{url}',
'target' => '_blank',
'who' => 'members',
'after' => 'copy-link',
) );
}
add_action( 'poly_community_register_ui', 'poly_community_tweaks_post_menu' );
The ⋯ menu of a post: Copy link and Open in a new tab boxed in red, then Edit post, the pins, Save, Turn off notifications and Delete post
The ⋯ of a post in a card: Copy link moved to the top, Open in a new tab added for members

A function given the post (for, when) reads the post alone. What it answers is the same for every reader, so it travels with the row to all of them.

The buttons under a post are the place post.actions, and an item there is drawn as the buttons beside it:

// Share before Reply, an Open button after Reply, and the views taken out.
function poly_community_tweaks_post_actions( $ui ) {
$ui->change( 'post.actions', 'share', array( 'before' => 'reply' ) );
$ui->remove( 'post.actions', 'views' );
$ui->add( 'post.actions', 'tweaks-open', array(
'label' => __( 'Open', 'poly-community-tweaks' ),
'icon' => 'external',
'url' => '{url}',
'after' => 'reply',
) );
}
add_action( 'poly_community_register_ui', 'poly_community_tweaks_post_actions' );
A post in the feed with Like, Share, Reply and Open under it, Share and Open boxed in red
The buttons under a post: Share moved before Reply, Open added, the views taken out

Who sees it​

who says who sees an item, and Poly Community settles it wherever the item is drawn: in PHP for a page drawn for its reader, when a page file is made for what all its readers answer alike, and in the browser, from the facts of the reader and the row of the post, for the rest. So an item holds in every page file and every list without a check of your own.

whoWho sees it
everyoneEveryone, as when it is left out.
visitorsThose not logged in.
membersThose logged in.
ownThe member on their own profile or their own post, and no one else.
type:{slug}The members of a member type, as Menus saves it.
A rule, array( 'roles' => array( 'moderator', 'staff' ) )The rule the "Who may" settings of Poly Community save, below.

A rule lets the reader in by any of these:

KeyLets in
rolesMembers with any of the roles: member (any member), moderator (the team of the community), staff (its staff), admin. With no key of this table given, roles is member.
spaceRolesThe admin or moderator of the space the post is in.
owntrue: the member on their own profile or post.
canWho has a right on it, below.

Then it must meet each restriction it names: trust (a trust level at least, while the Trust levels component runs), verified (identity, age), and the parts the components add, memberTypes (slugs) and plans (ids). A part whose component is off lets no one through. An administrator passes any rule written this way. The word own alone lets in no one else, so an item with 'who' => 'own' is the member alone, and array( 'own' => true ) the member and the administrators.

can names a right on the post or the profile the item is for, the check its route runs:

canOnWho has it
edita postIts author within the edit window, an administrator
deletea postIts author, whoever may act as its profile, an administrator, the team of its space
holda postThe team of its space, on a shown post another wrote
reporta postA member, on what is neither theirs nor theirs to keep in order
moderatea postAn administrator, or the team of its space on a post another wrote
moderatea profileAn administrator, on a member who is not one

A right on a post belongs to each reader alone, so a post page that holds an item with one is drawn for its reader rather than kept as a file: never wrong, a little slower. In the cards of a list the browser settles it from the row.

An item for the team of the community, under Moderation in the ⋯ of a profile:

function poly_community_tweaks_team_item( $ui ) {
$ui->add( 'profile.menu', 'tweaks-notes', array(
'label' => __( 'Notes on this member', 'poly-community-tweaks' ),
'icon' => 'tweaks-notes',
'url' => home_url( '/team-notes/?member={url:enc}' ),
'who' => array( 'roles' => array( 'moderator', 'staff' ) ),
'group' => 'moderation',
) );
}
add_action( 'poly_community_register_ui', 'poly_community_tweaks_team_item' );
The ⋯ menu of a profile as a moderator sees it, with Notes on this member under Moderation boxed in red
The ⋯ of a profile for the team: Notes on this member added under Moderation

An item settled in the browser is in the page of every reader of the page file or the list, hidden for those its rule leaves out. Its words, its link and its data must hold nothing for some readers alone: a secret belongs on the page the item opens.

Showing is not a permission. An item only shows. What it opens or does checks again on the server: a page of yours with current_user_can(), a REST route with its permission_callback.

A button of your own for some readers alone: Shown to​

A button in a head is a block of yours. Who sees it is its "Shown to": a block that asks for it in its block.json ("supports": { "poly-community/who": true }) takes the attribute polyWho, a who as an item takes it, which an owner sets in the editor, in the panel "Shown to". Poly Community settles it wherever the block is drawn: for the reader of the page, and in the browser where a page file serves many readers. own and the roles of a space read the profile or the space of the page. It never shows a block where the block itself draws nothing. The header buttons of Poly Community take it too.

A button in the head of a profile for its member alone is a block put after Edit profile ("blockHooks": { "poly-community/edit-profile-button": "after" }), which gives polyWho the default own:

{
"name": "poly-community-tweaks/add-project-button",
"attributes": {
"polyWho": { "type": [ "string", "object" ], "default": "own" }
},
"supports": { "html": false, "poly-community/who": true },
"blockHooks": { "poly-community/edit-profile-button": "after" },
"render": "file:./render.php"
}

Its render.php draws from the profile shown, and leaves the reader to the rule:

<?php
defined( 'ABSPATH' ) || exit;

$poly_tweaks_profile = Poly_Community()->routes->current()['profile'] ?? null;
if ( ! $poly_tweaks_profile ) {
return;
}
?>
<a class="poly-btn poly-btn-secondary tweaks-add-project-button" href="<?php echo esc_url( Poly_Community()->routes->profile_url( $poly_tweaks_profile, 'projects' ) ); ?>"><?php esc_html_e( 'Add a project', 'poly-community-tweaks' ); ?></a>

List it for page files as the Projects button is: drawn as nothing in the file every other reader shares, it keeps their moves fast. A tab for the member alone is one registration with 'who' => 'own', its address not found for others (Tabs and menus).

A member on their own profile: Edit profile, then Add a project boxed in red, and a Drafts tab boxed in red
A member on their own profile: Add a project and a Drafts tab added, which no one else sees

Other buttons and menus​

The rest are places of the same registry, each with the ids of Poly Community in it:

PlaceWhereIts own ids
reply.menuThe ⋯ of a reply, on its own and over every comment of a treereport, hold, delete
reply.actionsThe buttons of a replylike, reply, edit, and of the components writer and votes
topic.menuThe ⋯ of a topic of the forum, its group moderation firstpin, lock, move, copy-link, mute, report, delete
space.memberThe ⋯ of a member in the member list of a space, for those who may act on them. Its subject is the member, in the space of the listroles, owner, remove, ban
nav.footThe buttons at the foot of the navigation column, each opening something on the page with its action and its datalive-chat (Live Support)

The actions poly_community_reply_actions, poly_community_comment_template, poly_community_space_member_menu and poly_community_space_person_menu of before still print, deprecated, for at least two releases.

Two are not places of the registry. Each takes what a plugin adds, and none moves or takes out what Poly Community draws there:

WhereHow a plugin adds to it
Join, in the head of a space and on its cardpoly_community_join_button( null, $space, $state, $card ) answers the HTML drawn in its place.
Message and the calls, in the head of a profileDrawn by the messaging plugin (Better Messages) through poly_community_messaging_message_button and poly_community_messaging_call_button.

In a head, any of these blocks is taken out with its render_block_ filter, as above, or shown to some readers alone with its Shown to.

What is stable​

The registry is on the stable list: poly_community_register_ui and its methods, the places and the ids of Poly Community in them, the keys of an item, the words and keys of who, the rights of can, the commands, the three attributes the browser reads, Shown to (poly-community/who, polyWho) and the icons (What is stable). A change keeps the old way working and is written in the changelog. Block Hooks are of WordPress, and the blocks of Poly Community keep their names.

See also Tabs and menus, Changing a component, Page moves and Built on the Interactivity API.