Skip to content

Instantly share code, notes, and snippets.

@jpmarchand
Last active June 1, 2017 20:14
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 jpmarchand/fa97d859695ff42d8ecf to your computer and use it in GitHub Desktop.
Save jpmarchand/fa97d859695ff42d8ecf to your computer and use it in GitHub Desktop.
Enable keyboard navigation for single posts in WordPress. Source: http://techglimpse.com/create-keyboard-navigation-for-your-website/
<?php
//* Euqueue keyboard navigation script
add_action( 'wp_enqueue_scripts', 'customprefix_add_keyboard_navigation' );
function customprefix_add_keyboard_navigation() {
if ( is_single() ) {
wp_enqueue_script( 'keyboard-navigation', get_bloginfo( 'stylesheet_directory' ) . '/js/keyboard-navigation.js', array( 'jquery' ), '1.0.0' );
}
}
jQuery(document).ready(function() {
jQuery(document).keydown(function(e) {
var url = false;
if (e.which == 37) { // Left arrow key code
url = jQuery('.pagination-previous a').attr('href');
} else if (e.which == 39) { // Right arrow key code
url = jQuery('.pagination-next a').attr('href');
}
if (url) {
window.location = url;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment