Verified working as of March 20, 2023
GenID: 44594
Quick Post Updates with Command + S Keyboard Shortcut in WordPress
Original Prompt: if user does keybord combo (macbook) of command + s on any post edit admin view, click the update button. Add js to admin footer.
Original
function cwpai_save_post_on_cmd_s() {
if (is_admin()) {
echo '<script>
jQuery(document).keydown(function(e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
jQuery("#publish").click();
}
});
</script>';
}
}
add_action('admin_footer', 'cwpai_save_post_on_cmd_s');
Snippet Explanation
This code snippet adds a JavaScript event listener to the update button on the post edit admin view. When the user presses the Command + S keyboard shortcut on a laptop, the code will trigger the update button click event. This allows the user to quickly save their post to the WordPress database with the shortcut, rather than manually clicking the button.