Halloween Sale 15% off Ends in See the plans
PHP

Quick Find Box in the Admin Bar

Adds a small search box to the admin bar on every admin screen that searches posts, pages or users, and jumps straight to the editor when you type a post ID.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'admin_bar_menu', function ( $admin_bar ) {
    if ( ! is_admin() || ! current_user_can( 'edit_posts' ) ) {
        return;
    }

    $form = '<form action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" method="get" style="display:flex;gap:4px;align-items:center;height:32px">'
        . '<input type="hidden" name="action" value="wpide_quick_find">'
        . '<input type="hidden" name="_wpnonce" value="' . esc_attr( wp_create_nonce( 'wpide_quick_find' ) ) . '">'
        . '<label class="screen-reader-text" for="wpide-quick-find">Find</label>'
        . '<input id="wpide-quick-find" name="wpide_q" type="search" placeholder="Find or ID..." style="height:24px;min-height:24px;line-height:1;padding:0 6px;width:150px;font-size:13px">'
        . '<select name="wpide_in" aria-label="Where" style="height:24px;min-height:24px;line-height:1;font-size:12px;padding:0 20px 0 4px">'
        . '<option value="post">Posts</option><option value="page">Pages</option>' . ( current_user_can( 'list_users' ) ? '<option value="users">Users</option>' : '' )
        . '</select></form>';

    $admin_bar->add_node( array(
        'id'     => 'wpide-quick-find',
        'parent' => 'top-secondary',
        'title'  => $form,
        'meta'   => array( 'class' => 'wpide-quick-find' ),
    ) );
}, 100 );

add_action( 'admin_post_wpide_quick_find', function () {
    check_admin_referer( 'wpide_quick_find' );
    $q  = isset( $_GET['wpide_q'] ) ? sanitize_text_field( wp_unslash( $_GET['wpide_q'] ) ) : '';
    $in = isset( $_GET['wpide_in'] ) ? sanitize_key( $_GET['wpide_in'] ) : 'post';

    if ( ctype_digit( $q ) && get_post( (int) $q ) && current_user_can( 'edit_post', (int) $q ) ) {
        wp_safe_redirect( get_edit_post_link( (int) $q, 'url' ) );
        exit;
    }

    $url = 'users' === $in && current_user_can( 'list_users' )
        ? admin_url( 'users.php?s=' . rawurlencode( $q ) )
        : admin_url( 'edit.php?post_type=' . ( 'page' === $in ? 'page' : 'post' ) . '&s=' . rawurlencode( $q ) );

    wp_safe_redirect( $url );
    exit;
} );

Notes

Type a number to open that post or page for editing; anything else searches the chosen list.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.