Description
This filter allows you to add your own store locator template, loaded from outside the WP Store Locator plugin folder. That way you don’t lose your changes when you update the plugin.
How to create the template file is explained in Load a Custom Store Locator Template.
Adding a template with this filter doesn’t activate it. Go to Store Locator > Appearance, open the Themes tab, and click the toggle on the card of your template. To use the template on one page only, set it in the shortcode on that page: [wpsl template="my-custom-template"]. The template attribute always overrides the template selected on the Appearance page.
Parameters
$templates - (array) The list of templates. Each template is an array with these keys:
| Key | Required | Description |
|---|---|---|
id | Yes | A unique ID. Don't use default, horizontal or vertical. You use the ID in the template shortcode attribute, and the outer #wpsl-wrap div gets a wpsl-{id}-template class. |
name | Yes | The name shown on the template card on the Appearance page. |
path | Yes | The full path to the template file. If the file doesn't exist, the Default template is used instead. |
desc | No | The description shown below the name on the template card. Defaults to "Custom template". |
placeholder | No | The URL of the image shown on the template card. Defaults to a generic custom template image. |
has_panel | No | Set it to true for a copy of the Vertical template. The outer div then gets the wpsl-flex and wpsl-has-panel classes, the panel height from the Appearance settings is applied, and the styled dropdowns are always used. Without it, the panel layout isn't applied. |
customize_path | No | Set on the built-in Default template. WP Store Locator 3.0.3 doesn't read it, so you can leave it out. |
exclude_css_properties | No | Set on the built-in Vertical template. WP Store Locator 3.0.3 doesn't read it, so you can leave it out. |
Usage
This example loads a custom template from the wpsl-templates folder in your active theme folder:
| function my_wpsl_custom_template( $templates ) { | |
| $templates[] = [ | |
| 'id' => 'my-custom-template', | |
| 'name' => 'My custom template', | |
| 'desc' => 'Shown under the template name on the Appearance page.', | |
| 'path' => get_stylesheet_directory() . '/wpsl-templates/custom.php', | |
| // Only when you copied vertical.php: | |
| // 'has_panel' => true, | |
| ]; | |
| return $templates; | |
| } | |
| add_filter( 'wpsl_templates', 'my_wpsl_custom_template' ); |
If your template is a copy of vertical.php, remove the // in front of the 'has_panel' => true line.
The styles for the Horizontal and Vertical templates only apply to the .wpsl-horizontal-template and .wpsl-vertical-template classes. A copy gets its own wpsl-{id}-template class instead, so add the original class back with the wpsl_template_outer_classes filter:
| add_filter( 'wpsl_template_outer_classes', 'my_wpsl_template_classes' ); | |
| function my_wpsl_template_classes( $classes ) { | |
| if ( in_array( 'wpsl-my-custom-template-template', $classes, true ) ) { | |
| $classes[] = 'wpsl-vertical-template'; // or wpsl-horizontal-template | |
| } | |
| return $classes; | |
| } |
The default templates are located in /wp-content/plugins/wp-store-locator/includes/frontend/templates: default.php, horizontal.php and vertical.php.
Changelog
| Version | Description |
|---|---|
| 3.0 | Moved to includes/core/functions/functions-templates.php. Added the optional desc, placeholder and has_panel keys. Templates are now activated on the Appearance page, or with the template shortcode attribute, instead of the "Store Locator template" dropdown in the User Experience settings. The default templates moved to includes/frontend/templates, and the "below_map" template was replaced by Horizontal. Templates written for 2.x run in legacy mode. |
| 2.0 | Added the required id key. Moved to inc/wpsl-functions.php. |
| 1.2.20 | Introduced. |
Source File
This filter is located in /includes/core/functions/functions-templates.php.
Placement
The code needs to be placed in the functions.php file inside your active theme folder.