Monday, 26 Oct 2026

Exclusive resources and tools for web design and development experts

Explore
BiblioWeb

Biblioweb.es

All About Web Technology

  • Start
  • WordPress
    WordPress
    • All about WordPress26
    • Plugins de WordPress16
    • Desarrollo web y plugins15
    Rendimiento y seguridad
    • Hosting y rendimiento8
    • Ciberseguridad web7
    • Solución de errores5
    Negocio e IA
    • IA y automatización10
    • WooCommerce y tiendas online6
    Lo último
    Restaurant Menu Widget — WordPress Plugin27 Sep 2026JetStudio Bookings — WordPress Plugin27 Sep 2026SC Heatmap — WordPress Plugin27 Sep 2026
    Ver el blog completoHistorial de noticiasAbout usNewsletter semanal
  • Plugins de WordPress
    Fichas de plugins
    • WP Translate Master
    • Cart Tool Kit
    • SC Heatmap
    • JetStudio Bookings
    • Restaurant Menu Widget
    • WP SEO Pro
    • Rank Math SEO in 2026
    • WP Rocket
    • WPCode
    • AI Engine para WordPress
    Comparativas y guías
    • The best SEO plugins for WordPress
    • The best AI plugins for WordPress
    • The best booking plugins for WordPress
    • WordPress multiidioma
    • Cómo crear un formulario de contacto en WordPress
    • Stripe o PayPal
    Por temática
    • Plugins de WordPress16
    • WooCommerce y tiendas online6
    • Desarrollo web y plugins15
    • IA y automatización10
    • Hosting y rendimiento8
    BibliotecaPlugin Library

    Los plugins de WordPress que desarrollamos: qué hace cada uno, cómo se ve en acción y dónde conseguirlo.

    Entrar en la Pluginteca
    Toda la categoría Plugins de WordPressHerramientas gratis
  • Tools
    Herramientas gratis, sin registro y desde el navegador
    Generador de contraseñasContraseñas largas y aleatorias sin salir del navegador.Generador de QRCódigos QR en PNG de alta resolución, sin caducidad.Contador de palabrasPalabras, caracteres y tiempo de lectura en vivo.JSON FormatterFormatea, valida y localiza el error exacto.WebP ConverterPNG, JPG y PDF a WebP para que la web cargue…PDF a WebPCada página del PDF, en una imagen ligera.Comprobador de emailsSintaxis, correos temporales y registros MX.Compresor de vídeoBaja el peso del vídeo sin subirlo a ningún servidor.Vídeo a MP3Extrae el audio de un vídeo en un par de clics.Firmas de emailFirma en HTML lista para Gmail y Outlook.
    También te sirveHerramientas externas que merecen la pena

    DNS Checker, Query Monitor, Local WP y el resto del kit que usamos a diario, con para qué sirve cada una.

    Ver la selección
    Todas las herramientasPlugin LibraryRecíbelas por correo
  • Plugin Library
  • Español
  • English ✓
  • 🇨🇳 中文(简体)
  • Français
  • Deutsch
  • Italiano
Newsletter
  • WordPress
  • Plugins de WordPress
  • WooCommerce
  • IA para WordPress
  • Security
  • Herramientas gratis
  • See everything
BiblioWebBiblioWeb
Font ResizerAa
  • WordPress
  • Plugins
  • WooCommerce
  • Errores
  • Tools
Search
  • Start
  • WordPress
    • All about WordPress
    • WordPress Plugins
    • Web Development and Plugins
    • AI and Web Automation
    • WooCommerce
    • Hosting and Web Performance
    • Cybersecurity and Web Security
    • Web Error Resolution
  • Plugins de WordPress
    • Rank Math SEO
    • AI Engine
    • WP Rocket
    • WPCode
    • WP Translate Master
    • Cart Tool Kit
    • SC Heatmap
    • JetStudio Bookings
    • Restaurant Menu Widget
    • WP SEO Pro
  • Tools
    • Password Generator
    • QR Code Generator
    • Word Counter
    • JSON Formatter
    • Image to WebP Converter
    • Email Checker
    • Video Compressor for Web
    • Email Signature Generator
    • Video to MP3 Converter
    • PDF to WebP Converter
  • Plugin Library
  • Blog
    • Español
    • English ✓
    • 🇨🇳 中文(简体)
    • Français
    • Deutsch
    • Italiano
