Description
This filter allows you to modify the HTML structure and add additional data to the store listing template.
Usage
In this example the data from a custom field called “my_textinput” is added after the address details:
add_filter( 'wpsl_listing_template', 'custom_listing_template' );
function custom_listing_template() {
global $wpsl, $wpsl_settings;
$listing_template = '<li data-store-id="<%= id %>">' . "\r\n";
$listing_template .= "\t\t" . '<div class="wpsl-store-location">' . "\r\n";
$listing_template .= "\t\t\t" . '<p><%= thumb %>' . "\r\n";
$listing_template .= "\t\t\t\t" . wpsl_store_header_template( 'listing' ) . "\r\n"; // Check which header format we use
$listing_template .= "\t\t\t\t" . '<span class="wpsl-street"><%= address %></span>' . "\r\n";
$listing_template .= "\t\t\t\t" . '<% if ( address2 ) { %>' . "\r\n";
$listing_template .= "\t\t\t\t" . '<span class="wpsl-street"><%= address2 %></span>' . "\r\n";
$listing_template .= "\t\t\t\t" . '<% } %>' . "\r\n";
$listing_template .= "\t\t\t\t" . '<span>' . wpsl_address_format_placeholders() . '</span>' . "\r\n"; // Use the correct address format
if ( !$wpsl_settings['hide_country'] ) {
$listing_template .= "\t\t\t\t" . '<span class="wpsl-country"><%= country %></span>' . "\r\n";
}
$listing_template .= "\t\t\t" . '</p>' . "\r\n";
/**
* Include the data from a custom field called 'my_textinput'.
*
* Before you can access the 'my_textinput' data in the template,
* you first need to make sure the data is included in the JSON output.
*
* You can make the data accessible through the wpsl_frontend_meta_fields filter.
*/
$listing_template .= "\t\t\t" . '<% if ( my_textinput ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<p><%= my_textinput %></p>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
// Show the phone, fax or email data if they exist.
if ( $wpsl_settings['show_contact_details'] ) {
$listing_template .= "\t\t\t" . '<p class="wpsl-contact-details">' . "\r\n";
$listing_template .= "\t\t\t" . '<% if ( phone ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'phone_label', __( 'Phone', 'wpsl' ) ) ) . '</strong>: <%= formatPhoneNumber( phone ) %></span>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
$listing_template .= "\t\t\t" . '<% if ( fax ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'fax_label', __( 'Fax', 'wpsl' ) ) ) . '</strong>: <%= fax %></span>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
$listing_template .= "\t\t\t" . '<% if ( email ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'email_label', __( 'Email', 'wpsl' ) ) ) . '</strong>: <%= email %></span>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
$listing_template .= "\t\t\t" . '</p>' . "\r\n";
}
$listing_template .= "\t\t\t" . wpsl_more_info_template() . "\r\n"; // Check if we need to show the 'More Info' link and info
$listing_template .= "\t\t" . '</div>' . "\r\n";
$listing_template .= "\t\t" . '<div class="wpsl-direction-wrap">' . "\r\n";
if ( !$wpsl_settings['hide_distance'] ) {
$listing_template .= "\t\t\t" . '<%= distance %> ' . esc_html( $wpsl_settings['distance_unit'] ) . '' . "\r\n";
}
$listing_template .= "\t\t\t" . '<%= createDirectionUrl() %>' . "\r\n";
$listing_template .= "\t\t" . '</div>' . "\r\n";
$listing_template .= "\t" . '</li>';
return $listing_template;
}
The <% ... %> and <%= ... %> code is used by the
Underscore library to execute JS code, and to print the correct JSON data when the HTML template is rendered.
If the changes you made don't show up on the frontend, then make sure to flush the store locator transient cache. You can do this on the
settings page.
Change the Search Results Template Based on the Used Page
If your using the store locator on different pages, and you’d like to format the search results in a different way on some pages, then you can do so by using is_page in the template code.
add_filter( 'wpsl_listing_template', 'custom_listing_template' );
function custom_listing_template() {
global $wpsl, $wpsl_settings;
if ( is_page( 'your-page' ) ) {
// The template code for 'your-page' goes here
} else {
// The template code for all other pages goes here
}
}
Replace ‘your-page’ with any of the accepted parameters for is_page.
Make Different Data Clickable
The code examples below show how to make additional URL, phone or email data clickable in the template.
$listing_template .= "\t\t\t" . '<% if ( extra_url ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<p><a href="<%= extra_url %>"><%= extra_url %></a></p>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
$listing_template .= "\t\t\t" . '<% if ( extra_phone ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<p><%= formatPhoneNumber( extra_phone ) %></p>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
$listing_template .= "\t\t\t" . '<% if ( extra_email ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<p><%= formatEmail( extra_email ) %></p>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
Source File This filter is located in /frontend/underscore-functions.php.
Placement The code needs to be placed in the functions.php file inside your active theme folder.