Headless WordPress – Formatting URLs returned in JSON data so they work with client app

[ad_1]

Heyooo!

I’m wondering what the opinions are and possible solutions for doing this. Currently I have a WordPress website hosted at [example.wordpress.com]) and a Next.js client app hosted at [example.client.com]).

Using the REST API, the JSON returned contains WYSIWYG fields and link fields with the WordPress url (ex. [example.wordpress.com/test]) in them however, I need it actually be [example.client.com/test]) so that the routing works correctly in my Next.js app.

Currently, in my Next.js app I run a formatting function on every field that might contain a link and it find and replaces the WordPress URL with my Next.js URLs. That just doesn’t seem like a good solution and the WordPress REST API should return the correct Next.js URLs ideally.

Hope that makes sense. How do you all handle this? Thanks for the help!! 🙏

[ad_2]
3 Comments
  1. Hey there, Is there an issue with your solution? Just trying to understand it better.

  2. >…the WordPress url (ex. example.wordpress.com/test in them however, I need it actually be example.client.com/test so that the routing works correctly in my Next.js app.

    It seems you’re missing something in your understanding of REST APIs and routing in JS frameworks as the external REST API URL should have absolutely no bearing on the routing of the app.

  3. The best way would be to return relative urls from the WordPress . Maybe try this plugin : [https://wordpress.org/plugins/make-paths-relative/])

    I am working on a similar project as well. Are you also logging the user into your nextJS site ?

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer