Home News How to Add Admin Columns in WordPress

How to Add Admin Columns in WordPress

by Phill

A number of the major management areas within WordPress shows a table containing a listing of items and a number of their characteristics.

As an instance, the Posts monitor shows a table with a record of articles and a number of their features including writer, groups, tags, number of opinions, and date.

From time to time, you might choose to bring a new feature for screen, or remove a feature from such tables.

You can do this by employing the right filter and action hooks for all one of those tables. Specifically, there’s a filter hook which controls the columns or features that you wish to exhibit, and also an action hook in which you control the real data displayed on your brand new custom columns.

To delete a column, then you merely must unset it from the filter hook purpose.
To add a column that’s already managed by the WordPress program, but not shown by default, you just have to put it at the filter hook purpose.
To include a new custom column, then you’ll have to place it at the filter hook feature, in addition to manage the way to process and display the column info in the activity hook.
This menu now shows a listing of attachments and their features. We wish to add a new column for this table revealing the Tags feature for all the attachment items.

Insert the pins to your own theme or plugin initialization function.

1
2
3
4
<?php
    add_filter('manage_media_columns', 'add_tag_column');
    add_action('manage_media_custom_column', 'manage_attachment_tag_column', 10, 2);
?>

You will find comparable hooks for every one of the additional menus, such as manage_posts_columns and manage_posts_custom_column for those menu.

After we’ve added the pins we specify the add_tag_column filter operate. This function will let us add new columns or delete present columns.

1
2
3
4
5
6
7
8
9
function add_tag_column($posts_columns) {
        // Delete an existing column
    unset($posts_columns['comments']);
       // Add a new column
    $posts_columns['att_tag'] = _x('Tags', 'column name');
    return $posts_columns;
}

Line 3 — Publish the amount of remarks column.
Line 6 — Insert a new custom column known as att_tag.

Next, we will need to specify how information is displayed with this particular column. We do so at the manage_attachment_tag_column function under that is tied into the manage_posts_custom_column action hook.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function manage_attachment_tag_column($column_name, $id) {
    switch($column_name) {
    case 'att_tag':
        $tagparent = "upload.php?";
        $tags = get_the_tags();
        if ( !empty( $tags ) ) {
            $out = array();
            foreach ( $tags as $c )
                $out[] = "<a href='".$tagparent."tag=$c->slug'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'post_tag', 'display')) . "</a>";
            echo join( ', ', $out );
        } else {
            _e('No Tags');
        }
        break;
    default:
        break;
    }
    
}

Next, we have to specify how information is displayed with this particular column. We do so at the manage_attachment_tag_column function under that is tied into the manage_posts_custom_column action hook.

It’s possible to handle more habit columns by generating more entries on your switch statement.

By employing these column filter and action hooks, then you are able to customize what is displayed from the existing WordPress management menus to whatever you desire.

Ultimately, If You Would like to change the a column width, then define it in it is CSS fashion as follows —

1
2
3
<style>
    .column-att_tag { width: 150px; }
</style>

From the case above, our column title is att_tag hence our CSS style course is .column-att_tag.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More