I have an external database and I want to serve data to pages from the database.
A similar example would be location data within this database. Imagine 500 locations. We want to be able to either serve a location from a WordPress page or if the page doesn’t exist in WordPress serve out a template and client side in the page inject the appropriate JSON data in the page. We have it setup to inject the json data via client-side script.
Below is an example:
/locations/north-america/united-states/colorado/denver/ – if this page exists then there is likely custom content on the page and we want to serve out the page and insert the json data in the browser
Another Example:
/locations/north-america/united-states/colorado/colorado-springs/ – If this page doesn’t exist we want to treat it like a virtual page and generate the page from a template.
It’s important to note that SEO is important. I’ve been researching rewrite rules and permastruct. I’m not 100% sure that I know the direction to go and how to accomplish with a customization.
Another note:
The following pages may not exist, but we want to dynamically create them from different templates (the first using a country template and the second a city template):
/locations/north-america/canada
Or
/locations/north-america/canada/banff
We’ve tried investigating permastrcutres and rewrite rules. Not exactly the best approach to take.
[ad_2]
I think you’re going to need to publish the pages so the url exists and then inside your page template, check to see if the_content() has content or not. If it has content, display the custom stuff you want, if not use the json data to populate the page from the external db.
Will the location pages need to be editable through the WordPress admin by non-technical users? Or is this more of a read-only front-end?
If you don’t need the pages integrated with the WP admin interface, then using the built-in permalink and rewrite rule system is probably not the optimal approach. It’s designed more for mapping URLs to posts/pages in the database.
Instead, you could register a custom rewrite rule using the add_rewrite_rule() function. This will allow you to define a URL pattern like /locations/{country}/{state}/{city}/ and map it to a custom page template or plugin that can parse the URL and query your custom database table.
The advantage is it keeps this separate from WordPress’s main routing system. You have complete control over the logic for mapping those URLs to your listings. You could put this in a plugin or custom page template.
Keep in mind that you’ll need to add conditional logic in your custom page template to check if a page exists at the current URL using a function like get_page_by_path(). If a page is found, load its content. If not, default to the generic location template.
Some other things to consider if you go this route:
– Make sure to flush the rewrite rules when you register your custom rule
– You’ll need to handle the parsing of the URL parameters in your custom code and use them to query the right location data
– For performance, look into caching the database queries if you expect a lot of traffic
– You may also want to generate your own XML sitemap for these location pages to help with SEO