How to Add an Admin Post Column with wp_get_shortlink in WordPress

Home » Snippets » How to Add an Admin Post Column with wp_get_shortlink in WordPress
0

Created with:

Visibility: 

public

Creator: Alex

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re a WordPress user, you may have noticed that the post listing page in the admin area only displays a limited amount of information about each post. However, one important piece of information that’s not included by default is the post’s shortlink. Fortunately, by adding a custom column to the post listing page with the wp_get_shortlink function, you can display the shortlink for each post and make it easier to share your content. In this article, we’ll show you how to add an admin post column with wp_get_shortlink.

					function wpturbo_add_shortlink_column( $columns ) {
    $columns['shortlink'] = __( 'Shortlink', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_post_posts_columns', 'wpturbo_add_shortlink_column' );

function wpturbo_display_shortlink_column( $column_name, $post_id ) {
    if ( $column_name == 'shortlink' ) {
        $shortlink = wp_get_shortlink( $post_id );
        if ( $shortlink ) {
            echo '<a href="' . esc_url( $shortlink ) . '" target="_blank" rel="noopener">' . esc_html( $shortlink ) . '</a>';
        } else {
            echo '-';
        }
    }
}
add_action( 'manage_post_posts_custom_column', 'wpturbo_display_shortlink_column', 10, 2 );
				

This code snippet enables you to add an additional column to the Posts page in the WordPress admin area to display Shortlinks for each post.

The first function wpturbo_add_shortlink_column() is responsible for adding the custom column to the Posts page. The code achieves this by creating a new column called "Shortlink" and returning the new set of columns using the manage_post_posts_columns filter.

function wpturbo_add_shortlink_column( $columns ) {
    $columns['shortlink'] = __( 'Shortlink', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_post_posts_columns', 'wpturbo_add_shortlink_column' );

The next function wpturbo_display_shortlink_column() determines what content should be inserted into the newly created "Shortlink" column for each post. We achieve this by checking the $column_name variable to ensure that the content is only displayed in the Shortlink column, then we use wp_get_shortlink() function to retrieve the shortlink for the current post.

function wpturbo_display_shortlink_column( $column_name, $post_id ) {
    if ( $column_name == 'shortlink' ) {
        $shortlink = wp_get_shortlink( $post_id );
        if ( $shortlink ) {
            echo '<a href="' . esc_url( $shortlink ) . '" target="_blank" rel="noopener">' .esc_html( $shortlink ) . '</a>';
        } else {
            echo '-';
        }
    }
}
add_action( 'manage_post_posts_custom_column', 'wpturbo_display_shortlink_column', 10, 2 );

Once we have the shortlink, we check if there is any value inside the $shortlink variable. If there is, wrap the shortlink inside an <a> tag with the target set to _blank, and we display the shortlink as a clickable link. If there isn’t any value, we display a "-" to signify that there are no shortlinks available.

Finally, we hook the wpturbo_display_shortlink_column() function into the manage_post_posts_custom_column action using add_action(). This action is responsible for displaying the contents of the columns on the Posts page. With this code, we can now see the Shortlink column on the Posts page, which can be useful for quickly sharing posts on social media or other platforms.

Register an account to save your snippets or go Pro to get more features.