I'm working on a site running woocommerce and wp where the user will add/has added a dimension attribute.
The dimension string will take the following format originally:
Ht: 30 1⁄2" W: 22 1⁄2" D: 18"
and should return the string with html to format the fraction e.g:
Ht: 30 <sup>1</sup>⁄<sub>2</sub>" W: 22 <sup>1</sup>⁄<sub>2</sub>" D: 18"
will the following to consider:
- The amount of fractions in the string will be unknown (0 - X)
- The string can be any valid fraction
- The user may/may not add a space in between the fraction and the quotation e.g Ht: 30 1/2 " W...
Here's my solution:
add_filter( 'woocommerce_attribute', __NAMESPACE__ . '\\format_dimension_attr', 10, 2 );
/**
* Format fractions in the Dimension attr string to include proper html
*
* e.g The 1/2 in 'Height: 5 1/2"' should be
*
* <sup>1</sup>⁄<sub>2</sub>
*/
function format_dimension_attr( $value, $att ) {
if ( 'Dimensions' === $att['name'] ) {
// Use the raw value instead of $value (which includes html)
$raw = $att->get_options()[0];
if ( strpos( $raw, '/' ) !== false ) {
$raw = explode( ' ', $raw );
// Check for accidental space before quotation and remove it
// e.g 'height: 5 1/2 "' will produce [5, 1/2, "]
$raw = array_filter( $raw, function( $val ) {
return '"' !== $val;
} );
// Run through the array
$raw = array_map( function( $val ) {
// Format the current item if it contains a slash
if ( strpos( $val, '/' ) !== false ) {
// Remove any " before splitting
$val = explode( '/', trim( $val, '"' ) );
$val = '<sup>' . $val[0] . '</sup>⁄<sub>' . $val[1] . '</sub>"';
}
return $val;
}, $raw );
// Replace the p tag we removed by using the raw value
$value = wpautop( join( ' ', $raw ) );
}
}
return $value;
}