admin管理员组文章数量:1122832
I have placed my external api call inside the functions.php file in the server and I created a shortcode for the api call.
I'm familiar with React and I was expecting Wordpress to be somewhat similar. For example being able to grab the results, loop, and display in a list/component
sendExternalApiCall().then((res) => {
return (
res.listOfResults.map((item) => {
return <div> item.title </div>
})
)
})
I've tried googling/watching Youtube videos but most examples of dynamic content are using Custom post types, custom field options, but all this is seemingly still very manual effort and I'm having a hard time piecing together how I can grab my list of results from my api e.g.
results = [{title: 'first post'}, {title: 'second post'}]
loop through those results and render then on the page inside a wordpress widget. Am I able to do this or do I have to create a custom widget? I would love to be able to use existing widgets like elementor but I don't know how to integrate them
I have placed my external api call inside the functions.php file in the server and I created a shortcode for the api call.
I'm familiar with React and I was expecting Wordpress to be somewhat similar. For example being able to grab the results, loop, and display in a list/component
sendExternalApiCall().then((res) => {
return (
res.listOfResults.map((item) => {
return <div> item.title </div>
})
)
})
I've tried googling/watching Youtube videos but most examples of dynamic content are using Custom post types, custom field options, but all this is seemingly still very manual effort and I'm having a hard time piecing together how I can grab my list of results from my api e.g.
results = [{title: 'first post'}, {title: 'second post'}]
loop through those results and render then on the page inside a wordpress widget. Am I able to do this or do I have to create a custom widget? I would love to be able to use existing widgets like elementor but I don't know how to integrate them
Share Improve this question edited Jun 20, 2024 at 6:31 bart lu asked Jun 20, 2024 at 6:23 bart lubart lu 32 bronze badges2 Answers
Reset to default 0Guide to displaying data from an external API in WordPress using a shortcode:
1. Create a Shortcode in functions.php
Create a shortcode in your functions.php
file that outputs a container for the results and includes a JavaScript file:
function fetch_external_api_data() {
wp_enqueue_script('custom-api-fetch-script');
return '<div id="apiResults"></div>';
}
add_shortcode('external_api_data', 'fetch_external_api_data');
2. Enqueue and Write JavaScript
Create api-fetch.js
in your theme directory to fetch and display the data:
function enqueue_custom_scripts() {
wp_register_script('custom-api-fetch-script', get_template_directory_uri() . '/js/api-fetch.js', array('jquery'), null, true);
wp_localize_script('custom-api-fetch-script', 'apiSettings', array('apiUrl' => 'URL_OF_YOUR_API'));
wp_enqueue_script('custom-api-fetch-script');
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
In api-fetch.js
, use AJAX to get the data and render it:
jQuery(document).ready(function($) {
$.get(apiSettings.apiUrl, function(data) {
var content = data.listOfResults.map(item => '<div>' + item.title + '</div>').join('');
$('#apiResults').html(content);
});
});
3. Use the Shortcode
Place [external_api_data]
in any post, page, or widget to display the API results.
This method uses a JavaScript-based approach to render the API data dynamically on the client side, similar to React, and integrates easily with existing WordPress widgets like those in Elementor via shortcodes.
You can use for example wp_remote_get()
, which is part of the WordPress' HTTP API, to call the external API. After receiving the remote response you can pluck the body data from it with wp_remote_retrieve_body()
for further handling - e.g. decode json to an array.
If you have a high-traffic site, then for performance reasons, you may want to cache the remote response to a transient for suitable time period instead of calling the api on every page load.
If the remote data doesn't change frequently, then you may even want to push the remote call to a scheduled cron action to make the call in the background. In this case the flow could be something like this,
- Scheduled cron action calls the remote API
- Remote response is cached into a transient
- Shortcode/WP widget/dynamic block/Elementor widget is used to read the transient and render the data
本文标签: phpfetch from an external api call and display results in page
版权声明:本文标题:php - fetch from an external api call and display results in page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303170a1931791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论