Skip to content
← Back to Functions
Code

Title Tag Composer

Creates page title tags from entity title, section, brand, and length policy.

Function signature

ogComposeTitleTag(parts = array(), brand = 'PHPOG', max_length = 60)

Categories

  • SEO and Routing

Parameters

partsOrdered title-tag pieces before brand and length policy are applied.brandBrand label appended to title tags when appropriate.max_lengthMaximum preferred length for composed text.

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 page title tags from entity title, section, brand, and length policy.
 *
 * Primary use case: Dynamic SEO titles.
 * Typical inputs: title parts, brand, max length.
 * Typical output: title tag string.
 *
 * Implementation note: Preserve important words near the front.
 *
 * @param array $parts Title parts in priority order.
 * @param string $brand Site or brand suffix.
 * @param int $max_length Maximum title length.
 * @return array Title tag result.
 */
function ogComposeTitleTag($parts = array(), $brand = 'PHPOG', $max_length = 60) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($parts)) {
		$parts = array((string)$parts);
	}
	$brand = trim((string)$brand);
	$max_length = (int)$max_length;
	if ($max_length < 30) {
		$max_length = 30;
	}

	$clean_parts = array();
	foreach ($parts as $part) {
		$part = trim(strip_tags((string)$part));
		if (!empty($part)) {
			$clean_parts[] = $part;
		}
	}
	if (!empty($brand)) {
		$clean_parts[] = $brand;
	}
	if (empty($clean_parts)) {
		$result['message'] = 'At least one title part is required.';
		return $result;
	}

	$title = implode(' | ', $clean_parts);
	if (strlen($title) > $max_length) {
		$title = substr($title, 0, $max_length - 3);
		$title = rtrim($title, ' |-') . '...';
	}

	$result['success'] = true;
	$result['message'] = 'Title tag composed.';
	$result['data'] = array('title' => $title);

	return $result;
}