WordPress Car Listings: Widget or REST API?
WordPress can use an external API. You do not have to use a widget simply because your site runs on WordPress. The right integration depends on how much control you need, whether the listings must be present in the initial HTML for search engines, and whether you can install a small plugin or edit your theme.
For most dealer, exporter and car-import websites, there are three sensible approaches:
- Paste the XAPI Korea widget into a Custom HTML block.
- Wrap the widget in a WordPress shortcode when the editor removes scripts or custom elements.
- Call the REST API from PHP when you need custom templates, server-rendered content or deeper data processing.
Option 1: paste the widget into a page
The fastest route is the browser-side listings widget. In the WordPress block editor, add a Custom HTML block and paste:
<script src="https://xapikorea.com/widget.js" async></script>
<xapi-cars
pk="pk_live_..."
brand="hyundai"
year-from="2022"
limit="8"
></xapi-cars>
The script registers the <xapi-cars> element and loads the matching listings after the page opens. You can change the brand, model, year, price, fuel type, sort order, theme and other supported widget attributes without writing JavaScript.
Use a widget key beginning with pk_live_. It is a publishable key designed to appear in page source. Add your WordPress domain to that key's allowlist in Dashboard → Widgets before testing the grid.
WordPress may remove <script> tags and unfamiliar HTML elements when the editor account does not have permission to publish unfiltered HTML. If the snippet disappears after saving, do not weaken WordPress security settings just to make the embed work. Use the shortcode approach below instead.
Option 2: make a reusable WordPress shortcode
A small site-specific plugin can load the widget script through WordPress and return the custom element from a shortcode. This is also more maintainable when the same listing grid appears on several pages.
Create a plugin file such as wp-content/plugins/xapi-korea-widget/xapi-korea-widget.php:
<?php
/**
* Plugin Name: XAPI Korea Widget
* Description: Adds an [xapi_cars] shortcode.
*/
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script(
'xapi-korea-widget',
'https://xapikorea.com/widget.js',
array(),
null,
array('strategy' => 'async')
);
});
add_shortcode('xapi_cars', function ($attributes) {
$attributes = shortcode_atts(array(
'pk' => '',
'brand' => '',
'year_from' => '',
'limit' => '8',
), $attributes, 'xapi_cars');
if (strpos($attributes['pk'], 'pk_live_') !== 0) {
return '<p>Configure a valid XAPI Korea widget key.</p>';
}
return sprintf(
'<xapi-cars pk="%s" brand="%s" year-from="%s" limit="%s"></xapi-cars>',
esc_attr($attributes['pk']),
esc_attr($attributes['brand']),
esc_attr($attributes['year_from']),
esc_attr(max(1, min(12, absint($attributes['limit']))))
);
});
Activate the plugin, then place this in any post or page:
[xapi_cars pk="pk_live_..." brand="bmw" year_from="2021" limit="6"]
WordPress recommends loading frontend JavaScript with wp_enqueue_script(), and its Shortcode API provides the supported way to turn a compact tag into generated page markup. Keeping this code in a small plugin also prevents a theme update from deleting it.
Option 3: call the REST API from WordPress PHP
Use the REST API when the widget's layout or supported filters are not enough. A server-side integration can reshape the response, combine it with WordPress content, render your own cards and build dedicated inventory pages.
WordPress includes an HTTP API, so a plugin can call XAPI Korea without requiring raw cURL code:
$response = wp_remote_get(
'https://api.xapikorea.com/v1/search?brand=hyundai&year_from=2022&limit=8&lang=en',
array(
'timeout' => 10,
'headers' => array(
'X-API-Key' => XAPI_KOREA_API_KEY,
),
)
);
if (is_wp_error($response)) {
return '<p>Vehicle listings are temporarily unavailable.</p>';
}
$status = wp_remote_retrieve_response_code($response);
$data = json_decode(wp_remote_retrieve_body($response), true);
if (200 !== $status || !isset($data['results'])) {
return '<p>Vehicle listings are temporarily unavailable.</p>';
}
enc_... API keys are secret. Never place one in JavaScript, a Custom HTML block, a shortcode attribute or a public Git repository. Define it on the server—for example, through an environment variable read by wp-config.php—and expose only the rendered result to visitors.
Cache successful API responses with the WordPress Transients API. Without caching, every page view could become a new external request, slowing the page and consuming API quota. A short cache can keep pages responsive while still refreshing inventory regularly:
$cache_key = 'xapi_korea_hyundai_2022';
$data = get_transient($cache_key);
if (false === $data) {
// Run wp_remote_get(), validate the response, then cache its decoded data.
set_transient($cache_key, $data, 5 * MINUTE_IN_SECONDS);
}
Always handle timeouts and non-200 responses, validate the returned structure, and escape every value when generating HTML with functions such as esc_html(), esc_attr() and esc_url().
Widget or API: which should you choose?
| Requirement | Widget | REST API from PHP |
|---|---|---|
| Fastest setup | Best fit | More development work |
| WordPress plugin required | Usually no | Yes, or custom theme code |
| Key used on the page | Publishable pk_live_... | Secret enc_... stays server-side |
| Custom design and business logic | Focused attributes and themes | Full control |
| Advanced search filters | Limited widget subset | Full supported API filters |
| WordPress-side caching | Not required for the embed | Strongly recommended |
| Listings in the initial HTML | No, loaded by JavaScript | Yes, when rendered in PHP |
Google can render JavaScript, but server-rendered HTML is more direct for crawlers and is also visible to search engines that do not execute JavaScript. If the grid supports a useful article or landing page, the widget is a practical enhancement—keep meaningful explanatory text on the page as well. If individual inventory pages and their listing content are central to your SEO strategy, use the REST API and render those pages on the server.
A practical starting point
Start with the widget when you want a live grid on a dealer homepage, import guide or campaign page. Move to the API when the business needs custom inventory templates, comparison tools, saved searches, pricing calculations or tighter integration with WordPress data.
For all widget attributes, domain rules and request limits, read the complete widget guide. To build the server-side version, continue with the REST API search walkthrough and the interactive API reference.
WordPress is not the limitation: it supports both browser embeds and server-side API requests. The real choice is speed of setup versus control over rendering, caching and search visibility.
