To show a separate “Visit Website” link instead, use the Section Editor, or place the code examples below in the functions.php inside your active theme folder. Both add the link below the address, and only for stores with a URL.
Section Editor
- Go to Store Locator > Settings > Fields Manager > Section Editor, select the List section and click Load.
- Click Create Field Code, select
urlin the Additional Information group, choose thepwrapper tag and click Show Code. The generated code uses the URL itself as the link text. - Replace the link text with your own, and paste the code below the address in the editor:
<% if ( typeof url !== "undefined" && url ) { %> <p><a href="<%= url %>" target="_blank" rel="noopener">Visit Website</a></p> <% } %> Leave out
target="_blank" rel="noopener"if the link should open in the same window. - Check Activate this custom template section and click Save Section.
Repeat these steps for the Info window section to add the link to the marker pop-up as well. Text you type in the Section Editor isn’t translated. On a multilingual site, select each language in the Section Editor and save the section with its own text.
Search Results Template
With the wpsl_listing_template filter, the link text is translatable and the link follows “Open links in a new window?”. The rest of the template keeps following your settings.
| add_filter( 'wpsl_listing_template', 'custom_listing_template_url' ); | |
| function custom_listing_template_url( $listing_template ) { | |
| $new_window = wpsl_get_service( 'template_sections' )->new_window(); | |
| $link_text = esc_html__( 'Visit Website', 'your-textdomain' ); | |
| $website = '<% if ( typeof url !== "undefined" && url ) { %>' . "\r\n"; | |
| $website .= '<p><a href="<%= url %>"' . $new_window . '>' . $link_text . '</a></p>' . "\r\n"; | |
| $website .= '<% } %>' . "\r\n"; | |
| // Add the link below the address, the first paragraph in the template. | |
| $listing_template = preg_replace( '#</p>#', "</p>\r\n" . $website, $listing_template, 1 ); | |
| return $listing_template; | |
| } |
Info Window Template
The same for the marker pop-up, with the wpsl_info_window_template filter:
| add_filter( 'wpsl_info_window_template', 'custom_info_window_template_url' ); | |
| function custom_info_window_template_url( $info_window_template ) { | |
| $new_window = wpsl_get_service( 'template_sections' )->new_window(); | |
| $link_text = esc_html__( 'Visit Website', 'your-textdomain' ); | |
| $website = '<% if ( typeof url !== "undefined" && url ) { %>' . "\r\n"; | |
| $website .= '<p><a href="<%= url %>"' . $new_window . '>' . $link_text . '</a></p>' . "\r\n"; | |
| $website .= '<% } %>' . "\r\n"; | |
| // Add the link below the address, the first paragraph in the template. | |
| $info_window_template = preg_replace( '#</p>#', "</p>\r\n" . $website, $info_window_template, 1 ); | |
| return $info_window_template; | |
| } |
Use either the Section Editor or the filters for a section, not both: while a custom List or Info window section is active, the plugin uses that saved code and the filter doesn’t run for it.