Video Tutorial Add columns in administration


For custom content types we will sometimes need to push the administration customization a little further by adding additional columns in the administration listing. For example we want to set up the display of a thumbnail in our property listing to help the administrator select the property he wishes to edit.

First we will add the column to the list of columns thanks to the filter manage _ {$ post_type} _posts_columns. This filter should return an associative array.

add_filter ('manage_post_posts_columns', function ($ columns) {
    $ newColumns = ();
    foreach ($ columns as $ k => $ v) {
        if ($ k === 'date') {
            $ newColumns ('sponso') = 'Sponsored article?';
        }
        $ newColumns ($ k) = $ v;
    }
    return $ newColumns;
});

Then we will have to tell WordPress how to fill our column using the action manage _ {$ post_type} _posts_custom_column.

add_filter ('manage_post_posts_custom_column', function ($ column, $ postId) {
    if ($ column === 'sponso') {
        if (! empty (get_post_meta ($ postId, SponsoMetaBox :: META_KEY, true))) {
            $ class = 'yes';
        } else {
            $ class = 'no';
        }
        echo '
';     } }, 10, 2);