-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f76c715
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Registering Types and Fields: Pair Programming with Jacob Arriola & Jason Bahl | ||
|
||
This is the reference repo used during a pair programming session with Jacob Arriola and Jason | ||
Bahl. The essence of this session was to learn how to extend the WPGraphQL plugin's schema. | ||
|
||
## References | ||
* [Jason Bahl Twitter](https://twitter.com/jasonbahl/) | ||
* [WPGraphQL](https://docs.wpgraphql.com/) | ||
* [YouTube of session](https://www.wpgraphql.com/2020/03/30/registering-types-and-fields-pair-programming-with-jacob-arriola/) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
const SEO_TYPE = 'SEO'; | ||
|
||
add_action( 'graphql_register_types', function () { | ||
|
||
register_graphql_object_type( SEO_TYPE, [ | ||
'description' => 'SEO data for a given post', | ||
'fields' => [ | ||
'title' => [ | ||
'type' => 'String', | ||
'description' => 'The SEO title for the post', | ||
'resolve' => function ( $source, $args, $info, $context ) { | ||
|
||
/* @var $yoast \WPSEO_Frontend */ | ||
/* @var $post \WPGraphQL\Model\Post */ | ||
|
||
|
||
$yoast = $source['yoast']; | ||
$post = $source['post']; | ||
|
||
$title = $yoast->get_seo_title( $post ); | ||
|
||
// Provide a fallback if yoast title wasn't set | ||
if ( empty( $title ) ) { | ||
// Go get the Yoast formatted title | ||
$site_title_format = $yoast->get_title_from_options( 'title-' . $post->post_type, $post ); | ||
|
||
$title = sprintf( '%s %s', $post->titleRaw, $site_title_format ); | ||
} | ||
|
||
$yoast->reset(); | ||
|
||
return $title; | ||
}, | ||
], | ||
'description' => [ | ||
'type' => 'String', | ||
'description' => 'The SEO meta description for the post', | ||
'resolve' => function ( $source, $args, $info, $context ) { | ||
$meta = get_post_meta( $source['post']->ID, '_yoast_wpseo_metadesc', true ); | ||
|
||
return ! empty( $meta ) ? $meta : null; | ||
}, | ||
], | ||
], | ||
] ); | ||
|
||
|
||
$allowed_post_types = get_post_types( [ 'show_in_graphql' => true ], 'objects' ); | ||
|
||
if ( ! empty( $allowed_post_types ) ) { | ||
|
||
foreach ( $allowed_post_types as $allowed_post_type ) { | ||
|
||
register_graphql_field( $allowed_post_type->graphql_single_name, 'seo', [ | ||
'type' => SEO_TYPE, | ||
'resolve' => function ( $post, $args, $info, $context ) { | ||
$yoast = \WPSEO_Frontend::get_instance(); | ||
|
||
return [ 'post' => $post, 'yoast' => $yoast ]; | ||
}, | ||
] ); | ||
} | ||
} | ||
|
||
|
||
} ); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|