{"id":12167643,"date":"2019-12-02T09:29:08","date_gmt":"2019-12-02T08:29:08","guid":{"rendered":"https:\/\/it.wordpress.org\/support\/topic\/plugin-widget-weather\/"},"modified":"2019-12-02T09:29:08","modified_gmt":"2019-12-02T08:29:08","slug":"plugin-widget-weather","status":"publish","type":"topic","link":"https:\/\/it.wordpress.org\/support\/topic\/plugin-widget-weather\/","title":{"rendered":"plugin widget weather"},"content":{"rendered":"<p>Salve ha tutti devo sviluppare un plugin che crei un widget meteo sto usando come servizio di apikey : https:\/\/www.wunderground.com\/member\/api-keys questo \u00e8 il mio script non funziona \ud83d\ude41 potete aiutarmi per favore?<br \/>\nIl plug in dovrebbe fungere cosi&#8217; :<br \/>\n&#8211; creo un widget da inserire in side bar<br \/>\n&#8211; do la possibilit\u00e0 all utente di inserire Titolo \/ citta \/ apikey<br \/>\n&#8211; mi dovrebbe ritornare la temperatura etc etc della citta richiesta<\/p>\n<p>non succede niente sembra come se l apikey non funzionasse<\/p>\n<p>&lt;?php<br \/>\n\/*<br \/>\nPlugin Name: meteo plugin<br \/>\nPlugin URI: ###<br \/>\nDescription:<br \/>\nVersion: 1.0<br \/>\nAuthor:<br \/>\nAuthor URI:<br \/>\nLicense: GPL2<br \/>\n*\/<\/p>\n<p>class Wunderground_Widget extends WP_Widget {<\/p>\n<p>public function __construct() {<br \/>\nparent::__construct(<br \/>\n&#8216;wunderground_widget&#8217;,<br \/>\n&#8216;Wunderground Widget&#8217;,<br \/>\narray( &#8216;description&#8217; =&gt; &#8216;A Widget for displaying the current weather using the Wunderground API&#8217; )<br \/>\n);<br \/>\n}<\/p>\n<p>public function widget( $args, $instance ) {<br \/>\nextract( $args );<br \/>\n$title = apply_filters( &#8216;widget_title&#8217;, $instance[&#8216;title&#8217;] );<br \/>\n$api_key = $instance[&#8216;api_key&#8217;];<br \/>\n$location = $instance[&#8216;location&#8217;];<\/p>\n<p>wp_register_style(&#8216;meteo-style&#8217;, plugins_url(&#8216;meteo\/meteo.css&#8217;, dirname(__FILE__)));<br \/>\nwp_enqueue_style(&#8216;meteo-style&#8217;);<\/p>\n<p>$api_url = &#8216;http:\/\/api.wunderground.com\/api\/&#8217;.$api_key.&#8217;\/conditions\/forecast\/q\/&#8217;.$location.&#8217;.json&#8217;;<br \/>\n$response = wp_remote_get( $api_url );<\/p>\n<p>$wunderground = json_decode($response[&#8216;body&#8217;]);<\/p>\n<p>echo $before_widget;<br \/>\n?&gt;<br \/>\n&lt;div class=&#8217;wunderground-widget&#8217;&gt;<br \/>\n&lt;?php if(isset($title)){ ?&gt;<br \/>\n&lt;h3 class=&#8217;widget-title&#8217;&gt;&lt;?php echo $title ?&gt;&lt;\/h3&gt;<br \/>\n&lt;?php<br \/>\n}<\/p>\n<p>$current = $wunderground-&gt;{&#8216;current_observation&#8217;};<br \/>\n$location = $current-&gt;{&#8216;display_location&#8217;}-&gt;{&#8216;full&#8217;};<br \/>\n$temp = $current-&gt;{&#8216;feelslike_c&#8217;};<br \/>\n$wind = $current-&gt;{&#8216;wind_kph&#8217;};<br \/>\n$wind_dir = $current-&gt;{&#8216;wind_dir&#8217;};<br \/>\n$humidity = $current-&gt;{&#8216;relative_humidity&#8217;};<br \/>\n$summary = $current-&gt;{&#8216;weather&#8217;};<\/p>\n<p>$forecast = $wunderground-&gt;{&#8216;forecast&#8217;}-&gt;{&#8216;txt_forecast&#8217;}-&gt;{&#8216;forecastday&#8217;}[0];<br \/>\n$icon = $forecast-&gt;{&#8216;icon&#8217;};<br \/>\n$icon_url = $forecast-&gt;{&#8216;icon_url&#8217;};<br \/>\n$text = $forecast-&gt;{&#8216;fcttext_metric&#8217;};<br \/>\n?&gt;<br \/>\n&lt;div&gt;<br \/>\n&lt;div class=&#8221;temp&#8221;&gt;&lt;?php echo $temp ?&gt;&deg;C <img \/>&#8221; alt=&#8221;&lt;?php echo $icon ?&gt;&#8221;\/&gt;&lt;\/div&gt;<br \/>\n&lt;div class=&#8221;location&#8221;&gt;&lt;?php echo $location ?&gt;&lt;\/div&gt;<br \/>\n&lt;div class=&#8221;wind&#8221;&gt;Vento: &lt;?php echo $wind ?&gt;kph (&lt;?php echo $wind_dir ?&gt;)&lt;\/div&gt;<br \/>\n&lt;div class=&#8221;humidity&#8221;&gt;Umido&#8217;: &lt;?php echo $humidity ?&gt;&lt;\/div&gt;<br \/>\n&lt;div class=&#8221;summary&#8221; title=&#8221;&lt;?php echo $text ?&gt;&#8221;&gt;Sunto: &lt;?php echo $summary ?&gt;&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;?php<br \/>\necho $after_widget;<br \/>\n}<\/p>\n<p>public function update( $new_instance, $old_instance ) {<br \/>\n$instance = array();<br \/>\n$instance[&#8216;title&#8217;] = strip_tags( $new_instance[&#8216;title&#8217;] );<br \/>\n$instance[&#8216;api_key&#8217;] = strip_tags( $new_instance[&#8216;api_key&#8217;] );<br \/>\n$instance[&#8216;location&#8217;] = strip_tags( $new_instance[&#8216;location&#8217;] );<br \/>\nreturn $instance;<br \/>\n}<\/p>\n<p>public function form( $instance ) {<br \/>\nif ( isset( $instance[ &#8216;title&#8217; ] ) ) {<br \/>\n$title = $instance[ &#8216;title&#8217; ];<br \/>\n}<br \/>\nelse {<br \/>\n$title = &#8216;New title&#8217;;<br \/>\n}<\/p>\n<p>if ( isset( $instance[ &#8216;api_key&#8217; ] ) ) {<br \/>\n$api_key = $instance[ &#8216;api_key&#8217; ];<br \/>\n}<br \/>\nelse {<br \/>\n$api_key = &#8221;;<br \/>\n}<\/p>\n<p>if ( isset( $instance[ &#8216;location&#8217; ] ) ) {<br \/>\n$location = $instance[ &#8216;location&#8217; ];<br \/>\n}<br \/>\nelse {<br \/>\n$location = &#8221;;<br \/>\n}<br \/>\n?&gt;<br \/>\n&lt;p&gt;<br \/>\n&lt;label for=&#8221;&lt;?php echo $this-&gt;get_field_id( &#8216;location&#8217; ); ?&gt;&#8221;&gt;&lt;?php _e( &#8216;Location:&#8217; ); ?&gt;&lt;\/label&gt;<br \/>\n&lt;input class=&#8221;widefat&#8221; id=&#8221;&lt;?php echo $this-&gt;get_field_id( &#8216;location&#8217; ); ?&gt;&#8221; name=&#8221;&lt;?php echo $this-&gt;get_field_name( &#8216;location&#8217; ); ?&gt;&#8221; type=&#8221;text&#8221; value=&#8221;&lt;?php echo esc_attr( $location ); ?&gt;&#8221; \/&gt;<\/p>\n<p>&lt;label for=&#8221;&lt;?php echo $this-&gt;get_field_id( &#8216;title&#8217; ); ?&gt;&#8221;&gt;&lt;?php _e( &#8216;Title:&#8217; ); ?&gt;&lt;\/label&gt;<br \/>\n&lt;input class=&#8221;widefat&#8221; id=&#8221;&lt;?php echo $this-&gt;get_field_id( &#8216;title&#8217; ); ?&gt;&#8221; name=&#8221;&lt;?php echo $this-&gt;get_field_name( &#8216;title&#8217; ); ?&gt;&#8221; type=&#8221;text&#8221; value=&#8221;&lt;?php echo esc_attr( $title ); ?&gt;&#8221; \/&gt;<\/p>\n<p>&lt;label for=&#8221;&lt;?php echo $this-&gt;get_field_id( &#8216;api_key&#8217; ); ?&gt;&#8221;&gt;&lt;?php _e( &#8216;API Key:&#8217; ); ?&gt;&lt;\/label&gt;<br \/>\n&lt;input class=&#8221;widefat&#8221; id=&#8221;&lt;?php echo $this-&gt;get_field_id( &#8216;api_key&#8217; ); ?&gt;&#8221; name=&#8221;&lt;?php echo $this-&gt;get_field_name( &#8216;api_key&#8217; ); ?&gt;&#8221; type=&#8221;text&#8221; value=&#8221;&lt;?php echo esc_attr( $api_key ); ?&gt;&#8221; \/&gt;<br \/>\n&lt;\/p&gt;<br \/>\n&lt;?php<br \/>\n}<\/p>\n<p>}<\/p>\n<p>function wunderground_widgets_init(){<br \/>\nregister_widget( &#8216;Wunderground_Widget&#8217; );<br \/>\n}<br \/>\nadd_action( &#8216;widgets_init&#8217;, &#8216;wunderground_widgets_init&#8217; );<\/p>\n","protected":false},"template":"","class_list":["post-12167643","topic","type-topic","status-publish","hentry","topic-tag-api","topic-tag-apikey","topic-tag-plugin","topic-tag-weather","topic-tag-weatherplugin","topic-tag-widget"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/12167643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/12167643\/revisions"}],"wp:attachment":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=12167643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}