Code snippet to display ID gives critical error

Based on this post, I added the code snippet below to my WordPress site, as to display the ID in the Posts section of each post. However, it returns “critical error”. Any idea what is wrong with this code?

add_filter( 'manage_posts_columns', 'column_register_wpse_101322' );
add_filter( 'manage_posts_custom_column', 'column_display_wpse_101322', 10, 3 );

function column_register_wpse_101322( $columns ) {
    $columns['uid'] = 'ID';
    return $columns;
}

function column_display_wpse_101322( $empty, $column_name, $post_id ) {
    if ('uid' != $column_name) {return $empty;}
    return "$post_id";
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Unfortunately you can’t just copy code that plays a completely different role and replace users with posts and somehow expect this to do what you want it. The reason you get a fatal error is because you’re passing in too many arguments (3) to the filter, when there are only 2 supplied.

Here’s the code to achieve what you need, if I understand you correctly:


add_filter( 'manage_posts_columns', 'column_register_wpse_101322' );
add_action( 'manage_posts_custom_column', 'column_display_wpse_101322', 10, 2 );

function column_register_wpse_101322( $columns ) {
    $columns[ 'uid' ] = 'ID';
    return $columns;
}

function column_display_wpse_101322( $column_name, $post_id ) {
    if ( 'uid' === $column_name ) {
        echo $post_id;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x