Síguenos
© 2026 BiblioWeb — All rights reserved.
Cover » Blog » WordPress Shortcodes: what they are and how to create your own
Web Development and Plugins

WordPress Shortcodes: what they are and how to create your own

Adrián Alcalá
Actualizado: 08/08/2026 21:10
By Adrián Alcalá
Share
14 min de lectura
WordPress Shortcodes: what they are and how to create them
SHARE

You write [galeria] in a post, you publish, and in its place, a complete image gallery appears. That little magic is WordPress shortcodes: text shortcuts enclosed in square brackets that the system replaces with dynamic content generated with PHP. They have been in WordPress since version 2.5 and, although the block editor has taken over some of their territory, they remain an enormously practical tool: they are used by form plugins, pricing tables, booking systems, and thousands of themes. And the best part is that creating your own is surprisingly simple. In this guide, we will see what WordPress shortcodes are and how they work internally, how to use the ones you already have installed, and above all, how to program your own shortcodes step by step: simple, with attributes, with nested content, and even with template output. All with complete and functional PHP code that you can copy, adapt, and use today.

Índice
What WordPress shortcodes are and how they workHow to use shortcodes in posts, pages, and widgetsHow to create your first shortcode step by stepShortcodes with attributes: parameters that change the outputShortcodes with content and nested shortcodesA complete example: recent posts list with cacheBest practices and errors to avoidFrequently Asked QuestionsConclusion

What WordPress shortcodes are and how they work

A shortcode is a short tag enclosed in square brackets, like [contact-form-7] o [woocommerce_cart], which WordPress searches for in the content just before displaying it. When found, it executes the associated PHP function and replaces the tag with whatever that function returns. The process occurs on each page load, on the filter the_content, so the text saved in the database remains [galeria]: the substitution is always dynamic.

There are three ways to write a shortcode:

  • Simple: [mi_shortcode] — is replaced as is.
  • With attributes: [mi_shortcode color="rojo" numero="5"] — receives parameters that modify the output.
  • With content: [aviso]Texto interior[/aviso] — wraps content that the function can process.

WordPress includes several by default:

,

,

You Might Also Like

Cloud development environments for WordPress
What is a child theme in WordPress and how to create it
WordPress without coding: what you can build with no-code
Custom Post Types in WordPress: complete guide

,

o . Plugins add their own: each Contact Form 7 form, for example, generates a unique shortcode that you paste wherever you want to display it. If a shortcode appears as plain text on your website (brackets included), it almost always means that the plugin that interpreted it is deactivated.

How to use shortcodes in posts, pages, and widgets

In the block editor (Gutenberg) there is a specific block called «Shortcode»: add it and paste the tag inside. In reality, they also work pasted into a paragraph block, but the dedicated block makes it clearer what is content and what is code. In the classic editor, simply type them into the text.

Outside the main content, you have two ways:

  1. Widgets: text and custom HTML widgets process shortcodes since WordPress 4.9 without the need for extra code.
  2. Template files: if you need to execute a shortcode within a theme's PHP file, use the function do_shortcode():
<?php
// En un archivo de plantilla del tema (por ejemplo, footer.php):
echo do_shortcode( '[mi_formulario id="3"]' );

It is advisable not to overuse do_shortcode() in templates: if you control the code, calling the shortcode's PHP function directly is faster. But as a one-off resource, it is perfectly valid.

How to create your first shortcode step by step

The entire API revolves around one function: add_shortcode(). It receives the tag name and the callback function that generates the output. The golden rule: the callback returns the content with return, never prints it with echo. If you echo, the content will appear out of place (usually at the beginning of the post).

Let's start with the most useful of all simple shortcodes: the current year, perfect for copyright notices that never become outdated:

<?php
function biblioweb_shortcode_anio() {
    return date_i18n( 'Y' );
}
add_shortcode( 'anio', 'biblioweb_shortcode_anio' );
// Uso: &copy; [anio] Mi Empresa

Registration must be done when WordPress is already loaded, so the correct way is to hook it to init. If you don't yet master the actions and filters system, we recommend strengthening that foundation first, because WordPress shortcodes rely directly on it:

<?php
function biblioweb_registrar_shortcodes() {
    add_shortcode( 'anio', 'biblioweb_shortcode_anio' );
    add_shortcode( 'aviso', 'biblioweb_shortcode_aviso' );
}
add_action( 'init', 'biblioweb_registrar_shortcodes' );

Where to place this code? The same three options as always: the functions.php of a child theme, a custom plugin, or, the most convenient way for most, a snippet manager like WPCode, the snippet manager we analyze in detail, which validates the PHP before saving it and allows activating and deactivating each shortcode individually.

Shortcodes with attributes: parameters that change the output

Attributes turn a fixed shortcode into a reusable piece. The callback receives them in its first parameter ($atts), and the function shortcode_atts() it is responsible for merging them with the default values, discarding any unforeseen attributes:

<?php
function biblioweb_shortcode_boton( $atts ) {
    $datos = shortcode_atts(
        array(
            'url'   => '#',
            'texto' => 'Más información',
            'color' => '#0057d9',
        ),
        $atts,
        'boton'
    );

    return sprintf(
        '<a class="bw-boton" href="%s" style="background:%s">%s</a>',
        esc_url( $datos['url'] ),
        esc_attr( $datos['color'] ),
        esc_html( $datos['texto'] )
    );
}
add_shortcode( 'boton', 'biblioweb_shortcode_boton' );
// Uso: [boton url="https://ejemplo.com" texto="Descargar guía" color="#1d9e75"]

Three important details that this example applies and that distinguish a professional shortcode from a fragile one:

  • Escaping the output: esc_url(), esc_attr() y esc_html() prevent a malicious attribute from injecting HTML or JavaScript.
  • Default values: the shortcode works even if the user doesn't pass any attributes.
  • Third parameter of shortcode_atts(): the shortcode name activates the filter shortcode_atts_boton, which allows other developers to modify the default values.

A practical note: WordPress converts attribute names to lowercase, so always use mi_atributo and not miAtributo when reading them in the callback.

Shortcodes with content and nested shortcodes

When the shortcode wraps text ([aviso]...[/aviso]), that content arrives at the callback as a second parameter. The classic use case is a highlighted alert box:

<?php
function biblioweb_shortcode_aviso( $atts, $content = null ) {
    $datos = shortcode_atts(
        array( 'tipo' => 'info' ),
        $atts,
        'aviso'
    );
    $tipos = array( 'info', 'exito', 'alerta' );
    $tipo  = in_array( $datos['tipo'], $tipos, true ) ? $datos['tipo'] : 'info';

    return '<div class="bw-aviso bw-aviso--' . esc_attr( $tipo ) . '">'
        . do_shortcode( wp_kses_post( $content ) )
        . '</div>';
}
add_shortcode( 'aviso', 'biblioweb_shortcode_aviso' );
// Uso: [aviso tipo="alerta"]Haz una copia de seguridad [anio] antes de continuar.[/aviso]

Two functions do the fine work: wp_kses_post() sanitizes the content, allowing only HTML specific to a post, and do_shortcode() applied over $content makes sure that nested shortcodes (like the [anio] of the example) are also processed. Without that call, the inner brackets would appear as is.

With the same technique, you can build more ambitious structures, such as columns ([fila][columna]...[/columna][/fila]) or tabs, although for pure layout, the block editor is usually a better tool today. The strong point of the shortcode remains dynamic logic: showing content only to registered users, rendering data from an API, listing related posts.

A complete example: recent posts list with cache

Let's close the technical part with a realistic shortcode that combines attributes, a database query, and a caching layer with transients to avoid penalizing performance:

<?php
function biblioweb_shortcode_recientes( $atts ) {
    $datos = shortcode_atts(
        array(
            'numero'    => 5,
            'categoria' => '',
        ),
        $atts,
        'recientes'
    );

    $clave = 'bw_recientes_' . md5( serialize( $datos ) );
    $html  = get_transient( $clave );
    if ( false !== $html ) {
        return $html;
    }

    $args = array(
        'posts_per_page'      => absint( $datos['numero'] ),
        'ignore_sticky_posts' => true,
        'no_found_rows'       => true,
    );
    if ( '' !== $datos['categoria'] ) {
        $args['category_name'] = sanitize_title( $datos['categoria'] );
    }

    $consulta = new WP_Query( $args );
    if ( ! $consulta->have_posts() ) {
        return '';
    }

    $html = '<ul class="bw-recientes">';
    while ( $consulta->have_posts() ) {
        $consulta->the_post();
        $html .= '<li><a href="' . esc_url( get_permalink() ) . '">'
            . esc_html( get_the_title() ) . '</a></li>';
    }
    $html .= '</ul>';
    wp_reset_postdata();

    set_transient( $clave, $html, HOUR_IN_SECONDS );
    return $html;
}
add_shortcode( 'recientes', 'biblioweb_shortcode_recientes' );
// Uso: [recientes numero="3" categoria="wordpress"]

This pattern (query, build HTML, cache for an hour) is applicable to almost any shortcode that makes queries. If one day the listing doesn't update immediately, remember that the transient expires in an hour or can be deleted when saving posts with an action on save_post.

Best practices and errors to avoid

  • Return, never echo. The number one error: the output appears at the beginning of the post instead of where you placed the shortcode.
  • Prefix the names. [boton] can clash with that of another plugin; [bw_boton] no. The last one add_shortcode() registered wins, and the conflict is silent.
  • Always escape and sanitize. Attributes and content are user input: esc_html(), esc_url(), esc_attr() y wp_kses_post() are your seatbelt.
  • Avoid heavy logic without caching. The shortcode runs on every page load; transients are your allies.
  • Don't delete the plugin without cleaning up. If you deactivate the plugin that registers a shortcode, the text with brackets will remain visible. Before removing shortcodes from a project, find and replace them.
  • Document the attributes. Six months from now you won't remember if it was numero o cantidad.

The complete API reference, with all the details of the parser and its limitations, is in the official shortcode documentation from developer.wordpress.org. And if you're putting together your plugin toolbox, our selection of essential plugins to get started with WordPress will help you cover the rest of the basics.

Frequently Asked Questions

Are shortcodes obsolete with the block editor?

No. Gutenberg has replaced their use for layout (columns, buttons, galleries), but WordPress shortcodes are still the standard mechanism for many plugins and the fastest way to insert reusable dynamic logic. In fact, the editor includes a specific block for them, and converting a shortcode into a native block is a considerably more complex project.

Why does my shortcode appear as text with brackets?

Three common causes: the plugin or snippet that registers it is deactivated, the name is misspelled (names distinguish hyphens and underscores), or you have placed it in an area where shortcodes are not processed, such as some old widget fields or titles. First, check that the registration code is executing.

Can I use a shortcode inside another?

Yes, as long as the outer shortcode processes its content with do_shortcode( $content ) in its callback, as we saw in the alert box example. With a parser limitation: two shortcodes with the same name cannot be nested within each other.

Shortcode or custom block: which should I create?

If you need a quick, reusable solution that also works in widgets, templates, or the classic editor, the shortcode wins for simplicity: a PHP function and you're done. If you're looking for a visual experience in the editor, with real-time preview and graphical controls, the native block is superior, at the cost of requiring JavaScript and a build process.

Conclusion

WordPress shortcodes are the most direct path between a need ("I want to show this here") and a dynamic PHP solution: a tag in brackets, a function that returns HTML, and the system does the rest. We've seen how to use them in posts, widgets, and templates, how to create them from scratch with add_shortcode(), how to make them flexible with attributes and default values, how to safely process nested content, and how to cache their output so they don't impact performance. With the patterns in this guide (return instead of echo, systematic escaping, name prefixes, and transients for queries) you have everything you need to build solid and professional shortcodes. Start with the simplest one, the copyright year, and work your way up: in no time you'll have your own library of reusable utilities for any project.

Share This Article
Email Copiar enlace Print
ByAdrián Alcalá
Síguele:
Adrián Alcalá es desarrollador WordPress y consultor SEO, autor del plugin WP Translate y creador de BiblioWeb. Desde aquí comparte guías, tutoriales y herramientas gratuitas para quienes crean, optimizan y posicionan páginas web. Lleva años ayudando a negocios de toda España a sacar el máximo partido a WordPress, y todo lo que publica está probado en proyectos reales.
Previous Article WordPress Hooks: actions and filters explained WordPress Hooks: what actions and filters are
Next Article Custom Post Types in WordPress: complete guide Custom Post Types in WordPress: complete guide
Ilustración del plugin Restaurant Menu Widget: carta de restaurante con platos y precios editables en Elementor
Restaurant Menu Widget — WordPress Plugin
WordPress Plugins
Ilustración del plugin JetStudio Bookings: formulario de citas por pasos con calendario y bandeja de solicitudes
JetStudio Bookings — WordPress Plugin
WordPress Plugins
Ilustración del plugin SC Heatmap: mapa de calor de clics y scroll sobre una web, con los datos en tu servidor
SC Heatmap — WordPress Plugin
WordPress Plugins
Accesibilidad web en WordPress con IA
Accesibilidad web en WordPress: auditarla con IA en una tarde
AI & Web Automation

También te puede interesar

Design WordPress Website - Design your PRO Website in WordPress: The Perfect Theme for a Brutal Impact (and AI tricks that will blow your mind
Web Development and Plugins

How to choose the perfect theme to design your website in WordPress

28/09/2026
WordPress Hooks: actions and filters explained
Web Development and Plugins

WordPress Hooks: what actions and filters are

08/08/2026
How to create a WordPress plugin from scratch
Web Development and Plugins

How to create a WordPress plugin from scratch

28/09/2026
What is Elementor and how to use it to design pages
Web Development and Plugins

What is Elementor and how to use it: a getting started guide

28/09/2026
Show More
  • Temas:
  • WordPress
  • WordPress Plugins
  • Web Development and Plugins
  • AI & Web Automation
  • Web Hosting & Performance
  • Cybersecurity and Web Security
  • WooCommerce
  • Troubleshooting web errors
BiblioWeb

Biblioweb.es

Web Technology News

Information you can trust: practical web technology guides and solutions — WordPress, plugins, cybersecurity, hosting, performance, and error troubleshooting.

RSS

Enlaces de interés

  • About us
  • Contact
  • Free tools
  • Plugin Library

Recibe nuestras nuevas guías de WordPress en tu correo. Suscríbete a la newsletter.

Legal

  • Legal Notice
  • Terms and Conditions
  • Cookies Policy
  • Privacy Policy

Últimas novedades

Ilustración del plugin Restaurant Menu Widget: carta de restaurante con platos y precios editables en Elementor

Restaurant Menu Widget — WordPress Plugin

27/09/2026
Continue reading
Ilustración del plugin JetStudio Bookings: formulario de citas por pasos con calendario y bandeja de solicitudes

JetStudio Bookings — WordPress Plugin

27/09/2026
Continue reading

© 2026 BiblioWeb — All rights reserved.

Gestionar consentimiento
Utilizamos tecnologías como las cookies para almacenar y/o acceder a la información del dispositivo. Lo hacemos para mejorar la experiencia de navegación y para mostrar anuncios (no) personalizados. El consentimiento a estas tecnologías nos permitirá procesar datos como el comportamiento de navegación o los ID's únicos en este sitio. No consentir o retirar el consentimiento, puede afectar negativamente a ciertas características y funciones.
Funcional Always active
El almacenamiento o acceso técnico es estrictamente necesario para el propósito legítimo de permitir el uso de un servicio específico explícitamente solicitado por el abonado o usuario, o con el único propósito de llevar a cabo la transmisión de una comunicación a través de una red de comunicaciones electrónicas.
Preferences
El almacenamiento o acceso técnico es necesario para la finalidad legítima de almacenar preferencias no solicitadas por el abonado o usuario.
Estadísticas
El almacenamiento o acceso técnico que es utilizado exclusivamente con fines estadísticos. El almacenamiento o acceso técnico que se utiliza exclusivamente con fines estadísticos anónimos. Sin un requerimiento, el cumplimiento voluntario por parte de tu proveedor de servicios de Internet, o los registros adicionales de un tercero, la información almacenada o recuperada sólo para este propósito no se puede utilizar para identificarte.
Marketing
El almacenamiento o acceso técnico es necesario para crear perfiles de usuario para enviar publicidad, o para rastrear al usuario en una web o en varias web con fines de marketing similares.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Ver preferencias
  • {title}
  • {title}
  • {title}
Accede a BiblioWeb
Usuario o correo electrónico
Contraseña

¿Has olvidado tu contraseña?