Skip to content
+91 89607 57615
Code Craft — web development and digital marketing
Lesson 828 min

Your first plugin: a shortcode

Write a plugin that shows the bakery's opening hours and whether it's open right now.

Taught by Sandesh Sahetiya

The video is part of the full course

In this lesson you will

  • Write a valid plugin header
  • Register a shortcode that returns HTML
  • Use the site's time zone
  • Escape output safely

A plugin is just a PHP file (or a folder of them) in wp-content/plugins with a header comment. This one adds a [sunrise_hours] shortcode that lists the opening hours and says whether the bakery is open now.

wp-content/plugins/sunrise-hours/sunrise-hours.phpDownload
<?php
/**
 * Plugin Name:       Sunrise Opening Hours
 * Description:       Shows the bakery's opening hours and whether it's open now, with the [sunrise_hours] shortcode.
 * Version:           1.0.0
 * Requires at least: 6.7
 * Requires PHP:      7.4
 * Author:            Sunrise Bakery
 * License:           GPL-2.0-or-later
 * Text Domain:       sunrise-hours
 *
 * @package SunriseHours
 */

// Stop the file being run directly from a browser.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Opening hours: day number (0 = Sunday) => array( opens, closes ) in 24-hour time.
 * Days not listed are closed.
 */
function sunrise_hours_schedule() {
	$schedule = array(
		1 => array( '07:00', '20:00' ),
		2 => array( '07:00', '20:00' ),
		3 => array( '07:00', '20:00' ),
		4 => array( '07:00', '20:00' ),
		5 => array( '07:00', '20:00' ),
		6 => array( '07:00', '20:00' ),
	);
	// Let other code (or a child theme) change the hours without editing this plugin.
	return apply_filters( 'sunrise_hours_schedule', $schedule );
}

/**
 * Is the bakery open at this moment? Uses the site's time zone (Settings > General).
 *
 * @param DateTimeImmutable|null $now Time to check; defaults to now.
 * @return bool
 */
function sunrise_hours_is_open( $now = null ) {
	$now      = $now ? $now : new DateTimeImmutable( 'now', wp_timezone() );
	$schedule = sunrise_hours_schedule();
	$day      = (int) $now->format( 'w' );
	if ( empty( $schedule[ $day ] ) ) {
		return false;
	}
	$time = $now->format( 'H:i' );
	return $time >= $schedule[ $day ][0] && $time < $schedule[ $day ][1];
}

/**
 * [sunrise_hours] shortcode handler. Shortcodes must return their HTML, not echo it.
 *
 * @param array $atts Shortcode attributes, e.g. [sunrise_hours title="When to visit"].
 * @return string
 */
function sunrise_hours_shortcode( $atts ) {
	$atts = shortcode_atts(
		array( 'title' => __( 'Opening hours', 'sunrise-hours' ) ),
		$atts,
		'sunrise_hours'
	);

	$days     = array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' );
	$schedule = sunrise_hours_schedule();
	$open     = sunrise_hours_is_open();

	$html  = '<div class="sunrise-hours">';
	$html .= '<h3>' . esc_html( $atts['title'] ) . '</h3>';
	$html .= '<p class="sunrise-hours__status">' . esc_html( $open ? __( 'Open now', 'sunrise-hours' ) : __( 'Closed right now', 'sunrise-hours' ) ) . '</p>';
	$html .= '<ul>';
	foreach ( array( 1, 2, 3, 4, 5, 6, 0 ) as $d ) {
		$hours = empty( $schedule[ $d ] ) ? __( 'Closed', 'sunrise-hours' ) : $schedule[ $d ][0] . '–' . $schedule[ $d ][1];
		$html .= '<li>' . esc_html( $days[ $d ] . ': ' . $hours ) . '</li>';
	}
	$html .= '</ul></div>';

	return $html;
}

add_action(
	'init',
	function () {
		add_shortcode( 'sunrise_hours', 'sunrise_hours_shortcode' );
	}
);

Only Plugin Name is required. Description, Version, Requires at least, Requires PHP, Author, License and Text Domain are optional but good practice, and wordpress.org requires several of them for plugins listed there.

Continue with the full course

The rest of this lesson, the narrated video, the PDF and the exercise are part of WordPress and WooCommerce. One payment, lifetime access.

Already bought it? Sign in with the email you paid with.