Website Weather Integration

Here’s a quick tutorial on how to integrate your own, near real-time weather stats into your website. There are sites, such as AccuWeather (who should, in all honesty, change their name) and Weather Underground, that will do this for you, but I find that you’re 1) beholden to their style and 2) not very accurate. The Weather Underground one for example will say one thing on your site but when you click the sticker (that’s what they call it) it says a completely different thing; their mobile app does the same thing. As with any of these sites that provide weather information, unless you live right next to where they have their sensors set up (typically the closest airport), the readings are likely to be off. This tutorial will be using NOAA, so let’s get started.

What you’ll need first is your location, latitude and longitude. There are a number of sites that you can get that information from; this one works.

Next, you’ll need to download weather.js (right-click save-as) and change the lat & lon variables to your location.

/**
 * Change to your location. The below is Washington D.C.
 */
var lat = '38.907192';
var lon = '-77.036871';

Then add the following to the page you want to integrate weather into by adding a DIV element with the id of weather, like so:

<div id="weather"></div>

Towards the end of your page, before the <body> tag, add the following:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="weather.js"></script>
<script>
    <!--
    $(document).ready(function() {
        // get initial weather
        getWeather();
        // refresh every 15 minutes; 900 * 1000
        setInterval(function() { getWeather(); }, 900000);
    }
    -->
</script>

The above will result in something like the below that will automatically update every 15 minutes.

The following CSS was used to style the current conditions icon:

#weather-icon {
    -webkit-border-top-left-radius:5px;
    -moz-border-radius-topleft:5px;
    border-top-left-radius:5px;
    -webkit-border-top-right-radius:5px;
    -moz-border-radius-topright:5px;
    border-top-right-radius:5px;
    -webkit-border-bottom-right-radius:5px;
    -moz-border-radius-bottomright:5px;
    border-bottom-right-radius:5px;
    -webkit-border-bottom-left-radius:5px;
    -moz-border-radius-bottomleft:5px;
    border-bottom-left-radius:5px;
    border: 1px solid #b0b0b0;
}

And that’s all I have to say about that.

Listing WordPress Posts

So you have a WordPress site that hosts your blog posts along with a regular site and you want to list the blog posts on the other site, dynamically without having to manually add to the list every time you post something new. Here’s how I accomplished it.

Disclaimer: I found some of this on another site, but forgot where, so apologies for not giving credit where credit is due.

Create a posts.php page at the root of your WordPress site with the following:

<?php require('wp-blog-header.php');
    status_header(200);
    nocache_headers();
?>
<h4>::~ChrEli-BLOG~:: Posts</h4>
<table>
    <thead>
        <tr>
            <th>Post</th>
            <th>Published</th>
        </tr>
    </thead>
    <tbody>
<?php
$args = array( 'numberposts' => 100, 'category' => 0, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$posts = get_posts( $args );

 foreach ($posts as $post) :  setup_postdata($post); ?>
    <tr>
        <td><a href="<?php the_permalink(); ?>" title="<?php the_title();?>" target="_blank"> <?php the_title(); ?></a></td>
        <td><?php the_date(); ?></td>
    </tr>
<?php endforeach; ?>
    </tbody>
</table>

The above will produce an un-stylized output like the following:

To integrate with your other website, perform the following:

In the <head> element, add the following:

<script>
    <!--
    $(document).ready(function(){
        $( "#posts" ).load( "<LINK_TO_POSTS_PHP>", function(response, status, xhr){
            if (status == "error") {
                alert(msg + xhr.status + " " + xhr.statusText);
            }
         });
    });
    -->
</script>

Where <LINK_TO_POSTS_PHP> is the fully qualified link to the posts.php you created earlier.

Next, create a <div> element with the id of “posts”.

<div id="posts"><div>

If your sites are in different domains, you may get a message similar to the following in the JavaScript console:

XMLHttpRequest cannot load http://<BLOG_SITE_DOMAIN>/posts.php. No 
'Access-Control-Allow-Origin' header is present on the requested 
resource. Origin 'http://<SITE_ON_DIFFERENT_DOMAIN>' is therefore not allowed access.

Where <BLOG_SITE_DOMAIN> is the site for your WordPress blog and <SITE_ON_DIFFERENT_DOMAIN> is the site you trying to call http://<BLOG_SITE_DOMAIN>/posts.php from.

The reason for this is it is seen a a XSS (cross-site-scripting) attack. To work-around the above, add the following to the beginning of posts.php:

<?php header('Access-Control-Allow-Origin: http://<SITE_ON_DIFFERENT_DOMAIN>'); ?>

Whatever you do, DO NOT put * where http://<SITE_ON_DIFFERENT_DOMAIN> is.; that’s just asking for trouble.

The end result should look as follows:

And that’s all I have to say about that.