Show the Post ID and Short Link in the Block Editor
Adds the post ID and a copyable short link (?p=123) to the Summary panel of the block editor sidebar, which is handy for shortcodes, support tickets and quick sharing.
Opens WPIDE on
Code
add_action( 'enqueue_block_editor_assets', function () {
$screen = get_current_screen();
if ( ! $screen || 'post' !== $screen->base ) {
return;
}
wp_register_script( 'wpide-post-id-panel', false, array( 'wp-plugins', 'wp-element', 'wp-components', 'wp-data', 'wp-editor' ), '1.0', true );
wp_enqueue_script( 'wpide-post-id-panel' );
wp_add_inline_script( 'wpide-post-id-panel', 'var wpideHomeUrl = ' . wp_json_encode( home_url( '/' ) ) . ';' . <<<'JS'
( function ( wp ) {
var el = wp.element.createElement;
var Panel = ( wp.editor && wp.editor.PluginPostStatusInfo ) || ( wp.editPost && wp.editPost.PluginPostStatusInfo );
if ( ! Panel ) { return; }
function PostId() {
var id = wp.data.useSelect( function ( select ) { return select( 'core/editor' ).getCurrentPostId(); }, [] );
var copied = wp.element.useState( false );
if ( ! id ) { return null; }
var link = wpideHomeUrl + '?p=' + id;
return el( Panel, null,
el( 'div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', width: '100%' } },
el( 'span', null, 'ID ', el( 'strong', null, id ) ),
el( wp.components.Button, {
variant: 'link',
onClick: function () {
if ( navigator.clipboard ) { navigator.clipboard.writeText( link ).then( function () { copied[1]( true ); } ); }
}
}, copied[0] ? 'Copied!' : 'Copy short link' )
)
);
}
wp.plugins.registerPlugin( 'wpide-post-id-panel', { render: PostId } );
} )( window.wp );
JS
);
} );
Notes
The short link always redirects to the current address, even after the slug changes.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.