admin管理员组文章数量:1122832
I am currently trying to create a Wordpress plugin which create posts in the Wordpress database based on data from an external JSON API. As an example this NewsAPI feed could be used:
;apiKey=81143272da6c48d58bc38fe80dd110d6
The plugin I have written decodes the JSON data by using json_decode
and loops through the article
object in the JSON feed. Finally, the posts is being inserted programmatically using wp_insert_post
:
<?php
/**
* Plugin Name: Automatic News Feed Importer
* Version: 1.0.0
*/
function news_importer() {
$url = ";apiKey=81143272da6c48d58bc38fe80dd110d6";
$response = wp_remote_get( $url );
$data = json_decode( wp_remote_retrieve_body( $response ) );
foreach( $data->articles as $news ) {
$post_title = $news->title;
$post_content = $news->content;
$post_date = $news->publishedAt;
$post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_date' => $post_date,
'post_status' => 'publish',
'post_type' => 'post'
);
wp_insert_post( $post );
}
}
My problem is that when the plugin is activated, no posts are creating and added to the database. When a new post is uploaded and appearing in the feed it has to be uploaded automatically (asynchronously).
Any ideas on why this isn't working?
I am currently trying to create a Wordpress plugin which create posts in the Wordpress database based on data from an external JSON API. As an example this NewsAPI feed could be used:
https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=81143272da6c48d58bc38fe80dd110d6
The plugin I have written decodes the JSON data by using json_decode
and loops through the article
object in the JSON feed. Finally, the posts is being inserted programmatically using wp_insert_post
:
<?php
/**
* Plugin Name: Automatic News Feed Importer
* Version: 1.0.0
*/
function news_importer() {
$url = "https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=81143272da6c48d58bc38fe80dd110d6";
$response = wp_remote_get( $url );
$data = json_decode( wp_remote_retrieve_body( $response ) );
foreach( $data->articles as $news ) {
$post_title = $news->title;
$post_content = $news->content;
$post_date = $news->publishedAt;
$post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_date' => $post_date,
'post_status' => 'publish',
'post_type' => 'post'
);
wp_insert_post( $post );
}
}
My problem is that when the plugin is activated, no posts are creating and added to the database. When a new post is uploaded and appearing in the feed it has to be uploaded automatically (asynchronously).
Any ideas on why this isn't working?
Share Improve this question asked Feb 3, 2023 at 16:28 JoachimJoachim 213 bronze badges 3 |2 Answers
Reset to default 0By checking if the 'products' object exists in an if
statement and looping through the objects (as in the code above), the function seems to be working:
function json_to_post() {
$response = wp_remote_get('https://dummyjson.com/products');
$json_data = wp_remote_retrieve_body($response);
$data = json_decode($json_data, true);
if (isset($data['products'])) {
$products = $data['products'];
foreach ($products as $post) {
$title = $post['title'];
$content = $post['description'];
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'post'
);
wp_insert_post($new_post);
}
}
}
add_action('init', 'json_to_post');
There's a ready built plugin that does exactly this:
https://wpgetapi.com/downloads/custom-post-import/
本文标签: plugin developmentProgrammatically creating posts based on external JSON feed (asynchronously)
版权声明:本文标题:plugin development - Programmatically creating posts based on external JSON feed (asynchronously) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736280718a1926088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
news_importer()
? You've defined it in your plugin, where are calling it from? – darrinb Commented Feb 3, 2023 at 21:12articles
array - have you confirmed$data->articles
is set, and thatnews_importer()
andwp_insert_post()
were both called? Also, you could try setting the 2nd parameter to true (e.g.$post_id = wp_insert_post( $post, true );
), and check if an error is returned - see example here. – Sally CJ Commented Feb 4, 2023 at 3:09