MrHinsh MrHinsh - 2 months ago 10
PHP Question

Calculations in Wordpress

I am trying to figure out how to do calculations in WordPress. I don't want a "calculator" but I want to display the result of a calculations.

I already have a custom value called [days] associated with the custom post type. I need somewhere to store a global value of [rate] and a way to resolve calculations.

I want to be able to have something like [shortcode evaluate="([rate]/2)*[days]" so that I can have it display the final value. I can then change [rate] and it is reflected across my site.

I was hoping to not have to write any PHP, mainly because it is not my favourite language, but I want this to be reusable and work across updates of Wordpress and plugins.

I have Pagelines Framework installed, and I have WP-Types to give me custom fields on my custom types. I have been trawling the forums and documentation for Pagelines, WP-Types, and Wordpress but cant seam to figure this out. It sounds like it should already be there...

Do I need to create a custom plugin? Or is there something I can utilise?

UPDATE



I have experimented with creating a plugin, but I cant seam to get my PHP to work.

Full file: https://github.com/nkdAgility/wp-calculate-rate/blob/master/wp-calculate-rate.php

function nkdCalculate_shortcode($atts,$content=null) { <---Line 16
$content = wpv_do_shortcode($content);
$content = eval("return $content;");
    return $content;
}

add_shortcode('nkd-calculate', 'nkdCalculate_shortcode');

function nkdCalculate_rate_shortcode($atts) {
   return wget_option('my_rate');
}

add_shortcode('nkd-rate', 'nkdCalculate_rate_shortcode');

?>


Unfortunately my code always errors out with:


Parse error: syntax error, unexpected 'function' (T_FUNCTION) in /nas/content/staging/nakedalmweb/wp-content/plugins/wp-calculate-rate-1/wp-calculate-rate.php on line 16


Why is the function unexpected?

Answer

I got the following code to activate:

<?php
/**
* Plugin Name: nkdAgility Rate Calculator
* Description: Tell Us What Your Shortcode Does
* Version: 0.6
* Author: MrHinsh
* Author URI: https://nkdagility.com
*/

function nkdCalculate_shortcodes_init() {
    function nkdCalculate_shortcode($atts,$content=null) { 
    $content = wpv_do_shortcode($content);
    $content = eval("return $content;");
    return $content;
}

add_shortcode('nkd-calculate', 'nkdCalculate_shortcode');

function nkdCalculate_rate_shortcode($atts) {
   return wget_option('my_rate');
}

add_shortcode('nkd-rate', 'nkdCalculate_rate_shortcode');

}
add_action('init', 'nkdCalculate_shortcodes_init');
?>

Specifically enclosing your shortcode function within a function called 'nkdCalculate_shortcodes_init' and then telling wordpress when to initialise your shortcodes with add_action('init', 'nkdCalculate_shortcodes_init');

If you look at https://developer.wordpress.org/plugins/shortcodes/basic-shortcodes/ it explains more about how shortcodes should be initialised after wordpress.

Hope that helps

Comments