Ask your WordPress questions! Pay money and get answers fast! (more info)

Using WordPress to create Dynamic Pages and be SEO friendly WordPress

I have a website that needs to pull content from a database for web pages. I am building 50 pages manually that contain content dynamically pulled from a database table. These pages are US states, ie: AL, AK, NY, etc.

The dynamic content displayed on these state pages will contain hundreds or thousands of links to detail pages. Think counties in each state as an example.

I want these links to link to dynamically created pages. ie: there is no saved html file on the server. I'd also like these pages to be SEO friendly. ie: somesite.com/state/county.html

A user clicks the California page that pulls the WP file for CA. Within that page is a link or Orange County. When a user requests that page WP pulls the content from the database and displays the page to the user.

Using WordPress how can I accomplish this?


additional info: There will be tens of thousands of these detail pages so creating them manually isn't an option. The information to build the pages is in a database table of my own stored within WPs database. Technically these aren't 'counties,' however that is perfect as an example.

Answers (1)

2012-11-08

John Cotton answers:

I suspect you'll need to create some custom permalinks using the WP_Rewrite class, but you've not given enough info to make a clear recommendation.

What's the content for a "county" page? Where is it stored in the database? Why can't you create a normal page or post for it?

If you can answer those, then an answer to your question may be clearer.


John Cotton comments:

See code below.

You need to create a query var so that WP knows about your custom URL.

Then you need to create some custom permalinks.

Finally, you need to redirect to a custom template file that can retrieve the database info based on the query var value (get_query_var('county')).


function my_add_query_vars( $qvars ) {
$qvars[] = 'state';
$qvars[] = 'county';
return $qvars;
}
add_action('query_vars', 'my_add_query_vars');

function my_add_rewrite_rules( $wp_rewrite ) {
$newrules = array();

// whatever url you want eg states/CA/orange-county
$new_rules['^states/(.*)/(.*)'] = 'index.php?state=$matches[1]county=$matches[2]';

$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'my_add_rewrite_rules');

function my_template_redirect() {

if( get_query_var('county') ) {
include_once(locate_template('template-county-data.php', true));
exit();
}

}
add_action('template_redirect','template_redirect');


wpquestion comments:

I think I'm going to need more explanation. I should say that while I'm not new to programming, WP is pretty new to me.

Where does this code need to go?

I wish I didn't, but I'm going to need fairly explicit directions.

thank you


John Cotton comments:

It needs to go in your theme functions.php file.

There are three steps to making this work:

1/ Add a query var - WordPress is great for custom URLs, but it like to understand them. So add some new parameters for the query string

2/ Create the custom URLs - you can do whatever you want and use regex to avoid the need for hundreds of additions, just make it so that the URLs don't clash with any existing structure - put something unique in the first directory so that it's easily recognised.

3/ Spot that a custom URL has been called, and redirect to a custom template that will handle the logic of retrieving the appropriate data.

In turn, that's what those three functions do.

Probably easiest if you ask explicit questions where any of that doesn't make sense.