API returns should always be in JSON format. It’s up to the calling app to show the end user nice messaging based upon the returned values. It’s feasible to encode HTML as JSON, but then the calling app still needs to decode it for display to the user.
I am trying to make a custom unsubscribe process for users with a specific role, would it be better to use another process?
The “best” approach depends on the desired user experience, attenuated by one’s coding skills. If you can manage custom end points, your skills are unlikely a limiting factor. Another consideration is whether you use a modern block theme or one of the older classic themes.
You could build a custom page that manages the process and displays appropriate messages. You can then link to this page in the normal manner from anywhere else. The requisite PHP code would reside in either a bespoke page template (classic) or pattern (block). Or you could instead do this from a custom admin page; thus a user with the appropriate role could see a link in their dashboard as well as anywhere else you’ve placed the link.
If you’d rather the user not be taken to a different page, in order to unsubscribe, it could be managed via traditional Ajax. Then your PHP handler could return data in any format you want, though you’d still need some JS code to actually display the data. If you want returned HTML data to be displayed directly without further interpretation by client side code, you should implement the custom page approach. It’ll then be necessary that the user be taken to a new page.
I went with the custom page approach.
thanks