Chris K
was kind enough to review the tweaks I had made to his original
Updated Today plugin
. He endorsed the removal of
pngfix.js
but complained that my use of
bloginfo()
caused the new plugin to fail on his WordPress install. He also revealed his intent to make certain features of the plugin available to an admin page, requiring that portions of the output be salted away in variables for future manipulation. I was also dissatisfied with the amount of repetition in my otherwise very short plugin.
Here is the new code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /* config variables (later to be made available to an admin menu) */ $UTL['element'] = 'div'; $UTL['elementId'] = 'updated'; $UTL['imageUrl'] = get_bloginfo('url') . '/wp-content/plugins/updatedtodaylite/updatedbanner.png'; $UTL['style'] = 'position: absolute; display: block; top: 0px; left: 0px; height: 120px; width: 120px; z-index: 99;'; /* set up the action */ function showUpdateBanner() { global $UTL; $today = date("Y-m-d"); $query = "SELECT post_date, id FROM wp_posts WHERE wp_posts.post_date LIKE '" . $today . "%'"; //will need to sort by date/time to enable the element to be a link to the most recet content $results = mysql_query($query); if (mysql_num_rows($results) > 0) { $msieRegex = '/msie\s(5\.[5-9]|[6]\.[0-9]*).*(win)/i'; if( isset($_SERVER['HTTP_USER_AGENT']) && preg_match($msieRegex,$_SERVER['HTTP_USER_AGENT']) && !preg_match('/opera/i',$_SERVER['HTTP_USER_AGENT']) ) { $UTL['style'] .= " filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src=\"$UTL[imageUrl]\")"; } else { $UTL['style'] .= " background-image: url($UTL[imageUrl])"; } echo "<$UTL[element] id=\"$UTL[elementId]\" style=\"$UTL[style]\"></$UTL[element]>"; } } /* make it happen */ add_action('wp_footer', 'showUpdateBanner'); |
This has the following improvements over my previous attempt:
- You can choose what sort of element to wrap the image in.
- Uses
get_bloginfo()rather thanbloginfo()to prevent parsing errors - Exposes inline styles, element type, element ID, and image URL as variables
- These config variables are wrapped up in an array to prevent stepping on toes
-
USER_AGENTcondition adds only the appropriate style to the inline styles - Tolerant of an empty style variable, allowing folks to handle placement in a separate stylesheet if they like
- We only output any HTML once in the script
Still not much to it. Download the new plugin: Updated Today “Lite” v0.2
In the future, it would be nice to:
- Make this a clickable link to the most recent article, ideally on a page containing all recent content.
- Make it appear if the site has been updated in the past 23 hours or so, since I like to post at night, causing the banner to be visible for a only few hours at most.