Skip to content

Commit

Permalink
add keyboard shortcut (Ctrl/Cmd + s) functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterT2000 committed Aug 10, 2020
1 parent 3dedafd commit d11b866
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,44 @@ function clickHandlerRebuildButton()
<?php
}

/* THIS APPLIES TO THE SAVE/PUBLISH BUTTONS TRIGGERED BY CTRL/CMD + S */
add_action('admin_footer', 'addKeyboardShortcut')

function addKeyboardShortcut() {
?>
<script type="text/javascript">
const body = document.body;

let button = '';
let doingClick = false;

// let JSie know on which page to seek the buttons :)
if(body.classList.contains('post-php')) {
// now we're on a page where save buttons appear, let's seek for it!
button = body.querySelector("input[name='save']")
} else if(body.classList.contains('nav-menus-php')) {
button = body.querySelector("input[name='save_menu']")
} else if(body.classList.contains('theme-editor-php' || 'plugin-editor-php')) {
button = body.querySelector("input[name='submit']")
}

if(button) {
document.addEventListener('keydown', (e) => {
if(e.ctrlKey || e.metaKey && e.key === "s") {
if(doingClick) return
doingClick = true

// click that button!
button.click();
e.preventDefault();
doingClick = false
}
})
}
</script>
<?php
}

// Make ACF return null instead of false when value doesn't exist so Graphql won't throw an error
add_filter('acf/format_value', 'acf_nullify_empty', 100, 3);

Expand Down

0 comments on commit d11b866

Please sign in to comment.