afssafsafsa

Plain Text

Author: ahambridge

1 months ago 1,414 B


add_action('wpcf7_init', 'register_property_select_tag');

function register_property_select_tag() {
    wpcf7_add_form_tag(
        array('property_select', 'property_select*'),
        'render_property_select_tag',
        array('name-attr' => true)
    );
}

function render_property_select_tag($tag) {
    if (empty($tag->name)) {
        return '';
    }

    $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'meta_key'       => '_wp_page_template',
        'meta_value'     => 'page-propertydetails.php',
        'orderby'        => 'title',
        'order'          => 'ASC',
    );

    $query = new WP_Query($args);

    $required = $tag->is_required() ? ' required' : '';
    $class = 'wpcf7-form-control wpcf7-select';

    $html  = '<select name="' . esc_attr($tag->name) . '" class="' . esc_attr($class) . '"' . $required . '>';
    $html .= '<option value="">Choose a property</option>';

    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            $property_name = get_field('property_name');

            if ($property_name) {
                $html .= '<option value="' . esc_attr($property_name) . '">' . esc_html($property_name) . '</option>';
            }
        }
        wp_reset_postdata();
    }

    $html .= '</select>';

    return $html;
}