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

How to structure WordPress plugin to display data from API WordPress

I am creating a plugin that connects to a remote API that has car listings and displays the data that's returned in that call.

The plugin essentially does not need to interact with the WordPress database at all.

I already know I can use things like wp_remote_get, file_get_contents and file_get_contents to retrieve that data.

My problem is that I'm not sure if I should be creating a plugin for this or just shortcodes or page-templates or what.

I want the urls to look like:


example.com/cars/ //Cars listing results
example.com/cars/2 //Page 2 of Cars listing results
example.com/cars/?doors=4&engine=v8 //Search parameters for Cars listing results
example.com/car/1234 //Car info page 1234 is the car_id


I was thinking I could create pages in WordPress called <strong>cars</strong> and <strong>car</strong> and then create custom shortcodes that would hook into some functions I build to interact with the API, but for some reason that doesn't seem like the best idea to me.

Can anyone recommend a better structure for setting this up?

Answers (2)

2014-08-08

timDesain Nanang answers:

the simplest and best way (SEO, url_rewrite, editing, etc) is registering post type / taxonomy and insert the data into _posts table

- car as custom post type
- cars as custom taxonomy
- doors as custom taxonomy
- engine as custom taxonomy

hopefully this colud help:
[[LINK href="http://wpquestions.com/question/showChronoLoggedIn/id/9667"]]http://wpquestions.com/question/showChronoLoggedIn/id/9667[[/LINK]]
[[LINK href="http://codex.wordpress.org/Function_Reference/wp_insert_post"]]http://codex.wordpress.org/Function_Reference/wp_insert_post[[/LINK]]


bigmike7801 comments:

Thanks for your response. But as I dated stated, this does not need to interact with the database. In fact it should not.


timDesain Nanang comments:

then, you can do it:
<blockquote>I was thinking I could create pages in WordPress called cars and car and then create custom shortcodes that would hook into some functions I build to interact with the API, but for some reason that doesn't seem like the best idea to me. </blockquote>

create pages and custom shortcodes can achieve that.

2014-08-10

tormorten answers:

You could do it by adding rewrite rules to WordPress using the WordPress Rewrite API and then registering some custom query variables.

You could include your own template for this sort of this (and would not have to worry about page templates) using the template_redirect hook.

Here's how it could look:

function my_switch_template() {

if( get_query_var( 'car_template' ) ) {

$page = get_query_var( 'car_template' ) == 'car' ? 'single' : 'archive';

$file = PATH_TO_PLUGIN_TEMPLATES . '/cars-' . $page . '.php';

load_template( $file );

exit;

}

}

add_action( 'template_redirect', 'my_switch_template' );

function my_rewrite_rules($rules) {
$newrules = array();
$newrules['cars/(.+)'] = 'index.php?car_template=cars&car_page=$matches[1]';
$newrules['cars/'] = 'index.php?car_template=cars';
$newrules['car/(.+)'] = 'index.php?car_template=car&car_id=$matches[1]';
$finalrules = $newrules + $rules;
return $finalrules;
}

add_filter( 'rewrite_rules_array', 'my_rewrite_rules' );

function my_query_vars( $qvars ){

$qvars[] = 'car_template';
$qvars[] = 'car_page';
$qvars[] = 'car_id';

return $qvars;
}

add_filter( 'query_vars', 'my_query_vars' );


The search variables could still be regular $_GET parameters, or you could incorporate them into the rewrite rules.