Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Created October 29, 2020 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingram3541/306383b95b6415f4e6c72edd09108f04 to your computer and use it in GitHub Desktop.
Save pingram3541/306383b95b6415f4e6c72edd09108f04 to your computer and use it in GitHub Desktop.
Dynamic Widget checks for ACF field value or uses Shortcode fallback
/**
* 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;
$custom_code = get_field( 'custom_map_code' ); //get ACF map embed
$result = $args['before_widget'];
if ( $custom_code ){
$result .= $custom_code;
} else {
$result .= do_shortcode( '[elementor-template id="1030"]' );
}
$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' );
});
@pingram3541
Copy link
Author

The shortcode on line 30 is of a previously created Elementor Global Google Map widget in which it used the post's title for it's dynamic address which was the city name, providing a basic city map vs a real physical address if an embed had been provided.

In theory, the ACF field could be a simple address and invoke a different global map widget using that as the dynamic address source.

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