Home / Widgets / NSB News and Events Aggregator
Duplicate Snippet

Embed Snippet on Your Site

NSB News and Events Aggregator

bo pennington
<10
Code Preview
php
<?php
// Include Simple HTML DOM parsing library if needed (you can also manually include it if necessary)
if (!function_exists('file_get_html')) {
    function file_get_html($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return str_get_html($output);
    }
}
// Shortcode to display NSB news and events feed
function nsb_news_feed_display() {
    // Fetch and display the news and events
    $news = nsb_fetch_news();
    $events = nsb_fetch_events();
    // Combine the output
    $output = '<h2>See What’s Happening</h2>';
    $output .= $news;
    
    $output .= '<h2>Upcoming Events</h2>';
    $output .= $events;
    return $output;
}
// Fetch news from the City of NSB website
function nsb_fetch_news() {
    $url = 'https://www.cityofnsb.com/';
    $html = file_get_html($url);
    if (!$html) {
        return '<p>Error fetching news from the City of NSB website.</p>';
    }
    // Extract the sections for "See What’s Happening" and "NEW SMYRNA BEACH SPOTLIGHTS"
    $newsSection = $html->find('div#whats-happening', 0);
    $spotlightSection = $html->find('div#spotlight', 0);
    if (!$newsSection || !$spotlightSection) {
        return '<p>No news available at the moment.</p>';
    }
    // Return the content from both sections
    $news = $newsSection->innertext;
    $spotlight = $spotlightSection->innertext;
    return $news . '<br>' . $spotlight;
}
// Fetch events from the Granicus site for specific agendas
function nsb_fetch_events() {
    $url = 'https://cityofnsb.granicus.com/ViewPublisher.php?view_id=1';
    $html = file_get_html($url);
    if (!$html) {
        return '<p>Error fetching events from the Granicus website.</p>';
    }
    // Filter event agendas by specific topics
    $eventsToLookFor = [
        "Planning & Zoning Board",
        "City Commission Regular Meeting",
        "Historic Preservation Commission",
        "Housing Authority",
        "Code Compliance Board",
        "Community Development Block Grant Advisory Board",
        "Economic Development Task Force",
        "Special Magistrate"
    ];
    $events = '';
    foreach ($html->find('table#upcoming-events tr') as $row) {
        $title = $row->find('td.title', 0)->plaintext;
        $date = $row->find('td.date', 0)->plaintext;
        // Check if the event matches one of the topics we're looking for
        foreach ($eventsToLookFor as $eventTopic) {
            if (stripos($title, $eventTopic) !== false) {
                $events .= '<p>' . $date . ' - ' . $title . '</p>';
                break;
            }
        }
    }
    if (empty($events)) {
        return '<p>No upcoming events found for the specified topics.</p>';
    }
    return $events;
}
// Register the shortcode [nsb_news_feed] to display the news feed on a page or post
add_shortcode('nsb_news_feed', 'nsb_news_feed_display');

Comments

Add a Comment