Halloween Sale 15% off Ends in See the plans
PHP

Bookmark Admin Screens in the Admin Bar

Adds a star menu to the admin bar where each user can bookmark the admin screen they are on, such as a plugin's settings or a filtered list, and jump back to it from anywhere.

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

Code

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

    $bookmarks = (array) get_user_meta( get_current_user_id(), 'wpide_bookmarks', true );
    $bookmarks = array_filter( $bookmarks );
    $here      = is_admin() ? ( is_ssl() ? 'https://' : 'http://' ) . ( $_SERVER['HTTP_HOST'] ?? '' ) . ( $_SERVER['REQUEST_URI'] ?? '' ) : '';
    $here      = esc_url_raw( $here );

    $admin_bar->add_node( array(
        'id'    => 'wpide-bookmarks',
        'title' => '<span class="ab-icon dashicons dashicons-star-filled" style="margin-top:2px" aria-hidden="true"></span><span class="ab-label">Bookmarks</span>',
    ) );

    foreach ( $bookmarks as $i => $bookmark ) {
        $admin_bar->add_node( array(
            'id'     => 'wpide-bookmark-' . $i,
            'parent' => 'wpide-bookmarks',
            'title'  => esc_html( $bookmark['title'] ),
            'href'   => esc_url( $bookmark['url'] ),
        ) );
    }

    if ( $here ) {
        $saved = in_array( $here, wp_list_pluck( $bookmarks, 'url' ), true );
        $admin_bar->add_node( array(
            'id'     => 'wpide-bookmark-toggle',
            'parent' => 'wpide-bookmarks',
            'title'  => $saved ? 'Remove this page' : '+ Bookmark this page',
            'href'   => wp_nonce_url( add_query_arg( array(
                'action'   => 'wpide_bookmark',
                'wpide_bm' => $saved ? 'remove' : 'add',
                'url'      => rawurlencode( $here ),
                'title'    => rawurlencode( wp_strip_all_tags( ! empty( $GLOBALS['title'] ) ? $GLOBALS['title'] : 'Admin page' ) ),
            ), admin_url( 'admin-post.php' ) ), 'wpide_bookmark' ),
        ) );
    }
}, 90 );

add_action( 'admin_post_wpide_bookmark', function () {
    check_admin_referer( 'wpide_bookmark' );

    $url   = isset( $_GET['url'] ) ? esc_url_raw( wp_unslash( $_GET['url'] ) ) : '';
    $title = isset( $_GET['title'] ) ? sanitize_text_field( wp_unslash( $_GET['title'] ) ) : '';
    if ( ! $url || 0 !== strpos( $url, admin_url() ) ) {
        wp_die( 'Only admin screens can be bookmarked.', 400 );
    }

    $bookmarks = array_filter( (array) get_user_meta( get_current_user_id(), 'wpide_bookmarks', true ) );
    $bookmarks = array_values( array_filter( $bookmarks, function ( $bookmark ) use ( $url ) {
        return $bookmark['url'] !== $url;
    } ) );
    if ( isset( $_GET['wpide_bm'] ) && 'add' === $_GET['wpide_bm'] ) {
        $bookmarks[] = array( 'url' => $url, 'title' => $title ? $title : $url );
    }
    update_user_meta( get_current_user_id(), 'wpide_bookmarks', array_slice( $bookmarks, -15 ) );

    wp_safe_redirect( $url );
    exit;
} );

Notes

Bookmarks are saved per user (up to 15); use "Remove this page" in the menu to delete one.

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.