Tech

Hugo Shortcodes: A Practical Guide With Examples

Creating a website with Hugo becomes much easier when you understand how shortcodes work. While Markdown is excellent for writing articles and documentation, it has limitations when adding videos, buttons, image galleries, and other custom elements. Hugo shortcodes solve this problem by allowing content creators to insert reusable components without writing extensive HTML.

From simple blogs to large documentation websites, shortcodes help developers maintain consistent designs, reduce repetitive coding, and manage content more efficiently.

What Are Hugo Shortcodes?

Hugo shortcodes are reusable template snippets that allow users to insert custom elements directly into content files. They extend Markdown functionality and generate formatted output during the website build process.

Instead of manually writing HTML whenever you need a video, button, or notification box, you can insert a short command. Hugo processes the corresponding template and generates the required output automatically.

How Do Hugo Shortcodes Work?

Hugo connects shortcode commands within content files to their corresponding templates. When building a website, the static site generator identifies these commands, processes their parameters, and produces the appropriate output.

For example, a YouTube shortcode generates the markup required to display an embedded video. This process keeps content separate from presentation, making website development and maintenance more manageable.

Types of Hugo Shortcodes

Hugo supports three main types of shortcodes, each serving a different purpose:

  • Embedded shortcodes: Predefined components included with Hugo for common content requirements.
  • Custom shortcodes: Developer-created templates designed for specific website features.
  • Inline shortcodes: Templates defined directly within content files, which are disabled by default for security reasons.

Embedded shortcodes are particularly convenient for beginners, while custom shortcodes offer greater flexibility for experienced developers.

Understanding Shortcode Syntax

Hugo supports two primary shortcode notations. The standard notation uses angle brackets:

{{< shortcode-name >}}

The Markdown notation uses percentage symbols:

{{% shortcode-name %}}

Standard notation is commonly used for components that generate HTML. Markdown notation allows shortcode output to participate in Markdown processing.

Shortcodes can also accept named or positional parameters, allowing users to customize individual components without modifying their underlying templates.

Common Hugo Shortcodes

Hugo includes several embedded shortcodes that make content creation more convenient.

ShortcodeFunction
youtubeEmbeds YouTube videos
vimeoDisplays Vimeo videos
figureAdds images with captions
instagramEmbeds Instagram posts
highlightHighlights programming code
detailsCreates expandable content
qrGenerates QR codes
paramRetrieves parameter values
refGenerates absolute page links
relrefGenerates relative page links

Available shortcodes and their supported parameters may vary depending on the installed Hugo version.

Practical Shortcode Examples

One of the simplest applications is embedding a YouTube video:

{{< youtube w7Ft2ymGmfc >}}

Similarly, the figure shortcode allows authors to display images with descriptive captions:

{{< figure src="/images/nature.jpg"
alt="Mountain landscape"
caption="A beautiful mountain view" >}}

Developers can also display syntax-highlighted programming code:

{{< highlight python >}}
print("Hello World")
{{< /highlight >}}

These examples demonstrate how shortcodes reduce repetitive markup while keeping content files clean and readable.

Creating Custom Hugo Shortcodes

Custom shortcodes allow developers to introduce features that are not included in Hugo by default. To create a shortcode in recent Hugo versions, navigate to the layouts/_shortcodes/ directory and create a file named button.html.

Add the following template:

<a href="{{ .Get "url" }}" class="custom-button">
  {{ .Get "text" }}
</a>

You can then use the shortcode inside a Markdown file:

{{< button url="/about/" text="Learn More" >}}

Adding CSS to your website’s stylesheet allows you to customize the button’s appearance. Older Hugo projects may use the layouts/shortcodes/ directory instead.

Useful Custom Shortcodes

The open-source Hugo shortcodes collection maintained by Squidfingers demonstrates several practical components for content-focused websites.

