Skip to content

Data sources

By the end of this page your app lists data from your own site — a directory in a custom table, partner perks, anything a Memberside feature does not already show — in a grid or list you design, with a detail screen for each item.

A data source is a few lines of PHP on your site that say what an item looks like and how to list them. The canvas reads it with two blocks: Data Loop lists items, and Data Field shows one value of an item. Your site checks who may read the data on every request, so nothing leaves it that the source does not allow.

You need the Memberside plugin on your site, version 2.131.0 or later, and a developer who can add PHP to a theme’s functions.php or a small plugin.

Add this to your theme’s functions.php, or to a plugin of your own. It registers a source called my_listings with three fields:

add_action( 'memberside_data_sources', function ( $sources ) {
$sources->register( 'my_listings', array(
'schema' => array(
'contract' => 1,
'key' => 'my_listings',
'label' => 'My listings',
'id_field' => 'id',
'access' => array( 'audience' => 'signed_in' ),
'fields' => array(
array( 'key' => 'id', 'type' => 'number' ),
array( 'key' => 'title', 'label' => 'Name', 'type' => 'text' ),
array( 'key' => 'photo', 'type' => 'image' ),
),
'pagination' => array( 'per_page' => 20, 'max_per_page' => 50, 'total' => true ),
),
'list' => function ( array $args, WP_User $viewer ) {
// $args holds page, per_page, search, sort and params.
$items = array(); // Query your data here.
return array(
'items' => $items,
'pagination' => array( 'total' => count( $items ) ),
);
},
) );
} );

Data sources in the canvas header writes this snippet for you: open it, choose Add a source to your site, name the fields, and copy the code.

list returns one page of items. Each item is an array keyed by your field keys. Add an item callback, function ( $id, $viewer ), to look one item up by its id. Without one, list is called with $args['id'] set, so a provider that honours that argument answers both.

The example source below shows every part of the format.

access decides who can read the source, and the site enforces it on every request:

audienceWho can read
publicAnyone, signed in or not
signed_inAny signed-in member. This is the default when you leave access out
rolesMembers holding one of roles, or listed in user_ids
usersThe same pair, read the same way

Put access on a field to narrow that one field: a phone number only signed-in members see. A field the member may not read is never sent; it is not sent and hidden. Anyone who can manage your site can read everything, which is what lets you design against your own data.

Each field has a key, an optional label and a type:

TypeWhat your callback returnsDrawn as
text, selectA stringText or a chip
rich_textHTML, cleaned on the way outFormatted text
numberA numberA number, with decimals of your choice
booleantrue / false, or “yes”A tick, a chip or text
dateA timestamp or any date stringRelative (“2 days ago”), a date, or both
url, email, phoneA stringA link or a button
imageAn attachment id, a URL, or {url, width, height, alt}A photo or an avatar
galleryA list of imagesA row of photos
videoA URL, or {url, poster}A poster that opens the video
userA user idThe member’s avatar and name
postA post idIts title and image
tagsA list of strings, or a comma listChips
objectAn array with fields of its ownBind one of its fields: address.city
listRows with fields of their ownRows
buttontrue to show, false to hide, or {label, disabled}A button

Give the source and any field a description, and mark the fields every item has with 'required' => true:

array( 'key' => 'title', 'label' => 'Partner', 'type' => 'text', 'required' => true,
'description' => 'The partner as they sign themselves.' ),

Neither changes what the site sends. The builder shows the description beside the field, and the design brief and the Figma plugin hand both to whoever designs the source’s card: a design counts on a required field being there and draws every other one as optional. A description is 200 characters at most. A required field that comes back empty is listed when you sync the source; the item is still sent.

The site sends only the fields you declare. A key your callback returns but the schema does not name is dropped. A value that is not its type — a word in a number field — is sent empty, and the block shows its fallback.

conditions shows a field only when another field holds a value:

array(
'key' => 'hours',
'type' => 'text',
'conditions' => array( array( 'field' => 'is_open', 'op' => '=', 'value' => true ) ),
),

The operators are =, !=, >, <, in (a list of values), empty and not_empty. The site evaluates them, so a hidden field is not sent at all.

A button field declares what a tap does with its action:

