Skip to main content

Settings rows

Every setting of Poly Community is a row on a page of Settings, in a card, with its owner: the core, the Community component, or a component by its slug. A row shows, saves and is found by the search of Settings only while its owner runs, so the settings of a component that is switched off are hidden with it and kept for when it is on again.

A component adds its own on poly_community_register_settings_rows. The callback is given the registry, and adds a card with add_section() and each row with add(). Both are stable.

Settings and pages drawn from data​

Every page of Poly Community is one static file of its type for every reader, drawn by the browser from data (Pages drawn from data). A setting is the same for every reader, so reading one keeps a page that way: read it where the component registers what it draws, its blocks, its items and their who rules, as the starter reads its switch where it adds its menu item. Never read a setting together with a test of the reader to draw something for one reader in PHP. What depends on the reader is a fact the page shows its part by.

After a change, wp poly-community page-data check <page> on each page the setting changes says whether the page is still one drawing for every reader.

A card and a row​

The starter of Building a component adds one card with one switch to its page, features/example-recipes, and reads the switch where it draws its menu item:

public function boot() {
add_action( 'poly_community_register_settings_rows', array( $this, 'settings' ) );
add_action( 'poly_community_register_ui', array( $this, 'ui' ) );
}

// 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,
) );
}
Settings, Components, Recipes: its switch under On this site, then the card The member menu with the switch Add a recipe in the member menu
Settings → Components → Recipes, with the card the starter adds

$this->setting( 'memberMenu' ) then gives the value the owner saved, or the default until they save one. The plugin tests switch it off and check that the item leaves the menu (tests/specs/component-guide.spec.ts).

The rows of a component on its own page are kept with the component. They are saved with the rest of Settings, so a row needs no code to save it, and the value is cleaned by its type before it is kept.

A card​

KeyWhat
idIts id, prefixed with your component or plugin. Rows name it in their section.
pageThe page it is on: features/{slug} for the page of a component under Settings → Components.
ownerThe slug of the component, so the card shows while it is on.
title, descriptionIts heading and the line under it.
afterThe id of the card it follows on that page. Without it, it goes last.

A row​

KeyWhat
keyIts name, which $this->setting() reads, each key once in a component.
page, sectionIts page and its card.
ownerThe slug of the component.
typeHow it is set, below. A switch when left out.
label, descriptionIts words, and the line under them.
defaultIts value until the owner saves one.
optionsFor a choice: a list of array( 'value' => …, 'label' => … ).
min, max, step, unitFor a number: its bounds, its step (a step of 0.01 keeps two decimals) and the unit shown after it. For words, max is how many characters they keep (200 when left out).
placeholderWhat an empty field shows.
whenThe other rows of its page that must hold a value for this one to show: array( 'memberMenu' => true ) shows it only while that switch is on, and a list of values allows any of them.
sanitizeA named function given the cleaned value and the value sent, answering the value to keep, for a rule of your own on top of its type.

The types of row​

typeShowsIts value
toggleA switchtrue or false
numberA number fieldA whole number, or one with the decimals of its step, held between min and max
textA line of wordsThe words, as plain text, at most max characters
selectA dropdown of its optionsThe value chosen, or its default when the value is none of them
choiceA radio button for each of its optionsThe same as select
chipsA button for each of its options, any of them pressedThe list of values chosen
ticksA tick box for each of its optionsThe list of the options set off, so an option added later is on
whocanWho may do something, as the other "who can" rows of Poly CommunityA rule of roles and member types

The hook after a save​

poly_community_kit_settings_saved runs after a save that changed the settings of one or more components, with the list of their slugs. A component that keeps something worked out from its settings forgets it there.

See also Building a component and What you can build.