[obituary_list]
function scrape_obituaries() { $url = 'https://www.gilbertsonfuneralhome.com/'; $html = file_get_contents($url); // Check if content was retrieved if (!$html) return; $dom = new DOMDocument; @$dom->loadHTML($html); $xpath = new DOMXPath($dom); $obituaries = []; // Adjust the XPath based on the target website's structure foreach ($xpath->query('//div[@class="obituary-entry"]') as $node) { $title = $xpath->query('.//h2', $node)->item(0)->textContent; $content = $xpath->query('.//p', $node)->item(0)->textContent; $obituaries[] = [ 'title' => trim($title), 'content' => trim($content), ]; } foreach ($obituaries as $obituary) { // Insert into WordPress as a post wp_insert_post([ 'post_title' => $obituary['title'], 'post_content' => $obituary['content'], 'post_status' => 'publish', 'post_type' => 'post', 'post_category' => ['obituaries'], // Make sure 'obituaries' category exists ]); } } // Schedule the event on plugin activation register_activation_hook(__FILE__, 'obituary_scraper_activation'); function obituary_scraper_activation() { if (!wp_next_scheduled('scrape_obituaries_event')) { wp_schedule_event(time(), 'hourly', 'scrape_obituaries_event'); } } // Clear the event on deactivation register_deactivation_hook(__FILE__, 'obituary_scraper_deactivation'); function obituary_scraper_deactivation() { wp_clear_scheduled_hook('scrape_obituaries_event'); } // Hook the scrape function to the scheduled event add_action('scrape_obituaries_event', 'scrape_obituaries'); function obituary_shortcode() { $obituaries = get_posts([ 'post_type' => 'post', 'category_name' => 'obituaries', 'posts_per_page' => 10, ]); $output = '
[obituary_list]