Skip to content

Instantly share code, notes, and snippets.

@tobyink tobyink/functions.php

Last active Jun 9, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.