Skip to content
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

Added: Fix for adding skip links when they are not there #731

Merged
merged 31 commits into from
Sep 3, 2024

Conversation

pattonwebz
Copy link
Member

@pattonwebz pattonwebz commented Aug 30, 2024

This PR adds a fix to the plugin that users can add a skip link to their pages with.

  • It provides a checkbox to enable the feature and a text input to type in the target ID
  • The target ID field access a comma separated list of possible IDs to target. The list can include the # or not and it will be handled appropriately.
  • When enabled it inlines some JS that will attempt to detect if there is a valid skip link already and if so do nothing
    • If no skip link is found it will attempt to find one of the IDs referenced in the targets list.
    • If it finds an ID then it will clone a skip link and some styles for it from a <template> tag added to the page.

If you are using a FSE enabled theme like Twenty Twenty-Four then it has some features to add a skip links that lives outside the theme so for testing you will need to remove it and add an ID to target. With Twenty Twenty-Four this code works but you may have to tweak main tag to something else if you have a different theme being tested.

remove_action( 'wp_footer', 'the_block_template_skip_link' );
add_action('wp_footer', function () {
	?>
<script>
	// find the first main tag on the page and add id of 'main-content' if it doesn't have one.
	if ( ! document.querySelector( 'main' ).id ) {
		document.querySelector( 'main' ).id = 'main-content';
	}

	// find the first nav on the page and add id of 'main-navigation' if it doesn't have one.
	if ( ! document.querySelector( 'nav' ).id ) {
		document.querySelector( 'nav' ).id = 'main-navigation';
	}
</script>
	<?php
} );

Checklist

  • PR is linked to the main issue in the repo (or via card ID in the branch name)
  • Tests are added that cover changes

Copy link
Member

@SteveJonesDev SteveJonesDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing all the settings outlined on the card.

@pattonwebz pattonwebz force-pushed the william/7652418924/fixes/skip-link branch from 380415c to 330ad16 Compare September 3, 2024 20:20
Copy link
Member

@SteveJonesDev SteveJonesDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't fully tested functionality but this looks good to merge in. There is a lot of structure we need in the PR so that we can base off the develop branch for other fixes.

@pattonwebz pattonwebz merged commit bc11864 into develop Sep 3, 2024
32 checks passed
@pattonwebz pattonwebz deleted the william/7652418924/fixes/skip-link branch September 23, 2024 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants