Skip to main content
add gist link
Source Link
squarecandy
  • 956
  • 10
  • 16

I made some additions to the final suggestion by @birgire to make this work without knowing the ID of the first field in the after_title area if you're using ACF. If you're not using ACF, just adjust #acf_after_title-sortables .inside > div:first-child to match your after_title area selectors.

// Admin Functions
( function( $ ) {
    // better accessiblity and data entry for ACF
    $(document).ready( function() {
        // if there is a an "after_title" field group
        if ( $( '#acf_after_title-sortables' ).length ) {
            // listen for keydown inside the main title field for any add/edit screen
            $('body.post-new-php, body.post-php').on( 'keydown', '#title', function( e ) {
                var keyCode = event.keyCode || event.which;
                // if the key is tab
                if ( 9 == keyCode ) {
                    // don't do the normal core WP behavior
                    e.preventDefault();
                    // find the first field in the after_title area
                    $( '#acf_after_title-sortables .inside > div:first-child' )
                        .find( ':input:not([type=hidden])' )
                        .focus();
                }
            } );
        }
    } );
} )( jQuery );

/*
Thanks to these Stack Overflow answers for inspiration:
@neiker - https://stackoverflow.com/a/17150138/947370
@gdoron-is-supporting-monica https://stackoverflow.com/a/11278006/947370
@birgire https://wordpress.stackexchange.com/a/127042/41488
*/

( I also posted it here: https://gist.github.com/petertwise/8ebb6a76f018494fcc62171d622cc85f )

I made some additions to the final suggestion by @birgire to make this work without knowing the ID of the first field in the after_title area if you're using ACF. If you're not using ACF, just adjust #acf_after_title-sortables .inside > div:first-child to match your after_title area selectors.

// Admin Functions
( function( $ ) {
    // better accessiblity and data entry for ACF
    $(document).ready( function() {
        // if there is a an "after_title" field group
        if ( $( '#acf_after_title-sortables' ).length ) {
            // listen for keydown inside the main title field for any add/edit screen
            $('body.post-new-php, body.post-php').on( 'keydown', '#title', function( e ) {
                var keyCode = event.keyCode || event.which;
                // if the key is tab
                if ( 9 == keyCode ) {
                    // don't do the normal core WP behavior
                    e.preventDefault();
                    // find the first field in the after_title area
                    $( '#acf_after_title-sortables .inside > div:first-child' )
                        .find( ':input:not([type=hidden])' )
                        .focus();
                }
            } );
        }
    } );
} )( jQuery );

/*
Thanks to these Stack Overflow answers for inspiration:
@neiker - https://stackoverflow.com/a/17150138/947370
@gdoron-is-supporting-monica https://stackoverflow.com/a/11278006/947370
@birgire https://wordpress.stackexchange.com/a/127042/41488
*/

I made some additions to the final suggestion by @birgire to make this work without knowing the ID of the first field in the after_title area if you're using ACF. If you're not using ACF, just adjust #acf_after_title-sortables .inside > div:first-child to match your after_title area selectors.

// Admin Functions
( function( $ ) {
    // better accessiblity and data entry for ACF
    $(document).ready( function() {
        // if there is a an "after_title" field group
        if ( $( '#acf_after_title-sortables' ).length ) {
            // listen for keydown inside the main title field for any add/edit screen
            $('body.post-new-php, body.post-php').on( 'keydown', '#title', function( e ) {
                var keyCode = event.keyCode || event.which;
                // if the key is tab
                if ( 9 == keyCode ) {
                    // don't do the normal core WP behavior
                    e.preventDefault();
                    // find the first field in the after_title area
                    $( '#acf_after_title-sortables .inside > div:first-child' )
                        .find( ':input:not([type=hidden])' )
                        .focus();
                }
            } );
        }
    } );
} )( jQuery );

/*
Thanks to these Stack Overflow answers for inspiration:
@neiker - https://stackoverflow.com/a/17150138/947370
@gdoron-is-supporting-monica https://stackoverflow.com/a/11278006/947370
@birgire https://wordpress.stackexchange.com/a/127042/41488
*/

( I also posted it here: https://gist.github.com/petertwise/8ebb6a76f018494fcc62171d622cc85f )

Source Link
squarecandy
  • 956
  • 10
  • 16

I made some additions to the final suggestion by @birgire to make this work without knowing the ID of the first field in the after_title area if you're using ACF. If you're not using ACF, just adjust #acf_after_title-sortables .inside > div:first-child to match your after_title area selectors.

// Admin Functions
( function( $ ) {
    // better accessiblity and data entry for ACF
    $(document).ready( function() {
        // if there is a an "after_title" field group
        if ( $( '#acf_after_title-sortables' ).length ) {
            // listen for keydown inside the main title field for any add/edit screen
            $('body.post-new-php, body.post-php').on( 'keydown', '#title', function( e ) {
                var keyCode = event.keyCode || event.which;
                // if the key is tab
                if ( 9 == keyCode ) {
                    // don't do the normal core WP behavior
                    e.preventDefault();
                    // find the first field in the after_title area
                    $( '#acf_after_title-sortables .inside > div:first-child' )
                        .find( ':input:not([type=hidden])' )
                        .focus();
                }
            } );
        }
    } );
} )( jQuery );

/*
Thanks to these Stack Overflow answers for inspiration:
@neiker - https://stackoverflow.com/a/17150138/947370
@gdoron-is-supporting-monica https://stackoverflow.com/a/11278006/947370
@birgire https://wordpress.stackexchange.com/a/127042/41488
*/