0

I've created a custom taxonomy and added it to the admin menu. It works fine but I'd like to tweak it a little so that the input autofocus goes to the "name" field as soon as I select to add a new term. I've looked at edit-tags.php and can't find where I can insert this.

Thanks in advance!

EDIT:

Howdy_Mcgee's code worked perfectly. Now I need to add it a second time for a different taxonomy and this time it won't work. I changed the "tag_focus" function name in the function declaration line and in the add_action line. To me, it doesn't seem I should change anything else, should I? Or can I modify the first function to work on both pages? All the code for both taxonomies is in the child theme's function file.

Thanks again!

0

You can use wp_register_script() then wp_enqueue_script() to inject javascript into wp admin.

functions.php or plugin (my personal recc would be a simple plugin):

wp_register_script( 'focus-tag-name-script', 'path/to/script.js' , 10 );
wp_enqueue_script( 'focus-tag-name-script' );

script:

jQuery( window ).on( 'load', function() {
    jQuery( 'input[name="tag-name"]' )
        .focus() // place cursor in field
        .select(); // select field contents
} );

As requested in the comments, here it is as a plugin: (I just wrote this out here without testing, but at the very least should put you in the right direction if there are any errors)

// define the plugin
jQuery( function( $ ) {
  $.fn.fieldFocus = function( options ) {

    var settings = $.extend({
        select: false
    }, options );

    this.focus() // place cursor in field
    if ( settings.selectContents ) this.select(); // select field contents
  } );
} );

// calling the plugin
jQuery( window ).on( 'load', function() {
  // places cursor in the field
  jQuery( 'input[name="tag-name"]' ).fieldFocus();

  // places cursor in the field and selects contents
  jQuery( 'input[name="tag-name"]' ).fieldFocus( {
    selectContents: true
  } );
}
|improve this answer|||||
  • Thank-you. I've tried using jQuery (adding tabs to page content) unsuccessully. This is something I'll have to learn to do properly, when I have time. – 1td Feb 5 '15 at 20:04
  • Joey, could I create this jQuery script once and use it more than once? How would I do this? See my edit. I'm unable to manage the focus on a second custom taxonomy. – 1td Feb 13 '15 at 4:19
  • I've amended my answer to show it used as a plugin. If you're using Howdy_McGee's answer though you could just remove && 'YOUR_TAXONOMY_HERE' == $_GET['taxonomy'] from the conditional and it'll work in all taxonomies. Alternatively replace it with && ( 'YOUR_TAXONOMY_HERE' == $_GET['taxonomy'] || 'YOUR_OTHER_TAXONOMY_HERE' == $_GET['taxonomy'] ) if you want to keep the scope narrow. – Joey Yax Feb 13 '15 at 4:37
  • I really appreciate the quick and detailed response, Joey. Thanks! – 1td Feb 13 '15 at 4:55
0

Maybe this will work for you:

function tag_focus() {
    if( isset( $_GET['taxonomy'] ) && 'YOUR_TAXONOMY_HERE' == $_GET['taxonomy'] ) :
  ?>

    <script type="text/javascript">
        jQuery( document ).ready( function() {
            jQuery( '#submit' ).click( function() {
                jQuery( '#tag-name' ).focus();
            } );
        } );
    </script>

  <?php endif;
}
add_action( 'admin_head-edit-tags.php', 'tag_focus' );

This is a little bit of JS to get what you want done. If you want to use this on all taxonomies ( built-in and custom ) then you could remove the conditional entirely. The JS will only run on the edit-tags.php page as specified by our action.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.