All Collections
Create by Mediavine
How To Use Filters To Standardize Fractions In Create Cards
How To Use Filters To Standardize Fractions In Create Cards

Programmatically replacing text in cards with a filter

Sam Ellis avatar
Written by Sam Ellis
Updated over a week ago

In Create version 1.3, we added mv_create_card_render , a filter that allows you to directly filter the HTML output of your Create cards. The purpose of this filter is to make it possible to programmatically change the text content of your cards.

A use case would be for fractions – maybe at some point in your blog's history, you switched from textual fractions (1/4) to their Unicode equivalents (¼) and you'd like to standardize things. One of our core philosophies with Create is to avoid prescribing formatting with the plugin itself whenever possible, but with this filter, you can enforce a style for your own site.

The following code can be added to your theme's functions.php file:

add_filter('mv_create_card_render', function($html) {
$html = preg_replace("/(?<!\d|\/)1\/2(?!\d|\/)/", '½', $html);
$html = preg_replace("/(?<!\d|\/)1\/3(?!\d|\/)/", '⅓', $html);
$html = preg_replace("/(?<!\d|\/)2\/3(?!\d|\/)/", '⅔', $html);
$html = preg_replace("/(?<!\d|\/)1\/4(?!\d|\/)/", '¼', $html);
$html = preg_replace("/(?<!\d|\/)3\/4(?!\d|\/)/", '¾', $html);
$html = preg_replace("/(?<!\d|\/)1\/5(?!\d|\/)/", '⅕', $html);
$html = preg_replace("/(?<!\d|\/)2\/5(?!\d|\/)/", '⅖', $html);
$html = preg_replace("/(?<!\d|\/)3\/5(?!\d|\/)/", '⅗', $html);
$html = preg_replace("/(?<!\d|\/)4\/5(?!\d|\/)/", '⅘', $html);
$html = preg_replace("/(?<!\d|\/)1\/6(?!\d|\/)/", '⅙', $html);
$html = preg_replace("/(?<!\d|\/)5\/6(?!\d|\/)/", '⅚', $html);
$html = preg_replace("/(?<!\d|\/)1\/7(?!\d|\/)/", '⅐', $html);
$html = preg_replace("/(?<!\d|\/)1\/8(?!\d|\/)/", '⅛', $html);
$html = preg_replace("/(?<!\d|\/)3\/8(?!\d|\/)/", '⅜', $html);
$html = preg_replace("/(?<!\d|\/)5\/8(?!\d|\/)/", '⅝', $html);
$html = preg_replace("/(?<!\d|\/)7\/8(?!\d|\/)/", '⅞', $html);
$html = preg_replace("/(?<!\d|\/)1\/9(?!\d|\/)/", '⅑', $html);
$html = preg_replace("/(?<!\d|\/)1\/10(?!\d|\/)/", '⅒', $html);

return $html;

The above regex matches fractions that aren't preceded or followed by a number or slash, so that it won't match dates like "2/3/19" or fractions that don't have a Unicode equivalent, like 1/100. If you're not worried about those cases, you could just as easily use str_replace with just the fraction. It's also exhaustive – you've probably never used 1/7 of something, so you could just as easily leave the line out.

Note that this filter is designed to enable filtering text, not HTML. It's notoriously unreliable to modify HTML tags with regex. If you're interesting in modifying the markup of your cards, you can always use custom templates. If you have questions, please reach out to for help.

Did this answer your question?