The simplest possible Wordpress footnotes

Adding footnotes without any plugins

The other day I was building a Wordpress website that needed footnotes on posts. Wordpress doesn't support that natively (there's an issue about it that's been open since 2017). My first thought was to use a plugin, but I couldn't find one that was maintained, had the right features, and didn't include all kinds of extra markup.

Here's what I want my footnotes to do:

function mytheme_extract_footnotes($content)
{
	$footnotes = array();
	$context = array();
	$pattern = "/(?:\(\()(.*)(?:\)\))/";

	preg_match_all($pattern, $content, $footnotes);

	$content = preg_replace_callback($pattern, function ($matches) {
		static $fn_index = 0;
		$fn_index++;
		return '<a class="footnote__ref" href="#note-' . $fn_index . '">' . $fn_index . '</a>';
	}, $content);

	$context["footnotes"] = $footnotes[1];

	return $content . $output;
}

add_filter('the_content', 'mytheme_extract_footnotes');

The site I was working on used Timber, so I actually wrote the following:

$output = Timber::compile('/partial/footnotes.twig', $context);