Skip to content

Instantly share code, notes, and snippets.

@matezito
Created August 26, 2021 17:34
Show Gist options
  • Save matezito/3f357454a3daf7a03dd8ceed58a0ffeb to your computer and use it in GitHub Desktop.
Save matezito/3f357454a3daf7a03dd8ceed58a0ffeb 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();
}
]);
};
@webwidely
Copy link

Thank you so much Sir, it's very helpful for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment