The following code adds a text input for a subtitle to the space directly below the title input, via the edit_form_after_title
hook. However, when editing the title, pressing tab moves the cursor to the main post editor and I can’t figure out what tabindex I should use (or some other method if available) in order to tab from the title to my subtitle input instead.
add_action( 'edit_form_after_title', 'add_input' );
function add_input(){
global $post;
$options = get_option( 'kia_subtitle_options' );
// only show input if the post type was not enabled in options
if ( isset ( $options['post_types'] ) && in_array( $post->post_type, $options[ 'post_types'] ) ) {
//create the meta field (don't use a metabox, we have our own styling):
wp_nonce_field( plugin_basename( __FILE__ ), 'kia_subnonce' );
//get the subtitle value (if set)
$sub = get_post_meta( get_the_ID(), 'kia_subtitle', true );
// echo the inputfield with the value.
printf( '<input type="text" class="widefat" name="subtitle" placeholder="%s" value="%s" id="the_subtitle" tabindex="1"/>',
__( 'Subtitle', 'kia-subtitle' ),
esc_attr($sub) );
}
}
I found last part of @birgire answer the most useful way, however it breaks the possibility to tab to content. In fact I think is normal focus the content by click tab while in the subtitle field.
To do that, you have also to take care if the content is shown in the “Text” tab or in the “Visual” tab (TynyMCE).
I put the code inline , inside your function, but you can use
wp_enqueue_script
or add it to some javascript file already enqueued in post edit screen.You can also try playing with the
focusout
Javascript event and thefocus()
method:Here is a demo Subtitle plugin:
/wp-content/plugins/subtitle/subtitle.php
and here is for example a non-jQuery version for your
wp-content/plugins/subtitle/js/script.js
file:My initial jQuery version didn’t work, so that’s why I tested the non-jQuery version and that works on my install 😉
Well, this jQuery version seems to work:
and this
When the title input text field looses the focus:
it goes to the subtitle input text field.
But maybe it’s more user-friendly to only intercept the
TAB
keycode in the title input text field. With a modification of this code, we can for example use: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 yourafter_title
area selectors.( I also posted it here: https://gist.github.com/petertwise/8ebb6a76f018494fcc62171d622cc85f )