Skip to content

Instantly share code, notes, and snippets.

@oddevan
Last active March 15, 2021 15:46
Show Gist options
  • Save oddevan/572ca26228ea763434a1c5e4ae203e2f to your computer and use it in GitHub Desktop.
Save oddevan/572ca26228ea763434a1c5e4ae203e2f to your computer and use it in GitHub Desktop.
<?php
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Album', 'externalLinks', [
'type' => 'String',
'description' => __( 'Links to the album on external services', 'smolblog' ),
'resolve' => function( $post ) {
// Get the meta field
$links = get_post_meta( $post->ID, 'find_links', true );
// Return an empty JSON object if the meta field is empty
if (empty($links)) {
return '{}';
}
// If $links isn't an array (there's only one link), make it an array.
if ( ! is_array( $links ) ) {
$links = [ $links ];
}
// JSON encode the array and return it
return wp_json_encode( $links );
}
] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment