Let’s start with the first practical example of ChatGPT’s ability to generate code. The scenario is an embedded news page on a blog about AI topics that retrieves its content through a web API.
As a developer, you may be familiar with the problem of needing a specific code but having to search for it on the internet. In such cases, ChatGPT can be a great resource to help you generate sample code that can be adapted to your specific requirements.
It is important to note that general programming knowledge is required to use ChatGPT for code generation. You need to know how to integrate and customize the generated code in your project to achieve the desired result. You may also need to troubleshoot if errors occur. Nevertheless, ChatGPT can be a useful tool for developers who lack the necessary knowledge to write the code themselves.
Now let’s get to the practical part: Creating a news page with content from the AI field.
One way to do this is to use GDELT to generate dynamic news content on a website. With ChatGPT, you can quickly and easily generate code for API calls and even learn how to integrate them into WordPress in this example.
GDELT stands for “Global Database of Events, Language, and Tone” and is a platform that monitors and analyzes thousands of news sources worldwide. The platform offers various APIs that developers can use to access and use this content in their applications.
One of these APIs is GDELT Summary, which provides a curated summary of the most important news events in real-time. The summary is updated every 15 minutes and covers a wide range of topics, including politics, economics, science, technology, and more.
There, you can configure and test your API call through an interface:

On the result page, you can view the corresponding JSON output. The address bar shows the string for the API call, and you may need to adjust the URL parameters (such as maxrecords for the number of articles) to suit your needs.
And now, enough prelude – here’s the excerpt from the chat:
How can I generate dynamic content in my WordPress blog? I want to call a web API that returns content in JSON format. To generate dynamic content in your WordPress blog, you can create a function in your functions.php file that calls the web API and retrieves the data in JSON format. Then, you can display the returned data in your WordPress blog.
Here's an example code that can show you how to do it:
<?php
$api_data = get_api_data();
if( $api_data ) {
foreach( $api_data as $item ) {
echo '<h2>' . $item->title . '</h2>';
echo '<p>' . $item->content . '</p>';
}
}
?>
The function get_api_data calls the web API and returns the data in JSON format. You can use this function to display the data in your WordPress blog.
For example, you can use the following code in your template file to display the content from the web API:
<?php
$api_data = get_api_data();
if( $api_data ) {
foreach( $api_data as $item ) {
echo '<h2>' . $item->title . '</h2>';
echo '<p>' . $item->content . '</p>';
}
}
?>
In this example, we assume that the web API returns a list of articles, with each article having a title and content. The loop iterates through each item in the list and displays the title and content of each article.
Note: When retrieving data from external sources, it is important to ensure that the data is reliable and does not contain any harmful content. It is also important to ensure that you retrieve the APIs in a secure manner, such as by using SSL and authentication, if necessary.
This is what the call looks like:
https://api.gdeltproject.org/api/v2/doc/doc?format=html×pan=24H&query=Artificial%20Intelligence%20sourcelang:eng&mode=artlist&maxrecords=1&format=json&sort=hybridrel
and the following JSON is returned:
{"articles": [ { "url": "https://investorplace.com/2023/02/the-future-of-ai-stocks-7-companies-to-keep-your-eye-on/", "url_mobile": "", "title": "The Future of AI Stocks : 7 Companies to Keep Your Eye On", "seendate": "20230228T161500Z", "socialimage": "https://investorplace.com/wp-content/uploads/2023/02/artificial-intelligence-ai-green-1600.jpg", "domain": "investorplace.com", "language": "English", "sourcecountry": "United States" }] }
Assuming you have the example JSON data provided, you can display the data in your WordPress blog as follows:
<?php
$url = 'https://api.gdeltproject.org/api/v2/doc/doc?format=html×pan=24H&query=Artificial%20Intelligence%20sourcelang:eng&mode=artlist&maxrecords=1&format=json&sort=hybridrel';
$response = wp_remote_get( $url );
if( is_wp_error( $response ) ) {
echo 'Fehler beim Abrufen der Daten';
} else {
$data = json_decode( wp_remote_retrieve_body( $response ) );
if( $data && !empty( $data->articles ) ) {
foreach( $data->articles as $article ) {
echo '<h2>' . $article->title . '</h2>';
echo '<img src="' . $article->socialimage . '">';
echo '<p>' . $article->seendate . '</p>';
echo '<p>' . $article->url . '</p>';
}
} else {
echo 'Keine Artikel gefunden';
}
}
?>
In this example, the wp_remote_get function is used to retrieve data from the specified URL. Then, an error check is performed and the JSON data is decoded and looped through to display the data.
In this case, the article title, social image, publish date, and article URL are displayed. However, you can customize the display to show the desired data.
How does the API call work through functions.php?
To implement the API call in your WordPress site through the functions.php file, you can use the following code:
function display_api_data() {
$url = 'https://api.gdeltproject.org/api/v2/doc/doc?format=html×pan=24H&query=Artificial%20Intelligence%20sourcelang:eng&mode=artlist&maxrecords=1&format=json&sort=hybridrel';
$response = wp_remote_get( $url );
if ( is_array( $response ) ) {
$headers = $response['headers'];
$body = json_decode( $response['body'], true );
if ( ! empty( $body ) && isset( $body['articles'] ) ) {
$articles = $body['articles'];
foreach ( $articles as $article ) {
$title = $article['title'];
$url = $article['url'];
$image = $article['socialimage'];
echo '<div>';
echo '<h2>' . $title . '</h2>';
echo '<img src="' . $image . '" />';
echo '<p><a href="' . $url . '">Read more</a></p>';
echo '</div>';
}
}
}
}
This code calls the API URL and outputs the JSON data. You can customize the code to generate the content of your page by modifying the foreach loop according to your requirements and adjusting the content of the echo statements.
To execute the code, you can embed the function display_api_data() in a WordPress page or post using the shortcode [api_data]. To do this, you need to add the following code to your functions.php file:
function api_data_shortcode() {
ob_start();
display_api_data();
return ob_get_clean();
}
add_shortcode( 'api_data', 'api_data_shortcode' );
This code registers a shortcode [api_data] which executes the display_api_data() function and outputs the generated content on the page. You can use the shortcode on any WordPress page or post by entering the following code into the editor:
[api_data]
Where can I find the functions.php file?
The functions.php file is located in the active WordPress theme's folder and contains functions and filters that can be used on the website. The path to the functions.php file is usually:
/wp-content/themes/your-theme/functions.php
Replace your-theme with the name of your active WordPress theme. If you are using the default theme "Twenty Twenty-One", the path to the functions.php file is:
/wp-content/themes/twentytwentyone/functions.php
You can access the functions.php file through the dashboard of your WordPress website. To do this, go to the "Appearance" menu and click on "Theme Editor". Then, select the active theme from the dropdown list at the top right and select the functions.php file from the list of theme files on the right-hand side. You can edit and save the code directly in the editor. However, note that an error in the code can damage or render your website unusable when you edit the functions.php file. It is recommended to create a backup of the file before making changes.
In this article, we have shown how to generate a function in the functions.php file of WordPress using ChatGPT that calls the GDELT API and retrieves the latest news. You can see the result on the news page.
It is important to note that when using supported programming with ChatGPT, you can and often need to optimize the program code in the ChatGPT dialog. There will be a separate post on tips and tricks for using ChatGPT for code generation.
Overall, the use of ChatGPT can increase the efficiency of your work as a developer by helping you generate example code quickly and easily. Whether it is about integrating APIs or other use cases, ChatGPT can be a useful tool to save time and speed up your development work.
Leave a Reply