"Copy Link" Action in Admin Lists
Adds a "Copy link" action under each published post and page in admin lists that copies its public URL to the clipboard in one click, for sharing in chats and emails.
Opens WPIDE on
Code
$wpide_copy_link_action = function ( $actions, $post ) {
if ( 'publish' === $post->post_status && is_post_type_viewable( $post->post_type ) ) {
$actions['wpide_copy_link'] = sprintf(
'<button type="button" class="button-link wpide-copy-link" data-url="%s">Copy link</button>',
esc_attr( get_permalink( $post ) )
);
}
return $actions;
};
add_filter( 'post_row_actions', $wpide_copy_link_action, 10, 2 );
add_filter( 'page_row_actions', $wpide_copy_link_action, 10, 2 );
add_action( 'admin_footer-edit.php', function () {
?>
<script>
document.addEventListener( 'click', function ( event ) {
var button = event.target.closest( '.wpide-copy-link' );
if ( ! button ) { return; }
var url = button.dataset.url;
var done = function () {
var label = button.textContent;
button.textContent = 'Copied!';
setTimeout( function () { button.textContent = label; }, 1500 );
};
if ( navigator.clipboard && window.isSecureContext ) {
navigator.clipboard.writeText( url ).then( done );
} else {
window.prompt( 'Copy this link:', url );
}
} );
</script>
<?php
} );
Notes
Uses the browser clipboard; very old browsers fall back to a prompt with the URL selected.
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.