Tech DocsIntegrationWordPress
Decoupled (headless) support

Decoupled (headless) support

Some sites use WordPress as a back-end service through their REST API and render the front-end using static site generators such as NextJS or Gatsby. In those scenarios, the wp-parsely plugin can still be used to generate the metadata. On such a site:

  • Make sure the tracking script is loaded on your front end. Please refer to the Parse.ly documentation on how to add it.
  • You are responsible for rendering the metadata as well. However, you can use the data that wp-parsely provides through the REST API to populate it.

The plugin adds a parsely field to certain REST API responses. This can be disabled by returning false from the wp_parsely_enable_rest_api_support filter.

// Disable all REST API output from the Parse.ly plugin.
add_filter( 'wp_parsely_enable_rest_api_support', '__return_false' );

The plugin adds the parsely field to endpoints corresponding to the Tracked Post Types and Tracked Page Types selected in the plugin settings. By default, this would be the /wp-json/wp/v2/pages and /wp-json/wp/v2/posts endpoints along with the corresponding single resource endpoints.

This choice of objects types can be further changed by using the wp_parsely_rest_object_types filter.

// Disable REST API output from pages, but enable for term archives.
add_filter(
	'wp_parsely_rest_object_types',
	function( $object_types ) {
		$object_types = array_diff( $object_types, array( 'page' ) );
		$object_types[] = 'term';
		return $object_types;
	}
);

The parsely field contains the following fields:

  • version, which is a string identifying the version of the REST API output; this will be updated if changes are made to the output, so consuming applications can check against it.
  • meta, which is an array of metadata for the specific post, page, or other custom post type.
  • rendered, which is the rendered HTML of the metadata for the post, page, or other custom post type. This will be a JSON-LD <script> tag, or a set of <meta> tags, depending on the format selected in the plugin settings. The decoupled code can consume and use this directly, instead of building the values from the meta field values.

The rendered field is a convenience field containing the HTML-formatted metadata which can be printed to a decoupled page as is. This can be disabled by returning false from the wp_parsely_enable_rest_rendered_support filter.

// Disable rendered field output from the REST API output.
add_filter( 'wp_parsely_enable_rest_rendered_support', '__return_false' );

This will return the REST API to the default.

GraphQL is not supported out of the box, but some customizations can be made on your theme's code to make it work. Please contact support for more details.

Ready to get started with Parse.ly?

Questions?

Or

rocket emoji