Last active
June 9, 2020 13:11
-
-
Save tobyink/51d941a87b01b3621126586c164a47fe to your computer and use it in GitHub Desktop.
Wordpress edit hotkey
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
<?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' ) ); | |
} ); |
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
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