Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Created September 19, 2018 20:30
Show Gist options
  • Save pingram3541/f6b74e9a8f1e9419c908eb93efbab30f to your computer and use it in GitHub Desktop.
Save pingram3541/f6b74e9a8f1e9419c908eb93efbab30f to your computer and use it in GitHub Desktop.
Widget - Switch Map based on post's ACF values
/**
* add custom widget to switch city page map preferences
*
* Simply performs an audit on City Pages to check if
* the preference is to output custom code or use the
* automatic map search by city name
*
**/
class Custom_City_Map_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'custom_city_map_widget',
'description' => 'Determines if City Page has a preference to display a custom code based map or use the automated search by City Name field',
);
parent::__construct( 'custom_city_map_widget', 'Custom City Map', $widget_options );
}
public function widget( $args, $instance ) {
$current_post = get_queried_object();
$post_id = $current_post ? $current_post->ID : null;
//$parent_id = wp_get_post_parent_id( $post_id );
//global $post;
//$post_id = $post->ID;
$use_custom = get_field( 'custom_map_or_auto' ); //true or false value
$result = $args['before_widget'];
if ( $use_custom && $use_custom == 'false' ){
$result .= do_shortcode( '[elementor-template id="1030"]' );
} else {
$custom_code = get_field( 'custom_map_code' );
$result .= $custom_code;
}
$result .= $args['after_widget'];
echo $result;
}
public function form( $instance ) {
//future use to add widget settings fields
}
public function update( $new_instance, $old_instance ) {
//future use to submit widget settings fields
}
}
add_action( 'widgets_init', function(){
register_widget( 'Custom_City_Map_Widget' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment