WP Poet – Including function in config file

I’m using Log1x’s Poet package to add a custom post type, and with that, add a few custom columns to the post type. Columns are a feature of extended-cpts, but aren’t documented in Poet, so it may be the case that it’s not possible, but I thought i’d ask to see if anyone has a suggestion.

This is the code in question
Edit: this does work fine until I run wp acorn optimize which then flags up errors.

...
'admin_cols' => [
   'post_excerpt' => [
     'title'    => 'Excerpt',
     'function' => function () {
        echo 'example thing to echo';
      },
  ]
],
...

This isn’t serialisable, so it gives me theses errors on wp acorn optimize: Your configuration files are not serializable. and Call to undefined method Closure::__set_state() .

I’ve tried using a value function: value(function() { echo 'example thing to echo'; }) which stops the error but doesn’t display in the column and i’ve also tried including the function outside of the object, which again stopped the error but doesn’t display anything.

If anyone has any suggestions i’d appreciate being pointed in the right direction :slightly_smiling_face:

yup, nest that in admin_cols: eg:

'admin_cols'          => [
    'featured_image' => [
        'title'          => 'Featured Image',
        'featured_image' => 'thumbnail',
    ],
    'event_type'      => [
        'taxonomy' => 'event_type',
    ],
    'event_date'     => [
        'title'          => 'Event Date',
        'function'       => function () {
            echo get_field('event_date') ? str_replace('/', '-', get_field('event_date')) : null;
        },
    ],
    'event_live'     => [
        'title'          => 'Event Live',
        'function'       => function () {
            $event_date = get_field('event_date') ? str_replace('/', '-', get_field('event_date')) : null;
            echo $event_date ? (strtotime($event_date) > strtotime('now') ? '🟢 Live' : 'Past') : null;
        },
    ],
    'event_location'     => [
        'title'    => 'Location',
        'meta_key' => 'event_location',
    ],
    'event_price'     => [
        'title'    => 'Price',
        'meta_key' => 'event_price',
    ],
],

Hey @slowrush, thanks for your quick response!

Am I missing something, or are we adding columns in the same way? For reference, i’ve pasted the full post type below – the column portion is giving me the Your configuration files are not serializable error on wp acorn optimize.

'post' => [
        'our-work' => [
            'enter_title_here' => 'Enter Project Name',
            'menu_icon' => 'dashicons-media-document',
            'supports' => ['title', 'editor', 'revisions', 'excerpt', 'thumbnail'],
            'show_in_rest' => true,
            'has_archive' => false,
            'menu_position' => 7,
            'public' => true,
            'show_in_rest' => true,
            'labels' => [
                'singular' => 'Post',
                'plural' => 'Work',
            ],
            'rewrite' => [
                'slug' => 'our-work',
                'with_front' => false
            ],
            'admin_cols' => [
                'tagline' => [
                    'title'    => 'Tagline',
                    'function' => function () {
                        echo 'test';
                    },
                ],
                'featured_image' => [
                    'title'          => 'Feature Image',
                    'featured_image' => 'medium',
                    'height'         => 40,
                ],
                'post_excerpt' => [
                    'title'    => 'Excerpt',
                    'function' => function () {
                        echo 'test';
                    },
                ]
            ],
        ],
    ],

aha it’s extending an existing post type post_type: post - there’s an issue on the Poet repo from a while back with more info + workarounds. The above will work for everything except for post / page as far as I know.

Hey thanks for sending over that issue, i’ll take a look!

In this case i’m registering a custom post type called ‘our-work’, apologies the copy and paste isn’t clear, the post at the start is wrapping around the registered post types.

aha so you are! :sweat_smile:

Just pasted your config in and admin_cols seems to work my side :person_shrugging:

Added note: I also get this error when running wp acorn optimize but admin cols still show up. Maybe worth popping an issue up on the Poet repo

1 Like

That’s good to know – i’ll create an issue. Thanks for testing it on your end.

1 Like

This turned out to have a simple resolution in the end – i’ve posted my solution on the issue.

2 Likes