kindWhat a tap does
open_screenOpens a screen of your project with the item. Name a target; you choose the screen under Data sources
open_itemOpens another source’s item, by the id in field
open_urlOpens the URL, email or phone number in field
triggerRuns one of your source’s actions on the site, by name

An action is a callback under actions, function ( $id, $input, $viewer ). It returns ok, a message the member sees, and optionally the updated item and open, the name of a target to go to next:

'actions' => array(
'favourite' => function ( $id, $input, $viewer ) {
// Save it, then:
return array( 'ok' => true, 'message' => 'Saved to your favourites' );
},
),

An action always needs a signed-in member, even on a public source. The site reads the item again before it runs, and refuses a button the item hides — so returning false for book on a closed listing stops the booking too. Doing the same thing twice in a few minutes asks the member whether they meant it.

  • 'search' => array( 'enabled' => true ) lets a Data Loop show a search box. The member’s words arrive as $args['search'].
  • sort lists your orderings as {key, label}. The chosen one arrives as $args['sort'].
  • params declares filters the loop can set, as {key, type} with type one of text, number, select (with options) or boolean. They arrive in $args['params']. A param you did not declare is refused.
  • pagination.total says your callback returns a total, which the loop’s “Showing 12 of 134” count reads.
  • cache.ttl keeps your callback’s answer for that many seconds. Set per_viewer when the answer depends on who is asking, or one member’s answer is served to the next. Call Memberside\Data_Sources\Registry::flush( 'my_listings' ) when your data changes behind the cache.
  1. Open your project in the canvas.
  2. Click Data sources in the header.
  3. Choose the app whose site serves the source under Connect from.
  4. Click your source under On this site.
  5. Click Connect.

The project keeps a copy of the schema and a few real items, so the blocks show your data while you design. When your site’s code changes, click Sync: it lists the fields that were added, removed or changed type, and the blocks a removed field leaves without one.

Under Button targets, choose the screen each open_screen button opens.

  1. Add a Data Loop and choose the source.
  2. Drop Data Field blocks into its Grid item template, with any other blocks you like.
  3. For each Data Field, choose the field and how it draws under Display as.
  4. Add a second screen and set Shows one to the source in its canvas settings.
  5. Add Data Fields to that screen.
  6. On the loop, set Tapping an item opens to the second screen.

The loop’s settings add a search box, a sort menu, an item count, Load more or loading as the member scrolls, a grid/list switch, Pull down to refresh, a separate list layout and a designed empty state.

The Community starter every account is given shows a Member perks grid on its Home screen. It reads Sample perks, a data source that lives in the project rather than on a site, so the Data Loop and Data Field blocks have something to show before your site serves any data.

Both the canvas and your app draw it under a Sample data notice that links here, and the store-readiness check warns while a placed screen still uses it. To replace it:

  1. Register your own source on your site.
  2. Connect it under Data sources.
  3. Select the Data Loop and choose your source.
  4. Choose your source and its fields on each Data Field in the loop.
  5. Disconnect Sample perks under Data sources.

The Memberside plugin ships a worked example, a directory of local businesses with search, sort, a category filter, a condition, a field rule and buttons. Turn it on with this line in wp-config.php:

define( 'MEMBERSIDE_DATA_EXAMPLES', true );

It appears under Data sources as Business listings. Its code is in includes/Data_Sources/class-example.php in the plugin.

Your source is not under On this site. The site’s code has not registered it. Check the snippet runs, that its key is lower-case letters, digits and underscores, and that the site runs Memberside 2.131.0 or later. “This site could not be asked” means the plugin is older than that, or the app is not connected.

Your source shows a warning and no Connect button. Its schema has problems, and the site will not serve it until they are fixed. Each is listed with the place it is at, such as fields[2].type: "picture" is not a type. Use image.

A loop says “Sign in to see this” or “This is not available to your account”. The source’s access refuses the member. Signed-out members see the first; signed-in members outside its roles see the second.

A loop says “This could not be loaded right now”. Your callback threw an error or returned something that is not {items, pagination}. Turn on WP_DEBUG and read the PHP error log; the member is never shown the error.

A field is always empty. Its value is not its declared type, or a condition or field rule hides it from that member. The sample under Data sources names every value it had to empty, item by item.

A button never appears. An open_screen button whose target is not linked under Data sources is hidden, and so is one whose value your callback returned as false.