Skip to content

Instantly share code, notes, and snippets.

@BinaryMoon
Created October 30, 2018 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMoon/af92b9bcadec87167848ceab1e046f0c to your computer and use it in GitHub Desktop.
Save BinaryMoon/af92b9bcadec87167848ceab1e046f0c to your computer and use it in GitHub Desktop.
Add a 'skip to search' link to the top of a WordPress theme.
<?php
/**
* Plugin Name: Skip to search.
* Plugin URI: https://prothemedesign.com
* Description: Add "Skip to search" button to site header.
* Author: Ben Gillbanks
* Version: 1.0
* Author URI: https://prothemedesign.com
* Text Domain: sts
*/
function sts_footer() {
?>
<script>
(function( $ ) {
// Find search widget id.
var $search = $( '.search-form:first' );
if ( $search.length < 1 ) {
return;
}
$search.parent( '.widget-wrap' ).attr( 'id', 'skip-to-search' );
var skip_link = $( '<a href="#skip-to-search" class="screen-reader-shortcut"><?php esc_html_e( 'Skip to Search', 'sts' ); ?></a>' );
$( 'a.screen-reader-shortcut[href="#site-content"]' ).after( skip_link );
})( jQuery );
</script>
<?php
}
add_action( 'wp_footer', 'sts_footer' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment