X

Add admin post column with wp_get_shortlink

Snippets by IsItWP

Are you looking for a way to add a new column to the posts screen with an input field containing the shortlink? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add admin post column with wp_get_shortlink in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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;
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 best WordPress help desk plugins and how to set up download tracking in WordPress with Google Analytics.

Comments   Leave a Reply

  1. I noticed this column works on posts but not on custom post types. Any ideas?

    1. Hi Clifford, you can do the following for example.

      manage_edit-${post_type}_columns

      For a more detailed overview the codex has a working sample.
      http://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column

      1. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      2. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      3. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      4. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      5. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      6. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      7. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      8. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      9. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      10. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      11. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      12. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      13. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      14. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      15. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      16. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      17. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      18. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      19. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      20. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      21. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      22. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      23. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      24. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      25. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      26. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      27. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      28. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      29. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      30. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      31. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      32. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      33. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      34. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      35. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

      36. The original snippet’s “Shortlink” column displays as one of the CPT’s columns. It’s just that nothing displays. I verified with echo wp_get_shortlink(‘13715’); that that CPT post has a shortlink.

        1. let me take a look and post something for you.

  2. Avinash D'Souza August 14, 2011 at 6:20 pm

    Can this also support bit.ly links?

    1. As of right now it just uses the wordpress short url’s although I’m sure it could be setup to work with bitly as well. I take a look into it and see what I can come up with.

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!