Skip to content

Instantly share code, notes, and snippets.

@tobyink
Last active June 9, 2020 13:11
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 tobyink/51d941a87b01b3621126586c164a47fe to your computer and use it in GitHub Desktop.
Save tobyink/51d941a87b01b3621126586c164a47fe to your computer and use it in GitHub Desktop.
Wordpress edit hotkey
<?php
# These snippets can be added to your existing functions.php.
#
# 1. This outputs <link rel="edit-form"> in your page <head> elements.
#
add_action( 'wp_head', function () {
global $post;
if ( $post && current_user_can( 'edit_post', $post->ID ) ) {
$id = $post->ID;
if ( $id ) {
$link = get_edit_post_link( $id );
if ( $link ) {
echo "<link rel='edit-form' href='$link'>";
}
}
}
} );
# 2. Load the hotkeys.js library plus our own script.
#
add_action( 'wp_enqueue_scripts', function () {
$the_theme = wp_get_theme();
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'hotkeys', 'https://unpkg.com/hotkeys-js/dist/hotkeys.min.js', [], '3.8.1' );
wp_enqueue_script( 'site-hotkeys', get_stylesheet_directory_uri() . '/js/site-hotkeys.js', ['jquery', 'hotkeys'], $the_theme->get( 'Version' ) );
} );
hotkeys( 'ctrl+;', function () {
var $link = jQuery( 'link[rel=edit-form]' );
if ( $link.length > 0 )
location.href = $link.attr( 'href' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment