https://codewp.ai/snippet/6sjrqy3ezuli7voto35dy48uuze4j/
Copy URL

Share this on your favorite social networks:

OR
Please log in to use this feature.
Email To Yourself Or A Developer
verified
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.

Read More
publishLoad Prompt
preview
Make Public
share
Share Snippet
download
Export Snippet
Mode: WordPress PHP
Language: PHP
Visibility: Public
Date:
March 10, 2023
Creator:
@James LePa...
cross