• Hello, I’m helping you can help me figure out how to display custom fields on the frontend. I will describe my situation thus far:

    I have copied the “content below map file” template file to my theme, renamed it “schools.php” and added the line to the functions file which makes it work. This works fine. (*Note all of the below code is in my functions file in the same order posted here)

    /*Add custom template and set location in themes folder*/
    add_filter( 'wpsl_templates', 'custom_templates' );
    
    function custom_templates( $templates ) {
    
        $templates[] = array (
            'id'   => 'schools',
            'name' => 'School Directory',
            'path' => get_stylesheet_directory() . '/' . 'wpsl-templates/schools.php',
        );
    
        return $templates;
    }

    Then I added my first custom field using the code you provide in your documentation:

    /*Add the custom fields*/
    add_filter( 'wpsl_meta_box_fields', 'custom_meta_box_fields' );
    
    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' )
            ),
            'tuition_url' => array(
                'label' => __( 'Tuition Information', 'wpsl' )
            )
        );
    
        return $meta_fields;
    }

    This also works, field now displays. Now I set it to save to the database…

    /*Store the custom fields to the DB*/
    add_filter( 'wpsl_frontend_meta_fields', 'custom_frontend_meta_fields' );
    
    function custom_frontend_meta_fields( $store_fields ) {
    
        $store_fields['tuition_url'] = array( 
            'name' => 'tuition_url',
            'type' => 'url'
        );
    
        return $store_fields;
    }

    This works as well. I can save the field value, and when I reopen that store in the backend my value saved. In this case it is a web url.

    So now when I try to get it to display in the frontend this is where eveything falls apart. So here is what I have:

    /*Return the custom fields on the frontend*/
    add_filter( 'wpsl_listing_template', 'schools' );
    
    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 'appointment_url' contains data before including it.
        $listing_template .= "\t\t\t" . '<% if ( tuition_url ) { %>' . "\r\n";
        $listing_template .= "\t\t\t" . '<p><a href="<%= tuition_url %>">' . __( 'Tuition Information', 'wpsl' ) . '</a></p>' . "\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;
    }

    Once this last block of code has been added if I visit the frontend of the site and use the locator no listings get returned under the map. The map still works, and if you click a marker its infowindow opens as expectged. If I delete this code from the functions things resume working as they should, but obviously the custom field I added doesnt display.

    Is there anything obvious that I screwed up here? Thanks for all of your help.

Viewing 1 replies (of 1 total)
  • Plugin Author Tijmen Smit

    (@tijmensmit)

    This add_filter( 'wpsl_listing_template', 'schools' ); should be add_filter( 'wpsl_listing_template', 'custom_listing_template' );

    If after changing that it still doesn’t work, then make sure no JS errors show up in the browser console that are related to the store locator plugin?

    If so, try to flush the WPSL transient cache. You can do this on the WPSL settings page ( tools section ).

Viewing 1 replies (of 1 total)
  • The topic ‘Confusion on adding new Fields’ is closed to new replies.