Created
July 21, 2026 02:57
-
-
Save wplit/997ac68e5a43b068141ef15866cf37b2 to your computer and use it in GitHub Desktop.
Example: Add 'Favorited Products' column to users list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Add custom column to Users list | |
| add_filter( 'manage_users_columns', 'x_add_user_favorites_column' ); | |
| function x_add_user_favorites_column( $columns ) { | |
| $columns['x_favorited_products'] = 'Favorited Products'; | |
| return $columns; | |
| } | |
| // Output column value | |
| add_filter( 'manage_users_custom_column', 'x_show_user_favorites_column_content', 10, 3 ); | |
| function x_show_user_favorites_column_content( $value, $column_name, $user_id ) { | |
| if ( 'x_favorited_products' !== $column_name ) { | |
| return $value; | |
| } | |
| $favorite_json = get_user_meta( $user_id, 'x_favorite_ids', true ); | |
| if ( empty( $favorite_json ) ) { | |
| return '—'; | |
| } | |
| $favorite_data = json_decode( $favorite_json, true ); | |
| if ( json_last_error() !== JSON_ERROR_NONE || empty( $favorite_data['product'] ) ) { | |
| return '—'; | |
| } | |
| $product_ids = array_map( 'absint', $favorite_data['product'] ); | |
| $titles = array(); | |
| foreach ( $product_ids as $product_id ) { | |
| $title = get_the_title( $product_id ); | |
| if ( $title ) { | |
| $titles[] = esc_html( $title ); | |
| } | |
| } | |
| return $titles ? implode( ', ', $titles ) : '—'; | |
| } |
wplit
commented
Jul 21, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment