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: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you run a WordPress site, you know how important it is to keep track of your post’s performance. One way to do this is by adding a custom column to the admin posts page that displays the shortlink for each post. This way, you can easily share your post on social media or track clicks on the link. In this article, we’ll show you how to use wp_get_shortlink to add a custom admin post column displaying the shortlink for each post.

					function wpturbo_add_shortlink_column($columns) {
    $columns['shortlink'] = 'Shortlink';
    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);
        echo '<a href="'.$shortlink.'" target="_blank">'.$shortlink.'</a>';
    }
}
add_action('manage_post_posts_custom_column', 'wpturbo_display_shortlink_column', 10, 2);
				

The code snippet allows you to add a custom column to the WordPress admin posts page which displays the post’s shortlink. By default, WordPress does not include a shortlink column on the posts page in the dashboard.

The first function, wpturbo_add_shortlink_column, adds the new column to the post list table. It takes an array of columns as its parameter and returns the new array with the ‘shortlink’ column added.

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

Here, we’re adding a new column to the $columns array with the key 'shortlink' and a value of 'Shortlink'. This will display the column with the header ‘Shortlink’ on the post list table.

The add_filter() function hooks the wpturbo_add_shortlink_column() function into the manage_post_posts_columns action, which allows us to modify the columns displayed on the post list table in the WordPress admin.

The second function, wpturbo_display_shortlink_column, adds the data to be displayed in the new column. It accepts two parameters: $column_name (the name of the column being displayed) and $post_id (the ID of the post being displayed).

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

Here, we check if the $column_name is equal to 'shortlink', which ensures that the function only affects the 'shortlink' column. Then, we use the wp_get_shortlink() function to retrieve the shortlink for the post, passing in $post_id as its parameter.

We wrap the shortlink in an anchor tag and output it to the page, targeting it to open in a new window. This allows users to quickly get the shortlink for any post without having to open the post itself.

Finally, we use the add_action() function to hook the wpturbo_display_shortlink_column() function into the manage_post_posts_custom_column action, to add the column data for the 'shortlink' column.

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