These include:

  • Accordion: Organizes expandable content sections.
  • Alert: Displays warnings and important information.
  • Button: Creates customizable call-to-action links.
  • Icon: Inserts SVG icons into content.
  • Img: Supports additional image attributes and processing.
  • Tabpane: Organizes related information into tabs.
  • Include: Inserts content from external files within the project.
  • Command-output: Separates terminal commands from their output.

These components require the corresponding custom templates and are not all included in Hugo by default. The referenced collection requires Hugo v0.147.0 or later.

Shortcodes and Render Hooks

Although shortcodes and render hooks both customize website content, they serve different purposes. Shortcodes require authors to insert specific commands, whereas render hooks automatically control how supported Markdown elements are converted into HTML.

For example, image render hooks can customize image markup, while heading render hooks can add anchor links. Combining both features gives developers greater control over content presentation.

Benefits of Hugo Shortcodes

One of the biggest advantages of Hugo shortcodes is code reusability. Developers can create a component once and use it across multiple pages without duplicating its underlying HTML.

Shortcodes also improve design consistency, simplify website maintenance, and reduce clutter within Markdown files. Updating a central template applies those changes throughout the website after rebuilding it.

Moreover, content creators can manage advanced elements without regularly editing template files.

Hugo Shortcodes and SEO

Properly implemented shortcodes can support SEO by generating structured HTML, descriptive image attributes, accessible components, and consistent internal links.

Since Hugo generates static pages, shortcode-generated HTML is generally available to search engines. However, excessive third-party embeds and unnecessary scripts can affect website performance.

Developers should prioritize lightweight components, responsive layouts, optimized images, and accessible markup rather than relying on shortcodes alone to improve rankings.

Hugo vs WordPress Shortcodes

FeatureHugoWordPress
Template languageGo TemplatesPHP
ProcessingBuild timeTypically request time
DatabaseNot requiredRequired
Content formatMarkdownWordPress editor
CustomizationTemplate-basedPlugin or code-based

Both systems simplify reusable content creation, although they operate within different website architectures.

Common Errors and Fixes

Shortcode errors often result from incorrect template locations, missing parameters, spelling mistakes, or improperly closed tags.

When encountering an error, check the template filename, shortcode syntax, and installed Hugo version. Running hugo server locally helps identify build problems and preview components before publication.

Developers should also test custom shortcodes across different screen sizes and avoid enabling inline shortcodes for untrusted content authors.

Best Practices

Keep shortcode names short and descriptive. Validate required parameters, use sensible default values, and separate CSS from template logic wherever possible.

Additionally, avoid creating unnecessarily complex components when standard Markdown can accomplish the same task. Regularly reviewing templates for compatibility, accessibility, and performance helps maintain a reliable website.

Conclusion

Hugo shortcodes offer a practical solution for extending Markdown and building reusable website components. Whether you need embedded videos, notification boxes, customizable buttons, or advanced content layouts, shortcodes make these features easier to implement and maintain.

By combining built-in and custom shortcodes with sensible development practices, you can create a well-organized, flexible, and efficient Hugo website.

FAQs

What are Hugo shortcodes used for?

Hugo shortcodes allow users to add videos, images, buttons, and custom content elements to Markdown files without writing repetitive HTML code.

How do I create a custom shortcode in Hugo?

Create an HTML template inside the layouts/_shortcodes/ directory, define its functionality using Go templates, and call the shortcode within your Markdown content.

What is the difference between Hugo shortcodes and render hooks?

Shortcodes require specific commands within content files, while render hooks automatically customize how standard Markdown elements, such as images, headings, and links, appear on a website.

Can Hugo shortcodes be nested?

Yes, regular Hugo shortcodes support nesting. This allows developers to combine components, such as expandable sections, accordions, and tabbed content, within a single layout.

Why are my Hugo shortcodes not working?

Common reasons include incorrect syntax, missing template files, invalid parameters, and version compatibility issues. Check Hugo’s build logs to identify and resolve the problem.

Must Read: Oracle Max Length of VARCHAR in SQL and PL/SQL

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button