# REST API Link for the Current Item in the Admin Bar

> Adds a "REST" link to the front-end admin bar for administrators that opens the JSON for the post, page, term or author being viewed, which speeds up headless and integration work.

- Type: PHP
- Runs: Frontend only
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: rest-api, admin-bar, headless
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/debug-rest-link-admin-bar/

## Code

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

    $object = get_queried_object();
    $route  = '';

    if ( $object instanceof WP_Post ) {
        $route = rest_get_route_for_post( $object );
    } elseif ( $object instanceof WP_Term ) {
        $route = rest_get_route_for_term( $object );
    } elseif ( $object instanceof WP_User ) {
        $route = '/wp/v2/users/' . $object->ID;
    }

    if ( ! $route ) {
        return;
    }

    $admin_bar->add_node( array(
        'id'    => 'wpide-rest-link',
        'title' => 'REST',
        'href'  => rest_url( ltrim( $route, '/' ) ),
        'meta'  => array( 'target' => '_blank', 'rel' => 'noopener' ),
    ) );
}, 999 );
```

## Notes

Only items exposed in the REST API get a link.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "REST API Link for the Current Item in the Admin Bar" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
