Created
June 17, 2021 14:36
-
-
Save hsimah/999dfe74928384313bd758bcb9d5db1c to your computer and use it in GitHub Desktop.
wpgraphql_register_custom_mutation_inputs.php
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
<?php | |
$graphql_singular_name = 'Post'; | |
$graphql_field_type = 'String'; | |
$field_config = [ | |
'name' => 'The human readable ACF field name', | |
'graphql_name' => 'The GraphQL field name, eg first_name', | |
]; | |
add_filter('graphql_input_fields', function ($fields, $type_name) use ($graphql_singular_name, $graphql_field_type, $field_config) { | |
if ($type_name === "Create{$graphql_singular_name}Input" || $type_name === "Update{$graphql_singular_name}Input") { | |
// add field as input untested with non-scalar fields | |
['name' => $name, 'graphql_name' => $graphql_name] = $field_config; | |
$fields[$graphql_name] = [ | |
'type' => $graphql_field_type, | |
'description' => $name, | |
]; | |
} | |
return $fields; | |
}, 10, 2); | |
add_action('graphql_post_object_mutation_update_additional_data', function (int $post_id, array $input) use ($field_config) { | |
// update the meta value | |
['id' => $id, 'graphql_name' => $graphql_name] = $field_config; | |
rwmb_set_meta($post_id, $id, $input[$graphql_name]); | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment