/**
* This is an example of wrapping a shortcode around the LearnDash [courseinfo] shortcode to control the format.
*
* In the following example we are targeting the courseinto item 'cumulative_percentage'. By default the
* value returned via LearnDash will be a number with 2 decimal places. We want to round the number value
* to the closest whole integer.
*
* For this we wrap the LearnDash shortcode with our own [number_format][/number_format] as in
* [number_format show="cumulative_percentage" decimal_places="1"][courseinfo show="cumulative_percentage"][/number_format]
* We use the same show="" parameter this was we only have one shortcode function instead of one for each of the courseinfo items.
* Also, we have a second optional parameter decimal_places="" this is used to control the number of decimal places to trim to.
* If not provided the default is decimal_places="2".
*
*/
add_shortcode('number_format', function ( $atts = array(), $content = '') {
// We call do_shortcode because what is passed here in $content
// is the inner shortcode '[courseinfo show="cumulative_percentage"]'
// We need to get the returned numer value first.
$content = do_shortcode( $content );
if ( ( isset( $atts['show'] ) ) && ( !empty( $atts['show'] ) ) ) {
if ( !isset( $atts['decimal_places'] ) ) {
$atts['decimal_places'] = 2;
}
$atts['decimal_places'] = intval( $atts['decimal_places'] );
switch( $atts['show'] ) {
case 'cumulative_percentage':
//$content = number_format( $content, $atts['decimal_places'] );
// Instead of trimming the decimal places using number_format() we instead want to round the value up/down
$content = round( $content, $atts['decimal_places'] );
break;
default:
break;
}
}
return $content;
}, 10, 2);
Copy to clipboard