Home / admin / Add admin post column with wp_get_shortlink
  • How to Add admin post column with wp_get_shortlink

    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.

    ( click code to copy )

    wordpress snippet : PHP

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

    ( WordPress codex functions, hooks, in this snippet. )

    manage_posts_columns, add_filter, add_action, __, _n, _e, wp,
  • Can this also support bit.ly links?

    • 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.

  • Clifford P

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

    • 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

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

      • Clifford P

        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.

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