Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active October 19, 2021 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/21ac4723d6147e6e67349d17b7eac011 to your computer and use it in GitHub Desktop.
Save kellenmace/21ac4723d6147e6e67349d17b7eac011 to your computer and use it in GitHub Desktop.
WPGraphQL User Meta Example
<?php
namespace MyCoolApp;
use WPGraphQL\Model\User;
class UserFields {
public function register_hooks() {
add_action( 'graphql_register_types', [ $this, 'register_fields' ] );
}
public function register_fields() {
register_graphql_fields( 'User', [
'favoriteColor' => [
'type' => 'String',
'description' => __( 'User\'s favorite color', 'my-cool-app' ),
'resolve' => function( User $user ) {
$favorite_color = get_user_meta( $user->fields['userId'], 'favorite_color', true );
return $favorite_color ?: null;
},
],
'favoriteFood' => [
'type' => 'String',
'description' => __('User\'s favorite food', 'my-cool-app'),
'resolve' => function ( User $user ) {
$favorite_food = get_user_meta( $user->fields['userId'], 'favorite_food', true );
return $favorite_food ?: null;
},
],
] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment