(Potential) ABV Calculator Shortcode


Put this in your functions.php to do the before and after math for specific gravity readings predicting ABV (and potential ABV).

Usage:

[abv_calc before=”1.06″ after=”1.033″] becomes: HYDROMETER Before 1.06 After 1.033 ABV 3.54%

If the after is omitted, it will do a potential calculation:

[abv_calc before=”1.05″] becomes: HYDROMETER Before 1.05 Potential ABV 6.56%

// [abv_calc before="1.05" after="1.043"]
function abv_calc_func( $atts ) {
        $a = shortcode_atts( array(
                'before' => '',
                'after' => '',
        ), $atts );

        if( !empty($a['before']) ) {
                if( !empty($a['after']) ) {
                        $a['abv'] = round( abs( (float)$a['after'] - (float)$a['before'] ) * 131.25, 2);
                } else {
                        $a['potential'] = round( abs( 1 - (float)$a['before'] ) * 131.25, 2);
                }
        }

        $s = '<span style="background:cornsilk;padding:5px 10px;border-radius:4px"><span class=badge style="font-size:.6em">HYDROMETER</span>';

        if( !empty($a['before']) ) $s .= ' Before <strong>'.$a['before'].'</strong>';
        if( !empty($a['after']) ) $s .= ' After <strong>'.$a['after'].'</strong>';
        if( !empty($a['abv']) ) $s .= ' ABV <strong>'.$a['abv'].'%</strong>';
        if( !empty($a['potential']) ) $s .= ' Potential ABV <strong>'.$a['potential'].'%</strong>';

        $s .= '</span>';

        return $s;
}
add_shortcode( 'abv_calc', 'abv_calc_func' );