Skip to content

Instantly share code, notes, and snippets.

@jonshipman
Last active November 19, 2020 15:54
Show Gist options
  • Save jonshipman/10e2fe0a60a4d4b7b7d519575ec8666d to your computer and use it in GitHub Desktop.
Save jonshipman/10e2fe0a60a4d4b7b7d519575ec8666d to your computer and use it in GitHub Desktop.
Adds themeMods to WP-Graphql
<?php
/**
* Registers the field ThemeMod
*
* @return void
*/
function gql_theme_mod() {
$_mods = get_theme_mods();
$config = array();
$mods = array();
array_walk(
$_mods,
function( $a, $b ) use ( &$mods, &$config ) {
if ( is_string( $a ) || is_int( $a ) ) {
$name = graphql_format_field_name( $b );
$mods[ $name ] = $a;
$config[ $name ] = array(
'type' => is_int( $a ) ? 'Integer' : 'String',
'description' => __( 'An option on the theme.', 'react-build' ),
);
}
}
);
register_graphql_object_type(
'ThemeMod',
array(
'description' => __( 'Gets the theme mods for the active theme.', 'react-build' ),
'fields' => $config,
)
);
register_graphql_field(
'RootQuery',
'ThemeMods',
array(
'type' => 'ThemeMod',
'resolve' => function( $root, $args, $context, $info ) use ( $mods ) {
return $mods;
},
)
);
}
add_action( 'graphql_register_types', 'gql_theme_mod' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment