wpsl_meta_box_fields

  1. Description
  2. Parameters
  3. Usage
  4. Changelog
  5. Source File
  6. Related

Description

This filter allows you to change the fields in the “Store Details” box on the store edit screen. Each tab in the box is a key in the array, and holds the fields shown on that tab.

The easier way to add fields is Store Locator > Settings > Fields Manager. Fields you create there are already in the array when this filter runs, and they’re included in the search results automatically. Use this filter when the fields have to be defined in code, for example in a theme or plugin.

Fields you add with this filter are saved with the store, but they aren’t added to the location data on the front end. To use them in the templates, add them with wpsl_frontend_meta_fields, and with wpsl_cpt_info_window_meta_fields for the pop-up on store pages.

Don’t replace one of the plugin’s tabs, for example with $meta_fields['Location'] = [ ... ]. That removes the plugin’s own fields from the tab. Add your field to the tab instead, as in the example below.

Parameters

$meta_fields - (array) The tabs, with the fields on each tab. The plugin's tabs are Location, Opening Hours and Additional Information, followed by the groups from the Fields Manager. Use esc_html__( 'Additional Information', 'wp-store-locator' ) as the key, so it also matches on a translated site. Each field key is the field name, and each field can have these options:

OptionDescription
labelThe label shown before the field.
typeOptional. text (the default), tel, email, url, textarea, wp_editor, checkbox or dropdown. The type also decides how the value is cleaned up when it's saved.
requiredOptional. Set to true to add a * after the label. A store without a value for this field is saved as pending instead of published.
defaultOptional. The value a text or textarea field starts with when it's empty.
optionsFor the dropdown type. An array of value => label pairs; the value is saved.

Usage

This example adds a Mobile field to the Additional Information tab, and a new Extra Details tab with the other field types.

add_filter( 'wpsl_meta_box_fields', 'custom_meta_box_fields' );
function custom_meta_box_fields( $meta_fields ) {
// Add a field to the existing "Additional Information" tab.
$meta_fields[ esc_html__( 'Additional Information', 'wp-store-locator' ) ]['mobile'] = [
'label' => esc_html__( 'Mobile', 'your-textdomain' ),
'type' => 'tel',
];
// Add a new tab with the other field types.
$meta_fields[ esc_html__( 'Extra Details', 'your-textdomain' ) ] = [
'my_textinput' => [
'label' => esc_html__( 'Text input', 'your-textdomain' ),
],
'my_checkbox' => [
'label' => esc_html__( 'Checkbox', 'your-textdomain' ),
'type' => 'checkbox',
],
'my_textarea' => [
'label' => esc_html__( 'Textarea', 'your-textdomain' ),
'type' => 'textarea',
],
'my_dropdown' => [
'label' => esc_html__( 'Dropdown', 'your-textdomain' ),
'type' => 'dropdown',
'options' => [
'option1' => esc_html__( 'Option 1', 'your-textdomain' ),
'option2' => esc_html__( 'Option 2', 'your-textdomain' ),
],
],
'my_editor' => [
'label' => esc_html__( 'Description', 'your-textdomain' ),
'type' => 'wp_editor',
],
];
return $meta_fields;
}

The fields are saved as post meta with a wpsl_ prefix, so my_textinput is saved as wpsl_my_textinput. Use a name that isn’t already used by the plugin or in the Fields Manager, otherwise the store edit screen shows two fields that overwrite each other.

To show the mobile number in the search results, add wpsl_mobile with the phone type with wpsl_frontend_meta_fields, and add it to the contact details as shown in Customize the Contact Details.

For a field type the plugin doesn’t support, output the input yourself with the wpsl_metabox_{type}_input action. It receives $args, with the field name in $args['key'] and the field options in $args['data']. Name the input wpsl[field_name] so the plugin saves it.

Changelog

VersionDescription
3.0Moved to includes/core/store/class-fields-manager.php. The array also includes the groups created in the Fields Manager.
2.0Introduced.

Source File

This filter is located in /includes/core/store/class-fields-manager.php.

Placement

The code needs to be placed in the functions.php file inside your active theme folder.