Posts Tagged ‘functions.php’

15 September, 2013 — Development, Snippets, WordPress

Display A Message On Older Posts

When blog posts get old, sometimes the information in them is not as accurate as they were at the time of writing. This can be misleading and can cause confusion to the reader.

This simple bit of code when either placed in your themes funcions.php file or a functionality plugin will display a message at the top of any posts that are older than 1 year. You can customise the message in the code below and add some styles to your style.css file. Once done upload your file WordPress will automatically take care of the rest.

// display a message on older posts
function tb_older_post_message () {
    $posted = get_the_time('U');
    $current = current_time('timestamp');
    //Convert difference in seconds to days
    $diffTime = ($current - $posted) / (60*60*24);
    if($diffTime > 365){
        echo '<div class=older-post-message>' . __('This post was written more than a year ago and <em>might</em> not be entirely accurate anymore.', 'wptips') . '</div><br />';
    }
}
add_action('get_template_part_content','tb_older_post_message');
Widget Shortcode
29 May, 2013 — Snippets, WordPress

Use Shortcodes Within The WordPress Text Widget

For some reason I have yet to think of by default, WordPress shortcodes do not work within text widgets. Why I have no idea but if you need this functionality, it is quite easy to accomplish, simply follow the steps below.

  1. Log into your WordPress Dashboard
  2. Under “Appearance” click “Editor” (if you cant see this it might be disabled – if so contact the company who created your theme)
  3. In the list of files on the right of the page, click “functions.php”
  4. At the bottom of the functions.php file, add the following code:

add_filter( ‘widget_text’, ‘shortcode_unautop’); add_filter( ‘widget_text’, ‘do_shortcode’);

Click “Update File” and as if by magic your shortcodes should now work in your text widgets!

Although this way does work it might not be the best way of doing thing if your theme is regally updated or there is a chance your theme could be changed. If you use a theme from the third party that does get updates you might want to consider making yourself a simple functionality plugin.