Skip to content

Instantly share code, notes, and snippets.

@dannyfoo
Created December 15, 2021 07:20
Show Gist options
  • Save dannyfoo/7a3f5f25dd8c1e7dd30bde81a83b8816 to your computer and use it in GitHub Desktop.
Save dannyfoo/7a3f5f25dd8c1e7dd30bde81a83b8816 to your computer and use it in GitHub Desktop.
Add and display custom fields to WP Store Locator listing template and info window template
function custom_meta_box_fields( $meta_fields ) {
$meta_fields[__( 'Additional Information', 'wpsl' )] = array(
'phone' => array(
'label' => __( 'Tel', 'wpsl' )
),
'fax' => array(
'label' => __( 'Fax', 'wpsl' )
),
'email' => array(
'label' => __( 'Email', 'wpsl' )
),
'url' => array(
'label' => __( 'Url', 'wpsl' )
),
'my_editor' => array(
'label' => __( 'Description', 'wpsl' ),
'type' => 'wp_editor',
'options' => array(
'media_buttons' => false,
'tinymce' => false,
),
),
);
return $meta_fields;
}
add_filter( 'wpsl_meta_box_fields', 'custom_meta_box_fields' );
function custom_frontend_meta_fields( $store_fields ) {
$store_fields['wpsl_my_editor'] = array(
'name' => 'my_editor'
);
return $store_fields;
}
add_filter( 'wpsl_frontend_meta_fields', 'custom_frontend_meta_fields' );
function custom_listing_template() {
global $wpsl_settings;
$listing_template = '<li data-store-id="<%= id %>">' . "\r\n";
$listing_template .= "\t\t" . '<div>' . "\r\n";
$listing_template .= "\t\t\t" . '<p><%= thumb %>' . "\r\n";
$listing_template .= "\t\t\t\t" . wpsl_store_header_template( 'listing' ) . "\r\n";
$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";
$listing_template .= "\t\t\t\t" . '<span class="wpsl-country"><%= country %></span>' . "\r\n";
$listing_template .= "\t\t\t" . '</p>' . "\r\n";
// Check if the 'my_editor' contains data before including it.
$listing_template .= "\t\t\t" . '<% if ( my_editor ) { %>' . "\r\n";
$listing_template .= "\t\t\t" . '<%= my_editor %>' . "\r\n";
$listing_template .= "\t\t\t" . '<% } %>' . "\r\n";
$listing_template .= "\t\t" . '</div>' . "\r\n";
// Check if we need to show the distance.
if ( !$wpsl_settings['hide_distance'] ) {
$listing_template .= "\t\t" . '<%= distance %> ' . esc_html( $wpsl_settings['distance_unit'] ) . '' . "\r\n";
}
$listing_template .= "\t\t" . '<%= createDirectionUrl() %>' . "\r\n";
$listing_template .= "\t" . '</li>' . "\r\n";
return $listing_template;
}
add_filter( 'wpsl_listing_template', 'custom_listing_template' );
function custom_info_window_template() {
global $wpsl_settings, $wpsl;
$info_window_template = '<div data-store-id="<%= id %>" class="wpsl-info-window">' . "\r\n";
$info_window_template .= "\t\t" . '<p>' . "\r\n";
$info_window_template .= "\t\t\t" . wpsl_store_header_template() . "\r\n";
$info_window_template .= "\t\t\t" . '<span><%= address %></span>' . "\r\n";
$info_window_template .= "\t\t\t" . '<% if ( address2 ) { %>' . "\r\n";
$info_window_template .= "\t\t\t" . '<span><%= address2 %></span>' . "\r\n";
$info_window_template .= "\t\t\t" . '<% } %>' . "\r\n";
$info_window_template .= "\t\t\t" . '<span>' . wpsl_address_format_placeholders() . '</span>' . "\r\n";
$info_window_template .= "\t\t" . '</p>' . "\r\n";
$info_window_template .= "\t\t" . '<% if ( phone ) { %>' . "\r\n";
$info_window_template .= "\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'phone_label', __( 'Phone', 'wpsl' ) ) ) . '</strong>: <%= formatPhoneNumber( phone ) %></span>' . "\r\n";
$info_window_template .= "\t\t" . '<% } %>' . "\r\n";
$info_window_template .= "\t\t" . '<% if ( fax ) { %>' . "\r\n";
$info_window_template .= "\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'fax_label', __( 'Fax', 'wpsl' ) ) ) . '</strong>: <%= fax %></span>' . "\r\n";
$info_window_template .= "\t\t" . '<% } %>' . "\r\n";
$info_window_template .= "\t\t" . '<% if ( email ) { %>' . "\r\n";
$info_window_template .= "\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'email_label', __( 'Email', 'wpsl' ) ) ) . '</strong>: <%= formatEmail( email ) %></span>' . "\r\n";
$info_window_template .= "\t\t" . '<% } %>' . "\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.
*/
$info_window_template .= "\t\t" . '<% if ( my_editor ) { %>' . "\r\n";
$info_window_template .= "\t\t" . '<%= my_editor %>' . "\r\n";
$info_window_template .= "\t\t" . '<% } %>' . "\r\n";
$info_window_template .= "\t\t" . '<%= createInfoWindowActions( id ) %>' . "\r\n";
$info_window_template .= "\t" . '</div>' . "\r\n";
return $info_window_template;
}
add_filter( 'wpsl_info_window_template', 'custom_info_window_template' );
@dannyfoo
Copy link
Author

Still trying to figure out how to remove the media upload option. And if possible, visual (tinymce) tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment