Skip to content
← Back to Functions
Code

Meta Description Composer

Creates unique meta descriptions from title, summary, type, and keyword context.

Function signature

ogComposeMetaDescription(title, summary = '', options = array())

Categories

  • SEO and Routing

Parameters

titleHuman-facing title used for metadata, schema, or alt text.summaryPlain-text summary used as source material for meta description composition.optionsOptional documented policy controls for the helper.

Return value

Public-safe status string returned by the function for controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate file paths, routes, email tokens, cart totals, discount rules, and tax-region rules before production use.

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.
 */

/**
 * Creates unique meta descriptions from title, summary, type, and keyword context.
 *
 * Primary use case: SEO for dynamic content pages.
 * Typical inputs: title, description, item type, max length.
 * Typical output: description text for page display and meta output.
 *
 * Implementation note: Avoid keyword stuffing; keep readable.
 *
 * @return array Structured result data with success, message, and data keys.
 */
function ogComposeMetaDescription($title, $summary = '', $options = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	$title = trim(strip_tags((string)$title));
	$summary = trim(strip_tags((string)$summary));
	if (!is_array($options)) {
		$options = array();
	}

	$max_length = 155;
	if (!empty($options['max_length'])) {
		$max_length = (int)$options['max_length'];
	}
	if ($max_length < 80) {
		$max_length = 80;
	}
	if ($max_length > 240) {
		$max_length = 240;
	}

	$item_type = '';
	if (!empty($options['item_type'])) {
		$item_type = trim(strip_tags((string)$options['item_type']));
	}

	if (empty($title) && empty($summary)) {
		$result['message'] = 'A title or summary is required.';
		return $result;
	}

	$description = '';
	if (!empty($summary)) {
		$description = $summary;
	} else {
		$description = $title;
	}

	if (!empty($item_type) && stripos($description, $item_type) === false) {
		$description .= ' ' . $item_type . ' for developers.';
	}

	$description = preg_replace('/\s+/', ' ', $description);
	$description = trim($description);
	if (strlen($description) > $max_length) {
		$description = substr($description, 0, $max_length - 3);
		$last_space = strrpos($description, ' ');
		if ($last_space !== false && $last_space > 40) {
			$description = substr($description, 0, $last_space);
		}
		$description = rtrim($description, ' .,;:-') . '...';
	}

	$result['success'] = true;
	$result['message'] = 'Meta description composed.';
	$result['data'] = array(
		'description' => $description,
		'length' => strlen($description)
	);

	return $result;
}