Skip to content

Instantly share code, notes, and snippets.

@wplit
Created July 21, 2026 02:57
Show Gist options
  • Select an option

  • Save wplit/997ac68e5a43b068141ef15866cf37b2 to your computer and use it in GitHub Desktop.

Select an option

Save wplit/997ac68e5a43b068141ef15866cf37b2 to your computer and use it in GitHub Desktop.
Example: Add 'Favorited Products' column to users list
// 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

wplit commented Jul 21, 2026

Copy link
Copy Markdown
Author
CleanShot 2026-07-21 at 12 54 48@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment