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

Slack Signup
Newsletter Optin
Help Desk

Math Shortcode

Labels

This Discussion is public

Notifications

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 );
});

Hi Andrew is this available so it woks within a sidebar?

Regards Chris

Forget that Andrew, i realised its possible by adding a new view and not using the shortcode directly in a text widget!!

 

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' );

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!

please today is my first time on this forum. Where do you put the above code?

many thanks

 

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?

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

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.