How to Create a WordPress Shortcode To Display Business Hours in 5 Easy Steps Without Plugins

One of the most practical uses for a custom shortcode on a local business website is displaying the operating hours. This allows you to easily update and display your hours across multiple pages, ensuring your customers always know when you’re open. 

In this step-by-step guide, I’ll walk you through how to create a custom settings page in the WordPress admin dashboard to manage and display your operating hours using a custom shortcode – no plugins required. For an overall look on how to create a simple Shortcode. Visit this page:

How to Create a Custom Shortcode in WordPress.

Step 1: Add a Custom Settings Page for Operating Hours

To create a settings page in the WordPress admin dashboard:

  1. Add the Settings Page in functions.php:
    • First, add the following code to your theme’s functions.php file
				
					function add_operating_hours_menu() {
    add_menu_page(
        'Operating Hours Settings',  // Page title
        'Operating Hours',           // Menu title
        'manage_options',            // Capability
        'operating-hours',           // Menu slug
        'display_operating_hours_settings', // Callback function
        'dashicons-clock',           // Icon
        20                           // Position
    );
}
add_action('admin_menu', 'add_operating_hours_menu');

				
			

This code adds a new menu item called “Operating Hours” to the WordPress dashboard:

New Operating Hours Menu Item

Step 2: Display the Settings Page

Next, create the form that will appear on the settings page:

  1. Create the Callback Function to Display the Settings Page:
    • Add this code to functions.php:
				
					function display_operating_hours_settings() {
    ?>
    <div class="wrap">
        <h3>Operating Hours</h3>
        <form method="post" action="options.php">
            <?php
            settings_fields('operating_hours_group');
            do_settings_sections('operating-hours');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}
				
			

This function renders the settings page where you’ll manage the operating hours. But before we can see the settings, we first need to register them:

Step 3: Register and Define the Settings

To manage and store the operating hours, you need to register these settings:

Register the Settings in functions.php:Add the following code to register and display the settings fields:

				
					function register_operating_hours_settings() {
    $days_of_week = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
    
    foreach ($days_of_week as $day) {
        register_setting('operating_hours_group', $day . '_opening_time');
        register_setting('operating_hours_group', $day . '_closing_time');
        
        add_settings_section(
            $day . '_section',         // Section ID
            $day,                      // Section title
            null,                      // Callback
            'operating-hours'          // Page
        );
        
        add_settings_field(
            $day . '_opening_time',    // Field ID
            'Opening Time',            // Field title
            'display_time_input',      // Callback to display field
            'operating-hours',         // Page
            $day . '_section',         // Section
            array('day' => $day, 'type' => 'opening')
        );
        
        add_settings_field(
            $day . '_closing_time',    // Field ID
            'Closing Time',            // Field title
            'display_time_input',      // Callback to display field
            'operating-hours',         // Page
            $day . '_section',         // Section
            array('day' => $day, 'type' => 'closing')
        );
    }
}
add_action('admin_init', 'register_operating_hours_settings');

function display_time_input($args) {
    $option = get_option($args['day'] . '_' . $args['type'] . '_time');
    echo '<input type="time" name="' . $args['day'] . '_' . $args['type'] . '_time" value="' . esc_attr($option) . '" />';
}

				
			

This code registers the opening and closing times for each day of the week, which can be selected using a time picker. Now the Settings page will look like this:

 

Business Hours Setting page

Step 4: Display the Operating Hours with a Shortcode

Now, let’s create a shortcode to display the operating hours on the front end of your website:

  1. Add the Shortcode Function to functions.php:
    • Use the following code to create the shortcode:
				
					function display_operating_hours_shortcode() {
    $output = '<div class="operating-hours">';
    $days_of_week = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
    
    foreach ($days_of_week as $day) {
        $opening_time = get_option($day . '_opening_time');
        $closing_time = get_option($day . '_closing_time');
        
        if ($opening_time && $closing_time) {
            $output .= '<p><strong>' . $day . ':</strong> ' . $opening_time . ' - ' . $closing_time . '</p>';
        } else {
            $output .= '<p><strong>' . $day . ':</strong> Closed</p>';
        }
    }
    
    $output .= '</div>';
    return $output;
}
add_shortcode('business_hours', 'display_operating_hours_shortcode');

				
			

This function generates the HTML needed to display the operating hours using a simple shortcode.

Step 5: Use the Shortcode

  1. Insert [business_hours]:
    • Add the shortcode to any post, page, or widget where you want the hours to appear.

This is what it would look like on your website

Business Hours Used on website

Optional Step: Add the Settings Page as a Subitem in the Settings Menu

If you’d prefer the “Operating Hours” settings page to be a subitem under the “Settings” menu:

  1. Modify the add_menu_page Function:
    • Replace it with add_options_page:
				
					function add_operating_hours_menu() {
    add_options_page(
        'Operating Hours Settings',  // Page title
        'Operating Hours',           // Menu title
        'manage_options',            // Capability
        'operating-hours',           // Menu slug
        'display_operating_hours_settings' // Callback function
    );
}
add_action('admin_menu', 'add_operating_hours_menu');

				
			

This will nest the “Operating Hours” settings page under the “Settings” menu in the WordPress dashboard.

Picture of Jonas

Jonas

About the Author:
Jonas is the Co-Founder of LPagery, a yearlong WordPress enthusiast and expert with over 8 years of WordPress experience.

Table of Contents

Start Bulk Creating Pages in WordPress
Need to boost your sEO?
Start bulk creating Pages Today with LPagery!
Pro Feature

When to Use This Feature

Use Examples