Skip to content

fix a11y breadcrumb #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions inc/Framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BEA\Theme\Framework\Services\Acf;
use BEA\Theme\Framework\Services\Assets;
use BEA\Theme\Framework\Services\Breadcrumb;
use BEA\Theme\Framework\Services\Performance;
use BEA\Theme\Framework\Services\Editor;
use BEA\Theme\Framework\Services\Editor_Patterns;
Expand Down Expand Up @@ -39,6 +40,7 @@ class Framework {
Svg::class,
Acf::class,
Menu::class,
Breadcrumb::class,

// Services as Tools
Body_Class::class,
Expand Down
76 changes: 76 additions & 0 deletions inc/Services/Breadcrumb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* Breadcrumb a11y service
* https://www.wpexplorer.com/accessible-yoast-seo-breadcrumbs/
* https://developer.yoast.com/features/blocks/breadcrumbs/
* https://gist.github.com/doubleedesign/7224a5e990b8506ddb8ec66de8348b2b
*/

namespace BEA\Theme\Framework\Services;

use BEA\Theme\Framework\Service;
use BEA\Theme\Framework\Service_Container;

class Breadcrumb implements Service {

public function register( Service_Container $container ): void {
}

public function boot( Service_Container $container ): void {
add_filter( 'wpseo_breadcrumb_output_wrapper', [ $this, 'replace_breadcrumb_wrapper' ]);
add_filter( 'wpseo_breadcrumb_single_link_wrapper', [ $this, 'replace_breadcrumb_link_wrapper' ]);
add_filter( 'wpseo_breadcrumb_single_link', [ $this, 'replace_breadcrumb_link' ] );
add_filter( 'wpseo_breadcrumb_separator', [ $this, 'remove_breadcrumb_separator' ] );
}

/**
* Get service name
*
* @return string
*/
public function get_service_name(): string {
return 'breadcrumb';
}

/**
* Replace the wrapper around the breadcrumbs
*
* @return string
*/
public function replace_breadcrumb_wrapper(): string {
return 'ol';
}

/**
* Replace the wrappers around the links.
*
* @return string
*/
public function replace_breadcrumb_link_wrapper(): string {
return 'li';
}

/**
* Replace the links and add the separator inside with aria-hidden="true".
*
* @param string $link_output The link output.
* @return string
*/
public function replace_breadcrumb_link( $link_output ): string {
if ( ! str_contains( $link_output, 'breadcrumb_last' ) ) {
return str_replace( '</li>', '<span class="breadcrumb__separator" aria-hidden="true">></span></li>', $link_output );
}

return $link_output;
}

/**
* Remove the breadcrumbs separator outside the links.
*
* @return string
*/
public function remove_breadcrumb_separator(): string {
return '';
}
}
Loading