Add admin post column with wp_get_shortlink

Add the following snippet to the functions.php of your wordpress theme. This snippet will add a new column to the posts screen with an input field containing the wordpress shortlink. I added a little javascript to auto select the text.

add_filter( 'manage_posts_columns', 'shortlink_add_column' );
add_action( 'manage_posts_custom_column', 'shortlink_add_value', 10, 2 );
function shortlink_add_column($cols) {
$cols['shortlink'] = __('Shortlink');
return $cols;
}
function shortlink_add_value($column_name, $post_id) {
if ( 'shortlink' == $column_name ) {
echo '<input type="text" value="'.wp_get_shortlink($post_id).'" onclick="this.focus(); this.select();" />';
}
}
add_filter( 'manage_edit-post_sortable_columns', 'shortlink_sortable_column' );
function shortlink_sortable_column( $columns ) {
$columns['shortlink'] = 'Shortlink';
return $columns;
}

Leave a comment