unserialize(): Error - When using custom admin column

Support MB Admin Columns unserialize(): Error - When using custom admin columnResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #47257
    CodogCodog
    Participant

    Hi there,
    I implemented a custom admin column function following your documentation a while back and all has been working fine. But recently I noticed an "unserialize(): Error at offset 1347 of 2148 bytes" in my Query Monitor. The custom admin column displays fine and before the title as expected. If I remove this function completely the "unserialize(): Error" disappears.

    MY CODE:
    ========

    add_action( 'init', 'pl_add_custom_columns', 20 );
    function pl_add_custom_columns() {
        class Pl_Custom_Admin_Columns extends \MBAC\Post {
            public function columns( $columns ) {
                $columns  = parent::columns( $columns );
                $position = 'before';
                $target   = 'title';
                $this->add( $columns, '_thumbnail_id', 'Featured Image', $position, $target );
                return $columns;
            }
            public function show( $column, $post_id ) {
                switch ( $column ) {
                    case '_thumbnail_id':
    					$img_url = wp_get_attachment_image_url( get_post_thumbnail_id( $post_id ), 'thumbnail' );
                        if( !empty( $img_url ) ) {
                            echo '<img class="admin-featured-img" src="' . $img_url . '" alt="'.get_the_title().'" title="'.get_the_title().'" />';    
    					} else {
    						echo '<img class="admin-featured-img" src="'. esc_url(get_stylesheet_directory_uri()).'/img/img-fallback.svg" alt="No Featured Image" title="No Featured Image">';
    					}
                        break;
                    // More columns
                }
            }
        }
        
        new Pl_Custom_Admin_Columns( 'custom_post_type', array() );
    }

    * Note this method was used to display the featured image in the admin column because the _thumbnail_id uses the 'type' => 'file_upload'.

    Q) Is my implementation correct?
    Q) Is there a better method?
    Q) Is this a known bug?

    I look forward to your response 🙂

    #47270
    PeterPeter
    Moderator

    Hello,

    Do you add/upload more featured images to the field _thumbnail_id? Because the field type file_upload allows uploading multiple files/images.
    You can try to use the field type single_image to recheck this issue. Following the documentation
    https://docs.metabox.io/fields/single-image/

    #47274
    CodogCodog
    Participant

    Hi Peter,
    thanks for your response. However, in my context I cannot use another field type such as the single_image as this provides access to the media library popup. My file_upload field is part of a frontend submission form that MUST prevent my users from accessing the media library.

    Are there any other work arounds or options to fix this error?

    Thanks!

    #47302
    PeterPeter
    Moderator

    Hello,

    Can you please move the class Pl_Custom_Admin_Columns to a separate file and include it in the callback function pl_add_custom_columns as noted in the documentation?
    https://docs.metabox.io/extensions/mb-admin-columns/#custom-admin-columns

    I follow the documentation, use your code to show a custom admin column for the field _thumbnail_id but don't see that issue. Please recheck this and let me know how it goes.

    #47321
    CodogCodog
    Participant

    Hi Peter,
    thanks for the guidance. After some further debugging, turns out the unserialize() issue I was experiencing originated from a completely unrelated function. My apologies.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.