I have built a handful of custom widgets on my site. In the main these are just fine how they are. But occassionally I also want to be able to use a field individually rather than the whole widget.
For example I have an address widget which has five fields: 'Street', 'Locality', 'Region', 'PostCode', and 'Country'.
The single address that is output will be used several locations on the site, but occasionally I just need the value from one field at a time eg: just the 'PostCode'.
Is there a way to extract individual field data from a widget?
As I assume is pretty standard the display for my widget is handled like so:
public function widget( $args, $instance ) {
extract( $args );
$street = $instance['street'];
$locality = $instance['locality'];
$region = $instance['region'];
$postCode = $instance['postCode'];
$country = $instance['country'];
echo $before_widget;
// Display the widget
if ( $street ) {
echo $street . '<br>';
}
if ( $locality ) {
echo $locality . ',<br>';
}
if ( $region ) {
echo $region . ',<br>';
}
if ( $postCode ) {
echo $postCode . '<br>';
}
if ( $country ) {
echo $country;
}
echo $after_widget;
}
It is used in a sidebar call 'address' and currently I output that to the page like so:
if ( is_active_sidebar( 'address' ) ) { dynamic_sidebar( 'address' ); }
My assumtion is that either an edit to the latter would do what I'm after, or I can somehow access the individual fields of the widget in the sidebar seperately. Eiter way (if any assumption is correct) I need some direction as to how to do this. I will need to usethis same data in several locations, just on occassion not all of it.