Skip to content

Instantly share code, notes, and snippets.

@webwidely
Forked from matezito/grapqlexample.php
Created October 14, 2021 16:31
Show Gist options
  • Save webwidely/875caa628140aabe6b5e6cb5e256e4f4 to your computer and use it in GitHub Desktop.
Save webwidely/875caa628140aabe6b5e6cb5e256e4f4 to your computer and use it in GitHub Desktop.
Extender WPGraphQL
<?php
/**
* Plugin Name: GraphQL Example
* Textdomain: graphqlexample
*/
/**
* Add logo and favicon images url to graphql schema in the rootquery. copy file into wp-content/plugins and then activate from Plugin's page.
**/
function get_logo()
{
$custom_logo_id = get_theme_mod('custom_logo');
$image = wp_get_attachment_image_src($custom_logo_id, 'full');
return $image[0];
}
function get_favicon()
{
return get_site_icon_url(32);
}
add_action('graphql_register_types', 'example_extend_wpgraphql_schema');
function example_extend_wpgraphql_schema()
{
register_graphql_field('RootQuery', 'logoUrl', [
'type' => 'String',
'description' => __('Show logo image', 'graphqlexample'),
'resolve' => function () {
return get_logo();
}
]);
register_graphql_field('RootQuery', 'faviconUrl', [
'type' => 'String',
'description' => __('Show favicon', 'graphqlexample'),
'resolve' => function () {
return get_favicon();
}
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment