-
-
Save oddevan/572ca26228ea763434a1c5e4ae203e2f to your computer and use it in GitHub Desktop.
This file contains 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
<?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