USE VERSION 2.0, AS IT INCLUDES MAJOR IMPROVEMENTS TO SUPPORT PERMALINK STRUCTURE AND CSS SPECIFICITY.

Breadcrumbs Appia Antica is a server-rendered breadcrumb component for Etch that provides accessible, SEO-friendly breadcrumb navigation while maintaining a fully visual editing experience inside the Etch builder.

Unlike shortcode-based breadcrumb plugins, this component renders a visual placeholder in the builder and automatically replaces itself with the correct breadcrumb trail on the frontend.


Features

  • Fully server-rendered on the frontend
  • Live preview inside the Etch builder
  • Automatic Home, Post Type, Taxonomy, Parent Page, and Current Page support
  • Optional SVG Home icon
  • Optional SVG Separator icon
  • JSON-LD BreadcrumbList schema
  • Accessible HTML
  • Supports custom post types
  • Supports hierarchical pages
  • Supports hierarchical taxonomies
  • Configurable title truncation
  • Uses Etch properties rather than hard-coded settings

How it Works

The component outputs a lightweight marker in the frontend HTML.

Example:

<nav
    data-breadcrumbs-appia-antica-root
    data-baa-show-home="true"
    data-baa-taxonomy="category"
    ...
></nav>

During page rendering, the PHP renderer searches for this marker and replaces it with fully rendered breadcrumb markup.

This approach provides several advantages:

  • The builder always has a lightweight preview.
  • PHP has full access to WordPress conditional tags.
  • Breadcrumb logic stays entirely on the server.
  • No JavaScript is required.

Required PHP

<?php
/**
 * Breadcrumbs Appia Antica
 *
 * Frontend renderer for the Etch component marker:
 * data-breadcrumbs-appia-antica-root
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Boolean parser for Etch/WP-ish values.
 */
if (!function_exists('baa_bool')) {
    function baa_bool($value, bool $default = true): bool {
        if ($value === null || $value === '') {
            return $default;
        }

        $value = strtolower(trim((string) $value));

        if (in_array($value, ['true', '1', 'yes', 'on'], true)) {
            return true;
        }

        if (in_array($value, ['false', '0', 'no', 'off'], true)) {
            return false;
        }

        return $default;
    }
}

/**
 * UTF-8 safe truncation helper.
 */
if (!function_exists('baa_truncate')) {
    function baa_truncate(string $text, int $max, string $ellipsis = '…'): array {
        if ($max <= 0) {
            return [$text, false];
        }

        $has_mb = function_exists('mb_strlen') && function_exists('mb_substr');
        $len = $has_mb ? mb_strlen($text, 'UTF-8') : strlen($text);

        if ($len <= $max) {
            return [$text, false];
        }

        $ell_len = $has_mb ? mb_strlen($ellipsis, 'UTF-8') : strlen($ellipsis);
        $slice = max(0, $max - $ell_len);

        $cut = $has_mb
            ? mb_substr($text, 0, $slice, 'UTF-8')
            : substr($text, 0, $slice);

        return [rtrim($cut) . $ellipsis, true];
    }
}

/**
 * Safely inline an SVG selected in Etch.
 */
if (!function_exists('baa_inline_svg')) {
    function baa_inline_svg(string $url, string $class): string {
        $url = esc_url_raw(trim($url));

        if (!$url) {
            return '';
        }

        $path = strtolower((string) wp_parse_url($url, PHP_URL_PATH));

        if (substr($path, -4) !== '.svg') {
            return '';
        }

        $response = wp_safe_remote_get($url, [
            'timeout' => 5,
            'redirection' => 2,
        ]);

        if (is_wp_error($response)) {
            return '';
        }

        $code = (int) wp_remote_retrieve_response_code($response);

        if ($code < 200 || $code >= 300) {
            return '';
        }

        $svg = wp_remote_retrieve_body($response);

        if (!$svg || stripos($svg, '<svg') === false) {
            return '';
        }

        $svg = preg_replace('/<\?xml.*?\?>/is', '', $svg);
        $svg = preg_replace('/<!doctype.*?>/is', '', $svg);
        $svg = preg_replace('/<script\b[^>]*>.*?<\/script>/is', '', $svg);
        $svg = preg_replace('/<foreignObject\b[^>]*>.*?<\/foreignObject>/is', '', $svg);
        $svg = preg_replace('/\son\w+="[^"]*"/i', '', $svg);
        $svg = preg_replace("/\son\w+='[^']*'/i", '', $svg);
        $svg = preg_replace('/\s(?:href|xlink:href)=["\']\s*javascript:[^"\']*["\']/i', '', $svg);

        $class = sanitize_html_class($class);

        $svg = preg_replace(
            '/<svg\b([^>]*)>/i',
            '<svg$1 class="' . esc_attr($class) . '" aria-hidden="true" focusable="false">',
            $svg,
            1
        );

        return trim($svg);
    }
}

/**
 * Add post type archive crumb.
 */
if (!function_exists('baa_add_post_type_archive')) {
    function baa_add_post_type_archive(string $post_type, callable $add): void {
        $post_type = sanitize_key($post_type);

        if (!$post_type || $post_type === 'page') {
            return;
        }

        if ($post_type === 'post') {
            $page_for_posts = (int) get_option('page_for_posts');

            if ($page_for_posts) {
                $add(get_the_title($page_for_posts), get_permalink($page_for_posts));
            }

            return;
        }

        $post_type_object = get_post_type_object($post_type);
        $archive_url = get_post_type_archive_link($post_type);

        if (!$post_type_object || !$archive_url) {
            return;
        }

        $label = apply_filters(
            'baa_post_type_label',
            $post_type_object->labels->name,
            $post_type
        );

        $add((string) $label, $archive_url);
    }
}

/**
 * Resolve a taxonomy archive parent post type.
 */
if (!function_exists('baa_get_taxonomy_archive_post_type')) {
    function baa_get_taxonomy_archive_post_type(string $taxonomy): string {
        $taxonomy = sanitize_key($taxonomy);

        if (!$taxonomy || !taxonomy_exists($taxonomy)) {
            return '';
        }

        $taxonomy_object = get_taxonomy($taxonomy);

        if (!$taxonomy_object || empty($taxonomy_object->object_type)) {
            return '';
        }

        $post_types = array_values(array_filter((array) $taxonomy_object->object_type));

        if (!$post_types) {
            return '';
        }

        foreach ($post_types as $post_type) {
            $post_type = sanitize_key((string) $post_type);

            if ($post_type && $post_type !== 'post' && $post_type !== 'page' && get_post_type_archive_link($post_type)) {
                return $post_type;
            }
        }

        return sanitize_key((string) reset($post_types));
    }
}

/**
 * Add selected taxonomy term trail for singular content.
 */
if (!function_exists('baa_add_taxonomy_trail')) {
    function baa_add_taxonomy_trail(int $post_id, string $taxonomy, callable $add): void {
        $taxonomy = sanitize_key($taxonomy);

        if (!$taxonomy || !taxonomy_exists($taxonomy)) {
            return;
        }

        $terms = get_the_terms($post_id, $taxonomy);

        if (empty($terms) || is_wp_error($terms)) {
            return;
        }

        $term = reset($terms);

        if (!$term || is_wp_error($term)) {
            return;
        }

        if (!empty($term->parent)) {
            $parents = array_reverse(get_ancestors($term->term_id, $taxonomy, 'taxonomy'));

            foreach ($parents as $parent_id) {
                $parent = get_term($parent_id, $taxonomy);

                if ($parent && !is_wp_error($parent)) {
                    $add($parent->name, get_term_link($parent));
                }
            }
        }

        $add($term->name, get_term_link($term));
    }
}

/**
 * Render JSON-LD BreadcrumbList schema.
 */
if (!function_exists('baa_render_schema')) {
    function baa_render_schema(array $items): string {
        $schema_items = [];

        foreach ($items as $index => $item) {
            $schema_item = [
                '@type' => 'ListItem',
                'position' => $index + 1,
                'name' => wp_strip_all_tags((string) $item['label']),
            ];

            if (!empty($item['url'])) {
                $schema_item['item'] = esc_url_raw((string) $item['url']);
            }

            $schema_items[] = $schema_item;
        }

        if (!$schema_items) {
            return '';
        }

        $schema = [
            '@context' => 'https://schema.org',
            '@type' => 'BreadcrumbList',
            'itemListElement' => $schema_items,
        ];

        return '<script type="application/ld+json">' .
            wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) .
            '</script>';
    }
}

/**
 * Render label contents, including optional home SVG.
 */
if (!function_exists('baa_render_label_content')) {
    function baa_render_label_content(string $label, array $item, array $args): string {
        if (empty($item['is_home'])) {
            return esc_html($label);
        }

        $content = '';

        if (!empty($args['show_home_icon']) && !empty($args['home_icon_url'])) {
            $content .= baa_inline_svg((string) $args['home_icon_url'], 'baa__home-icon');
        }

        $content .= '<span class="baa__home-label">' . esc_html($label) . '</span>';

        return $content;
    }
}

/**
 * Render separator, including optional separator SVG.
 */
if (!function_exists('baa_render_separator')) {
    function baa_render_separator(array $args): string {
        if (!empty($args['show_separator_icon']) && !empty($args['separator_icon_url'])) {
            $svg = baa_inline_svg((string) $args['separator_icon_url'], 'baa__separator-icon');

            if ($svg) {
                return $svg;
            }
        }

        return esc_html((string) $args['separator']);
    }
}

/**
 * Render the final breadcrumbs.
 */
if (!function_exists('baa_render')) {
    function baa_render(array $args = []): string {
        $args = wp_parse_args($args, [
            'class' => '',
            'style' => '',
            'aria_label' => 'Breadcrumbs',
            'separator' => '/',
            'show_home' => true,
            'show_home_icon' => false,
            'home_icon_url' => '',
            'show_parents' => true,
            'show_post_type' => true,
            'taxonomy_archive_post_type' => '',
            'show_taxonomy' => false,
            'taxonomy' => '',
            'show_current' => true,
            'show_separator_icon' => false,
            'separator_icon_url' => '',
            'enable_schema' => true,
            'home_label' => __('Home', 'baa'),
            'current_title_max_length' => 0,
        ]);

        $items = [];

        $add = function (
            string $label,
            $url = null,
            bool $current = false,
            bool $is_current_post = false,
            bool $is_home = false
        ) use (&$items): void {
            if ($label === '') {
                return;
            }

            $items[] = [
                'label' => $label,
                'url' => $url,
                'current' => $current,
                'is_current_post' => $is_current_post,
                'is_home' => $is_home,
            ];
        };

        if (!empty($args['show_home'])) {
            $add(
                (string) $args['home_label'],
                is_front_page() ? null : home_url('/'),
                is_front_page(),
                false,
                true
            );
        }

        if (!is_front_page()) {
            if (is_singular()) {
                $post = get_queried_object();

                if (!$post || empty($post->ID)) {
                    return '';
                }

                $post_type = (string) get_post_type($post);

                if (!empty($args['show_post_type'])) {
                    baa_add_post_type_archive($post_type, $add);
                }

                if (
                    !empty($args['show_parents']) &&
                    is_post_type_hierarchical($post_type) &&
                    !empty($post->post_parent)
                ) {
                    foreach (array_reverse(get_post_ancestors($post->ID)) as $ancestor_id) {
                        $add(get_the_title($ancestor_id), get_permalink($ancestor_id));
                    }
                }

                if (!empty($args['show_taxonomy'])) {
                    baa_add_taxonomy_trail(
                        (int) $post->ID,
                        (string) $args['taxonomy'],
                        $add
                    );
                }

                if (!empty($args['show_current'])) {
                    $add(get_the_title($post->ID), null, true, true);
                }
            } elseif (is_post_type_archive()) {
                if (!empty($args['show_current'])) {
                    $post_type = get_query_var('post_type');

                    if (is_array($post_type)) {
                        $post_type = reset($post_type);
                    }

                    $post_type = sanitize_key((string) $post_type);
                    $label = post_type_archive_title('', false);

                    $add(
                        (string) apply_filters('baa_post_type_label', $label, $post_type),
                        null,
                        true
                    );
                }
            } elseif (is_tax() || is_category() || is_tag()) {
                $term = get_queried_object();

                if ($term && !is_wp_error($term)) {
                    if (!empty($args['show_post_type'])) {
                        $tax_post_type = sanitize_key((string) $args['taxonomy_archive_post_type']);

                        if (!$tax_post_type) {
                            $tax_post_type = baa_get_taxonomy_archive_post_type((string) $term->taxonomy);
                        }

                        if ($tax_post_type) {
                            baa_add_post_type_archive($tax_post_type, $add);
                        }
                    }

                    if (!empty($args['show_parents']) && !empty($term->parent)) {
                        $parents = array_reverse(get_ancestors($term->term_id, $term->taxonomy, 'taxonomy'));

                        foreach ($parents as $parent_id) {
                            $parent = get_term($parent_id, $term->taxonomy);

                            if ($parent && !is_wp_error($parent)) {
                                $add($parent->name, get_term_link($parent));
                            }
                        }
                    }

                    if (!empty($args['show_current'])) {
                        $add($term->name, null, true);
                    }
                }
            }
        }

        if (!$items) {
            return '';
        }

        $extra_class = trim((string) $args['class']);
        $extra_class = str_replace('breadcrumbs-appia-antica', '', $extra_class);

        $nav_class = trim('breadcrumbs-appia-antica ' . $extra_class);
        $style = trim((string) $args['style']);
        $style_attr = $style ? ' style="' . esc_attr($style) . '"' : '';

        ob_start();
        ?>
        <nav class="<?php echo esc_attr($nav_class); ?>" aria-label="<?php echo esc_attr((string) $args['aria_label']); ?>"<?php echo $style_attr; ?>>
            <ol class="baa__list">
                <?php foreach ($items as $index => $item): ?>
                    <?php
                    $is_link = !empty($item['url']) && empty($item['current']);
                    $label = (string) $item['label'];
                    $title_attr = '';

                    if (!empty($item['is_current_post']) && (int) $args['current_title_max_length'] > 0) {
                        [$short_label, $was_truncated] = baa_truncate(
                            $label,
                            (int) $args['current_title_max_length']
                        );

                        if ($was_truncated) {
                            $title_attr = ' title="' . esc_attr($label) . '"';
                            $label = $short_label;
                        }
                    }

                    $label_content = baa_render_label_content($label, $item, $args);
                    ?>

                    <li class="baa__item"<?php echo !empty($item['current']) ? ' aria-current="page"' : ''; ?>>
                        <?php if ($index > 0): ?>
                            <span class="baa__separator" aria-hidden="true">
                                <?php echo baa_render_separator($args); ?>
                            </span>
                        <?php endif; ?>

                        <?php if ($is_link): ?>
                            <a class="baa__link" href="<?php echo esc_url((string) $item['url']); ?>">
                                <?php echo $label_content; ?>
                            </a>
                        <?php else: ?>
                            <span class="baa__current"<?php echo $title_attr; ?>>
                                <?php echo $label_content; ?>
                            </span>
                        <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ol>
        </nav>
        <?php

        $html = trim(ob_get_clean());

        if (!empty($args['enable_schema'])) {
            $html .= "\n" . baa_render_schema($items);
        }

        return $html;
    }
}

/**
 * Read attributes from the Etch frontend marker and render.
 */
if (!function_exists('baa_render_marker')) {
    function baa_render_marker(array $matches): string {
        $attrs = shortcode_parse_atts($matches[1] ?? '');

        if (!is_array($attrs)) {
            $attrs = [];
        }

        $get = function (string $key, string $default = '') use ($attrs): string {
            if (!isset($attrs[$key])) {
                return $default;
            }

            return trim(html_entity_decode(
                (string) $attrs[$key],
                ENT_QUOTES,
                get_bloginfo('charset')
            ));
        };

        return baa_render([
            'class' => $get('class'),
            'style' => $get('style'),
            'aria_label' => $get('data-baa-aria-label', 'Breadcrumbs'),
            'separator' => $get('data-baa-separator', '/'),
            'show_home' => baa_bool($get('data-baa-show-home'), true),
            'show_home_icon' => baa_bool($get('data-baa-show-home-icon'), false),
            'home_icon_url' => esc_url_raw($get('data-baa-home-icon-url')),
            'show_parents' => baa_bool($get('data-baa-show-parents'), true),
            'show_post_type' => baa_bool($get('data-baa-show-post-type'), true),
            'taxonomy_archive_post_type' => $get('data-baa-taxonomy-archive-post-type'),
            'show_taxonomy' => baa_bool($get('data-baa-show-taxonomy'), false),
            'taxonomy' => $get('data-baa-taxonomy'),
            'show_current' => baa_bool($get('data-baa-show-current'), true),
            'show_separator_icon' => baa_bool($get('data-baa-show-separator-icon'), false),
            'separator_icon_url' => esc_url_raw($get('data-baa-separator-icon-url')),
            'enable_schema' => baa_bool($get('data-baa-enable-schema'), true),
            'home_label' => $get('data-baa-home-label', 'Home'),
            'current_title_max_length' => (int) $get('data-baa-current-title-max-length', '0'),
        ]);
    }
}

/**
 * Replace Etch frontend markers with rendered breadcrumbs.
 */
if (!function_exists('baa_replace_markers')) {
    function baa_replace_markers(string $content): string {
        if (strpos($content, 'data-breadcrumbs-appia-antica-root') === false) {
            return $content;
        }

        $pattern = '/<nav\b([^>]*\sdata-breadcrumbs-appia-antica-root(?:=["\'][^"\']*["\'])?[^>]*)>\s*<\/nav>/i';

        return preg_replace_callback($pattern, 'baa_render_marker', $content);
    }

    add_filter('render_block', 'baa_replace_markers', 20);
    add_filter('the_content', 'baa_replace_markers', 20);
}

/**
 * Add post type labels to Etch dynamic post data, only if missing.
 */
if (!function_exists('baa_add_post_type_labels_to_etch_dynamic_data')) {
    function baa_add_post_type_labels_to_etch_dynamic_data($data, $post_id) {
        if (
            isset($data['post_type']) &&
            is_array($data['post_type']) &&
            !empty($data['post_type']['slug']) &&
            !empty($data['post_type']['singular_label']) &&
            !empty($data['post_type']['plural_label'])
        ) {
            return $data;
        }

        $post_type = $data['type'] ?? get_post_type($post_id);

        if (!$post_type) {
            return $data;
        }

        $post_type_object = get_post_type_object($post_type);

        if ($post_type_object && !empty($post_type_object->labels)) {
            $data['post_type'] = [
                'slug' => esc_html($post_type),
                'singular_label' => esc_html($post_type_object->labels->singular_name ?? ''),
                'plural_label' => esc_html($post_type_object->labels->name ?? ''),
            ];
        }

        return $data;
    }

    add_filter(
        'etch/dynamic_data/post',
        'baa_add_post_type_labels_to_etch_dynamic_data',
        20,
        2
    );
}

Breadcrumb Order

For singular content, the breadcrumb order is:

Home
→ Post Type Archive
→ Parent Pages
→ Taxonomy Parents
→ Selected Taxonomy Term
→ Current Page

Example:

Home
→ Library
→ Components
→ Buttons
→ Primary Button

Taxonomy Archives

Taxonomy archive pages work slightly differently.

Instead of showing only the taxonomy hierarchy, the renderer attempts to determine the parent post type for the taxonomy and inserts its archive before the taxonomy trail.

Example:

Home
→ Library
→ Components

rather than simply

Home
→ Components

If a taxonomy belongs to multiple post types, the component uses the first registered post type unless a specific archive post type is supplied.


Home Icon

The Home icon is completely optional.

When enabled:

  • The selected SVG is inlined into the page
  • The Home label is still displayed
  • The SVG inherits the current text color
  • CSS can freely style the icon

Example output:

<a class="baa__link">
    <svg class="baa__home-icon">...</svg>
    <span class="baa__home-label">Home</span>
</a>

Unlike an <img> element, inline SVGs can use:

fill: currentColor;
stroke: currentColor;

making them theme-aware.


Separator Icons

Separators can be either:

  • text
  • inline SVG

When an SVG is selected, the component automatically replaces the separator character with the SVG while keeping the separator hidden from assistive technology.

Example:

Home
▸
Library
▸
Components

Accessibility

The component follows modern accessibility recommendations.

Features include:

  • <nav aria-label="Breadcrumbs">
  • Ordered list (<ol>)
  • aria-current="page" on the current page
  • Decorative separators marked aria-hidden
  • Decorative SVG icons marked aria-hidden
  • Screen readers announce only meaningful breadcrumb labels

SEO

When enabled, the component automatically outputs structured data using the Schema.org BreadcrumbList specification.

Example:

{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [
    {
      "@type": "ListItem",
      "position": 1,
      "name": "Home",
      "item": "https://example.com/"
    }
  ]
}

This helps search engines understand the site’s content hierarchy and may improve breadcrumb presentation in search results.


Current Title Truncation

Long page titles can be shortened without affecting accessibility.

Example:

Building Accessible Navigation…

The full title is preserved in the title attribute, while the complete title remains available to search engines.


Dynamic Post Type Labels

Etch’s dynamic data does not currently expose post type labels by default.

The component registers a lightweight filter that adds:

post_type.slug
post_type.singular_label
post_type.plural_label

The filter first checks whether another plugin has already supplied this information before adding it, preventing conflicts.


Security

The renderer follows standard WordPress security practices:

  • Escapes all HTML output
  • Escapes URLs
  • Sanitizes taxonomy names
  • Sanitizes CSS classes
  • Sanitizes SVG output before rendering
  • Uses wp_safe_remote_get() for SVG retrieval
  • Removes scripts and event handlers from SVGs
  • Uses WordPress JSON encoding for schema output

Why Server Rendering?

Breadcrumbs depend heavily on WordPress context:

  • current post
  • current taxonomy
  • archive type
  • parent hierarchy
  • page-for-posts
  • custom post type archives

Generating breadcrumbs in PHP ensures the component always reflects the correct WordPress query without requiring REST requests or client-side JavaScript.

This also improves:

  • SEO
  • performance
  • caching
  • accessibility
  • maintainability

Design Philosophy

Breadcrumbs Appia Antica was intentionally designed to separate presentation from logic.

  • Etch controls the editor experience and styling.
  • PHP determines the correct breadcrumb hierarchy.
  • CSS controls appearance.
  • JSON-LD improves search engine understanding.

This separation keeps the component easy to customize while ensuring the breadcrumb trail is always generated using the current WordPress context.