Skip to content
← Back to Functions
Code

RSS Feed Builder

Builds structured RSS feed data from site content without directly echoing XML.

Function signature

ogBuildRssFeedData(site_info = array(), items = array(), options = array())

Categories

  • Import and Export

Parameters

site_infoFeed channel data. Recognized keys: `description`, `link`, `title`.itemsContent rows for feed items.optionsFeed limits and defaults. Recognized keys: `limit`.

Return value

Short public-safe status message.

  • channel
  • items
  • item_count

Compatibility

Existing function name and call order preserved; metadata signature corrected to source.

Minimum PHP version: 7.4

Security notes

Validate request method, identity, permissions, policy arrays, URLs, signatures, and caller-owned allowlists before use; keep secrets and internal paths out of public output.

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Builds structured RSS feed data from site content without directly echoing XML.
 *
 * The function prepares channel and item arrays only. Rendering and XML escaping should happen
 * in the template/output layer so the data can be audited before output.
 *
 * @param array $site_info Feed channel data.
 * @param array $items Content rows for feed items.
 * @param array $options Feed limits and defaults.
 * @return array RSS feed data structure.
 */
function ogBuildRssFeedData($site_info = array(), $items = array(), $options = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($site_info)) {
		$result['message'] = 'Site information must be an array.';
		return $result;
	}
	if (!is_array($items)) {
		$result['message'] = 'Feed items must be an array.';
		return $result;
	}
	if (!is_array($options)) {
		$options = array();
	}

	$title = '';
	if (!empty($site_info['title'])) {
		$title = trim((string)$site_info['title']);
	}
	$link = '';
	if (!empty($site_info['link'])) {
		$link = trim((string)$site_info['link']);
	}
	$description = '';
	if (!empty($site_info['description'])) {
		$description = trim((string)$site_info['description']);
	}
	if (empty($title) || empty($link) || empty($description)) {
		$result['message'] = 'Feed title, link, and description are required.';
		return $result;
	}

	$limit = 50;
	if (!empty($options['limit'])) {
		$limit = (int)$options['limit'];
	}
	if ($limit < 1) {
		$limit = 1;
	}
	if ($limit > 500) {
		$limit = 500;
	}

	$feed_items = array();
	foreach ($items as $item) {
		if (!is_array($item)) {
			continue;
		}
		$item_title = '';
		if (!empty($item['title'])) {
			$item_title = trim((string)$item['title']);
		}
		$item_link = '';
		if (!empty($item['link'])) {
			$item_link = trim((string)$item['link']);
		}
		if (empty($item_title) || empty($item_link)) {
			continue;
		}
		$item_description = '';
		if (!empty($item['description'])) {
			$item_description = trim((string)$item['description']);
		}
		$guid = $item_link;
		if (!empty($item['guid'])) {
			$guid = trim((string)$item['guid']);
		}
		$pub_date = '';
		if (!empty($item['pub_date'])) {
			$pub_date = trim((string)$item['pub_date']);
		}
		$feed_items[] = array(
			'title' => $item_title,
			'link' => $item_link,
			'description' => $item_description,
			'guid' => $guid,
			'pub_date' => $pub_date
		);
		if (count($feed_items) >= $limit) {
			break;
		}
	}

	$result['success'] = true;
	$result['message'] = 'RSS feed data built.';
	$result['data'] = array(
		'channel' => array(
			'title' => $title,
			'link' => $link,
			'description' => $description,
			'language' => $language,
			'last_build_date' => gmdate('D, d M Y H:i:s') . ' GMT'
		),
		'items' => $feed_items,
		'item_count' => count($feed_items)
	);

	return $result;
}