To automatically import content into WordPress from a URL with information in JSON format, you can use the WordPress HTTP API to retrieve the JSON data and the wp_insert_post
function to insert the data as WordPress posts. Here’s an example:
phpCopy code// Get JSON data from URL
$response = wp_remote_get('https://example.com/data.json');
$data = json_decode(wp_remote_retrieve_body($response), true);
// Loop through the data and insert as posts
foreach ($data as $item) {
$post_data = array(
'post_title' => $item['title'],
'post_content' => $item['content'],
'post_status' => 'publish',
'post_type' => 'post',
);
wp_insert_post($post_data);
}
Replace 'https://example.com/data.json'
with the actual URL of the JSON data. The wp_remote_get
function is used to retrieve the JSON data, and the json_decode
function is used to convert the data from a JSON string to a PHP array.
The foreach
loop is used to loop through each item in the data array and insert it as a WordPress post using the wp_insert_post
function. The post data, such as the post title and content, is specified in an array that is passed as an argument to the wp_insert_post
function.
You can schedule this function to run automatically using the wp_schedule_event
function in WordPress, or you can use a plugin such as WP-Cron or WP Schedule Posts.
Last modified: March 3, 2023