The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group
Math Shortcode
Here is a shortcode that allows you to build equations using any shortcodes or numbers plus any of the four operators: + - * /
Format the number with optional attributes: decimal=2 dec_point='.' thousands_sep=',' (those are the defaults)
Example: [math decimal=0 thousands_sep='']0 + 0 - [384] / 100[/math]
Will only work if you separate everything with a single space.
add_shortcode( 'math', function( $a, $content = '' ) {// change 'math' to whatever you want the shortcode to be named if ( ! $content ) return "(No equation given)"; // Run Shortcodes to get values $content = do_shortcode( $content ); // Seperate at spaces (after removing commas space at front and end) $parts = explode( ' ', trim( str_replace( ',', '', $content ) ) ); // Check for odd number of parts (greater than 1) $count = count( $parts ); if ( $count === 1 || $count % 2 !== 1 ) return "(Malformed equation)"; // Set subtotal as the first number $total = (float) $parts[0]; // Process additional numbers using the correct operator for ( $i = 1; $i < $count; ) { switch ( $parts[$i++] ) { case '+': $total += $parts[$i++]; break; case '-': $total -= $parts[$i++]; break; case '/': if ( $parts[$i] == 0 ) return '0';// if dividing by zero, abort and return 0 to avoid php warnings $total /= $parts[$i++]; break; case '*': case 'x': case 'X': $total *= $parts[$i++]; break; } } // Handle format, defaults: decimal=2 dec_point='.' thousands_sep=',' $decimal = 2; $dec_point = '.'; $thousands_sep = ','; if ( isset( $a['decimal'] ) ) { $decimal = (int) $a['decimal']; }// Seems like formidable allows decimal or round elseif ( isset( $a['round'] ) ) { $decimal = (int) $a['round']; } if ( isset( $a['dec_point'] ) ) { $dec_point = $a['dec_point']; } if ( isset( $a['thousands_sep'] ) ) { $thousands_sep = $a['thousands_sep']; } return number_format( $total, $decimal, $dec_point, $thousands_sep ); });
June 1, 2016 at 3:58 am
Hi Andrew is this available so it woks within a sidebar?
Regards Chris
June 1, 2016 at 4:53 am
Forget that Andrew, i realised its possible by adding a new view and not using the shortcode directly in a text widget!!
June 20, 2016 at 8:36 am
Glad you got it working. By default shortcodes don't work in text widgets, but you can simply add this to your functions.php:
add_filter( 'widget_text', 'do_shortcode' );
December 6, 2016 at 10:45 pm
That is absolutely brilliant!! And it works with the stats short-code as well, been looking to figure this out for a while now. Thank you so much!
January 9, 2017 at 3:33 am
please today is my first time on this forum. Where do you put the above code?
many thanks
February 3, 2018 at 5:14 pm
Hello Andrew,
I use your great shortcode on many sites. Just updated to Formidable 3.0 and the shortcodes no longer work. Are you aware of this and might it be fixed?
July 11, 2018 at 5:41 pm
Hi, Andrew --
I love your clever, creative idea of having a math shortcode! The code you shared here was the inspiration for adding a math shortcode to Formidable Pro.
Thank you so much!
Having a math shortcode significantly expands what people can do with Formidable -- and it's all thanks to you.
https://formidableforms.com/knowledgebase/math-calculations/
With great appreciation,
Laura
November 30, 2018 at 8:16 pm
Hi Laura,
Thanks for your message! I don't seem to get notifications on this thread, so I never knew, but a client mentioned it to me yesterday.
Glad it can be of some use to people, I thought it was pretty fun to code :)
Discussion closed.