Timed Announcement Shortcode for WordPress

Here is a simple shortcode that can be put in the functions.php file from the active WordPress theme. In the shortcode arguments you can specify starting time, ending time, title and title tag as well as the background colour and padding values for the div element that envelopes the announcment content.

Of course you are free to modify anything as needed.

function timed_announcement_shortcode($args = array(), $content = '') {
	extract(shortcode_atts(
		array(
			'until' 	=> 'tomorrow',
			'from'  	=> 'yesterday',
			'title' 	=> __('Announcement','your_theme_textdomain'),
			'titletag' 	=> 'h3',
			'background' 	=> 'lightyellow',
			'padding' 	=> '20px',
		),
		$args
	));
	if ( strtotime($from) < time() && strtotime($until) > time()) {

		return '
< ' . $titletag . '>' . $title . '

' . $content . '

'; } return ''; } add_shortcode('announcement', 'timed_announcement_shortcode');

Usage examples (remove the * character for the shortcode to work):

[*announcement]This is some announcement with a H3 title as "Announcement" or its translation in another language[/announcement]

Result:[announcement]This is some announcement with a H3 title as “Announcement” or its translation in another language[/announcement]

[*announcement background="#ddd"]This is some announcement with a H3 title as "Announcement" or its translation in another language and with light gray background[/announcement]

Result:[announcement background=”#ddd”]This is some announcement with a H3 title as “Announcement” or its translation in another language and with light gray background[/announcement]

[*announcement padding="10px"]This is some announcement with a H3 title as "Announcement" or its translation in another language and with only 10px padding instead of the default 20px[/announcement]

Result:[announcement padding=”10px”]This is some announcement with a H3 title as “Announcement” or its translation in another language and with only 10px padding instead of the default 20px[/announcement]

[*announcement until="2011-08-01"]This is some announcement with a H3 title as "Announcement" or its translation in another language that will expire August 1st, 2011[/announcement]

Result:[announcement until=”2011-08-01″]This is some announcement with a H3 title as “Announcement” or its translation in another language that will expire August 1st, 2011[/announcement]

[*announcement from="2011-07-31" until="2011-08-01"]This is some announcement with a H3 title as "Announcement" or its translation in another language that will appear July 31st, 2011 and will expire August 1st, 2011[/announcement]

Result:[announcement from=”2011-07-31″ until=”2011-08-01″]This is some announcement with a H3 title as “Announcement” or its translation in another language that will appear July 31st, 2011 and will expire August 1st, 2011[/announcement]

[*announcement title="Some Title"]This is some announcement with a H3 title as "Some Title" instead of the default title[/announcement]

Result:[announcement title=”Some Title”]This is some announcement with a H3 title as “Some Title” instead of the default title[/announcement]

[*announcement titletag="h6"]This is some announcement with a H6 title as "Announcement" or its translation in another language[/announcement]

Result:[announcement titletag=”h6″]This is some announcement with a H6 title as “Announcement” or its translation in another language[/announcement]
Of course all shortcode arguments could be used together.

Enjoy!

By Richard Vencu

http://www.linkedin.com/in/richardvencu

1 comment

  1. Of course because we use background color argument, then we can also add color argument to change the color of the text inside the div element…

Leave a comment

Your email address will not be published.