I have a page running a custom shortcode. When an element on the page is clicked I am pushing a new url into the browser (using history.pushstate) and displaying the content in the same window.
If this url is copied and pasted at a later date into the browser, I want to be able identify this and display the content. As the requested element is not actually a real page/post, I am having difficulty. I can use template_redirect hook to identify if it is the link I am interested in, and I can forward the page - but I don't want to forward, I want to keep the url but load a specific page.
Is there a way of doing this?
I have a page running a custom shortcode. When an element on the page is clicked I am pushing a new url into the browser (using history.pushstate) and displaying the content in the same window.
If this url is copied and pasted at a later date into the browser, I want to be able identify this and display the content. As the requested element is not actually a real page/post, I am having difficulty. I can use template_redirect hook to identify if it is the link I am interested in, and I can forward the page - but I don't want to forward, I want to keep the url but load a specific page.
Is there a way of doing this?
Share Improve this question asked Mar 12, 2022 at 10:31 StripyTigerStripyTiger 2671 silver badge6 bronze badges 5- 1 I don't think you need WordPress knowledge to solve this, your JS application that the shortcode loads in needs to be able to read the URL and adjust itself. E.g. in react you might use a library such as react router. Routing and routers are the term you're looking for, as a redirect by definition sends you to a new URL – Tom J Nowell ♦ Commented Mar 12, 2022 at 13:09
- Its not js that loads the shortcode - the shortcode is loaded as part of the initial page load, server side by Wordpress – StripyTiger Commented Mar 12, 2022 at 15:35
- Can you include the code in your question and explain using different words to be as clear and precise as possible? I never stated that JS loaded the shortcode in my comment, there has been a misunderstanding of what I said – Tom J Nowell ♦ Commented Mar 12, 2022 at 21:14
- Sorry, I did misunderstand you, but I do not want to deal with it on the js side. I have found a solution using a Virtual Page. Thanks for your help though. – StripyTiger Commented Mar 13, 2022 at 9:41
- that shouldn't be possible based on the information you shared, you've left out important details about what this shortcode does and should update your question. Sharing the code would make it much clearer – Tom J Nowell ♦ Commented Mar 13, 2022 at 12:07
1 Answer
Reset to default 0This can be achieved using a Virtual Post. See this link on how to do it. https://ashiqur.com/how-to-add-a-virtual-page-in-wordpress/