Created
May 15, 2018 13:06
-
-
Save anybodesign/df647d0a60d339c3773a1785c184a0b5 to your computer and use it in GitHub Desktop.
Accessible Facetwp dropdowns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fs_add_dropdown_labels() { | |
?> | |
<script> | |
(function($) { | |
$(document).on('facetwp-loaded', function() { | |
$('.facetwp-dropdown').each(function() { | |
var facet_name = $(this).parent().attr('data-name'); | |
var facet_label = FWP.settings.labels[facet_name]; | |
$(this).attr('id', facet_name); | |
if ($('.facet-label[for="' + facet_name + '"]').length < 1) { | |
$(this).parent().before('<label class="facet-label" for="' + facet_name + '">' + facet_label + '</label>'); | |
} | |
}); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
add_action( 'wp_head', 'fs_add_dropdown_labels', 100 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For accessibility reasons, we add a label with a
for
attribute before each dropdown. We also add an ID to each dropdown, matching the for attribute of their label. Inspired by the original documentation: https://facetwp.com/add-labels-above-each-facet/