Skip to content

Using repeatable groups

nadavrt edited this page May 29, 2015 · 11 revisions

The Simple Meta Boxes class enables you to group several fields into a repeatable group. Repeatable groups are convenient in cases that require the same couple of fields to appear together in a repeatable manner for an undefined number of times inside a post/page. A repeatable group will allow you to duplicate an entire group, as opposed to a repeater field, which only duplicates one specific field.

By adding the repeater_group key to a metabox all the fields in that respective metabox will become a repeatable group. The value of repeater_group should be an array. The array can be empty, but you can also pass it several optional keys (arguments):

title - String. Optional. The titles for the group items. The default title is "group".

numbering - Boolean. Optional. Used to denote whether or not the group titles should be numbered. Defaults to TRUE.

numbering_prefix - String. Optional. Allows you to specify the prefix that comes before the group title number. Default to an empty string. This argument will be ignored if the numbering arguments is set to FALSE.

Lets create a group together. This group will be used on a "contact us" template and enable the end-user to specify contact details for different people. The group will only appear on the "contact us" template and will have the following fields: name, phone number and email.

	$metaboxes['contact_us_fields'] = array(
		'id' => 'contact_us_fields',
		'title' => 'Contacts List',
		'page_templates' => array( 'page-contact-us.php' ),
		'repeater_group' => array(
			'title' => 'Contact Person',
			'numbering_prefix' => '#',
		),
		'fields' => array(
	        array(
			    'title' => 'Name',
			    'id' => 'name',
			    'type' => 'text',
			),
			array(
			    'title' => 'Phone',
			    'id' => 'phone',
			    'type' => 'text',
			),
			array(
			    'title' => 'Email',
			    'id' => 'email',
			    'type' => 'email',
			),
		)
	);

Clone this wiki locally