Shortcode example: Seasons

Creating a shortcode is a simple way to add custom functionality to your WordPress site. Edit the functions.php file in your child theme, either through Appearance > Theme Editor in the WordPress admin area, or by editing the file on your server.

Here’s an example of a way to display the current season:

function function_season( $atts ) {
	$a = shortcode_atts( array(
		'case' => 'title'
	), $atts );

	$month = date('m');
	$season = '';
	switch ($month) {
    	case 1:
    	case 11:
    	case 12:
        	$season = "winter";
    	case 2:
    	case 3:
    	case 4:
        	$season = "spring";
    	case 5:
    	case 6:
    	case 7:
        	$season = "summer";
    	case 8:
    	case 9:
    	case 10:
        	$season = "fall";
    if ($a['case'] == 'title') {
		$season = ucwords($season);
	} else if ($a['case'] == 'upper') {
		$season = strtoupper($season);	

	return $season;    
add_shortcode( 'season', 'function_season' );

Implementing a shortcode takes two steps. The first is the function that does the work and returns a formatted value for the season. The second is the add_shortcode() command that describes the name of the shortcode (season) and which function is called when it is used (function_season).

Examples of ways to use this shortcode:

// Displays the season (ie. "Summer")

// Displays the season in upper case (ie. "SUMMER")
[season case="upper"]

Go ahead and give it a try. Take my code and use it as is or customize it to your own use. If you live in the Southern hemisphere, or live in a different climate, adjust the months for any given season. You can also add more options for the “case” parameter, or do something completely new. The sky is the limit with shortcodes.