Hi,
I managed to get it working using ajax, calling to bpwfwp_print_contact_card providing the parameter location.
However the value of rtb-location from the location drop down is Term_ID from taxonomy table, the location id parameter required by bpwfwp_print_contact_card is Post_ID.
How do I map between the 2 ? is it matching PostName from POST table against Slug from TERM table ?
i.e use rtb-location as term_id to find the corresponding slug, look up matching slug as post name from POST table and the post_id is the location_id to bpwfwp_print_contact_card.
Is that correct way of mapping rtb-location value to location_id ?
Regards,
Josh
Ok I got it sorted out with WP_Query. Thanks. Now it;s working perfectly as what I wanted.
For anyone what is interested :
Child Theme functions.php
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
//Register Opening Hours Ajax
wp_enqueue_script( 'rtb-mylocation-js', get_stylesheet_directory_uri() . '/assets/js/opening-hours.js', array( 'jquery' ), '', true );
wp_localize_script( 'rtb-mylocation-js', 'location_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action( 'wp_ajax_the_ajax_hook', 'location_action_function' );
add_action( 'wp_ajax_nopriv_the_ajax_hook', 'location_action_function' );
function location_action_function(){
$args = array(
'post_type' => array('location'),
'post_status' => array('publish'),
'meta_query' => array(
array(
'key' => 'rtb_location',
'value' => $_POST['rtb-location']
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$query->the_post();
$location_id = get_the_ID();
}
$openinghours = '';
if ( function_exists( 'bpwfwp_print_contact_card' ) )
{
$openinghours = bpwfwp_print_contact_card(
array(
'show_name' => true,
'show_address' => false,
'show_get_directions' => false,
'show_phone' => false,
'show_contact' => false,
'show_opening_hours' => true,
'show_opening_hours_brief' => true,
'show_map' => false,
'location' => $location_id,
)
);
}
echo $openinghours; // this is passed back to the javascript function
die();
}
?>
jQuery(document).ready(function ($) {
$('#rtb-location').change(function(){
$('#opening-hours').remove();
var location_id = $(this).val();
if ($(this).val()==='') { alert('Please select a location'); return false;}
jQuery.post(location_ajax_script.ajaxurl, { 'action': 'the_ajax_hook', 'rtb-location': location_id } ,
function(response_from_the_action_function){
$('.location').append('<div id="opening-hours">' + response_from_the_action_function + '</div>');
});
});
});
Hi Joschua,
Wow, you got that figured out before we had a chance to get back to you after the weekend. Good stuff! And thank you for sharing your workaround, which may help someone else looking to get a similar functionality.