Skip to content

Commit

Permalink
refactor: rename _tenup_preview_link to _headless_wp_preview_link
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Mar 11, 2024
1 parent 85f4f2e commit d153b09
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/documentation/06-WordPress Integration/previews.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ The `preview.usePostLinkForRedirect` was added in `@headstartwp/[email protected]` and

This becomes even more useful when you have a more complicated permalink structure, let's say your `news` post type adds the category name to the url such as `/news/political/news-name`. If both your Next.js project and WordPress are following the same permalink structure, no additional logic is required to get previews to work. Previously permalinks like this would require providing custom logic in `getRedirectPath`.

Note that by default, `draft` posts will not have a pretty permalink, instead, they have something like `domain.com/?p=id` so HeadstartWP adds a new rest field for all public post types called: `_tenup_preview_link` which will return a pretty permalink even for draft posts. This field will be used by the previewHandler for draft posts.
Note that by default, `draft` posts will not have a pretty permalink, instead, they have something like `domain.com/?p=id` so HeadstartWP adds a new rest field for all public post types called: `_headless_wp_preview_link` which will return a pretty permalink even for draft posts. This field will be used by the previewHandler for draft posts.

If you are overriding permalink in WordPress via filter you must ensure that draft posts have a fallback post name. e.g:

Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/handlers/previewHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ export async function previewHandler(
if (preview?.usePostLinkForRedirect) {
if (
result.status === 'draft' &&
typeof result._tenup_preview_link === 'undefined'
typeof result._headless_wp_preview_link === 'undefined'
) {
throw new Error(
'You are using usePostLinkForRedirect setting but your rest response does not have _tenup_preview_link, ensure you are running the latest version of the plugin',
'You are using usePostLinkForRedirect setting but your rest response does not have _headless_wp_preview_link, ensure you are running the latest version of the plugin',
);
}
const link = result._tenup_preview_link ?? result.link;
const link = result._headless_wp_preview_link ?? result.link;
return removeSourceUrl({ link: link as string, backendUrl: sourceUrl ?? '' });
}

Expand Down
4 changes: 2 additions & 2 deletions wp/headless-wp/includes/classes/Preview/PreviewLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PreviewLink {
public function register() {
add_filter( 'template_include', [ $this, 'handle_preview' ], 20 );

// only add _tenup_preview_link for preview authenticated requests
// only add _headless_wp_preview_link for preview authenticated requests
if ( PreviewToken::get_payload_from_token() ) {
add_action( 'rest_api_init', [ $this, 'add_preview_link_field' ] );
}
Expand All @@ -35,7 +35,7 @@ public function add_preview_link_field() {
foreach ( $post_types as $post_type ) {
register_rest_field(
$post_type,
'_tenup_preview_link',
'_headless_wp_preview_link',
[
'get_callback' => function ( $post_object ) {
return $this->get_draft_permalink( get_post( $post_object['id'] ) );
Expand Down

0 comments on commit d153b09

Please sign in to comment.