• Hi,

    I’m using a donations widget on a crowdfunding website, that shows the number of donations, goal amount, raised so far and percentage raised. For ‘raised so far’ it uses this in the PHP code:
    <span class="value"><?php echo '&euro;' . number_format( get_option( 'donations_total_raised' ), 2, ',', '.' ); ?></span>. Wherever I put this in a template, it nicely gives back the total amount raised.

    The crowdfunding project has several stages, each with a unit price of €1750. Now I would like to use this PHP code in a calculation in HTML:

    I want to devide the ‘amount raised so far’ by 1750 and always round it down to a whole number (1.9999 is still 1…), so it will show the number of stages that have been fully completed so far.

    How can I do this? I’ve searched on the internet, but did not find a clear answer. Any help is welcome.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Dion

    (@diondesigns)

    This would work:

    <?php echo floor(get_option('donations_total_raised') / 1750); ?>

    Thread Starter prodefu

    (@prodefu)

    @diondesigns: thanks for your quick reply. But no luck so far: this gives a blank/invisible output or no output at all.

    The ‘manual’ method does work, at least it shows a number:

    <div class="donate-section">
    <span class="value"><strong>0</strong></span> 
    <span class="label">kernwoorden gerealiseerd</span>
    </div>

    But of course I prefer to link this to the widget, if possible.

    • This reply was modified 7 years, 2 months ago by prodefu.
    Moderator bcworkz

    (@bcworkz)

    The option is maybe stored as a string, so it needs to be type cast as an integer to perform math.
    <?php echo floor( (int)get_option('donations_total_raised') / 1750); ?>

    If that still does not help, define WP_DEBUG as true in wp-config.php. DionD is on the right track, we just need it working for your implementation.

    If all you want is this value, I don’t see the point in working with a widget.

    Now I would like to use this PHP code in a calculation in HTML

    Where are you trying to do this? PHP code will not work in the post/page editor, or in the Text and Custom HTML widgets.

    You shouldn’t be getting a number at all if you were doing that though, but you mentioned it worked fine in templates and specifically reference a “custom HTML widget” in the topic title.

    Thread Starter prodefu

    (@prodefu)

    @bcworkz: thanks for your help. Still no luck.

    The reason for the widget is that I want to display this ‘number of stages’ right above the donations widget, in the same style. The original widget is ‘missing’ this option, but I expect it’s much harder to add this as a new field to the donations plugin. So I’m trying to work out an easy work-around.

    @jakept: I’m trying to do this in a HTML widget in a custom sidebar, using the custom sidebar plugin. Works fine on pages and I managed to add it to a section template of the front page. As I said, ‘manual’ method for this HTML widget works fine.

    • This reply was modified 7 years, 2 months ago by prodefu.
    Moderator bcworkz

    (@bcworkz)

    It’s possible the widget may have a filter or some way of modifying content, you would need to contact the author of that widget for assistance, that is beyond the type of support we can offer here.

    If the widget code is open source, you could copy the source code, modify it, then use your custom version instead of theirs.

    If this widget is at the top of your sidebar, you can add output to your sidebar template and it will appear above the widget output. When one alters theme templates, it’s best to create a child theme so your modifications are not overwritten during a theme update. Copy the sidebar template file to the child’s folder. On the child version, find the line that calls dynamic_sidebar(). Immediately above this, add DionD’s code or my modification of it. Be mindful of <?php ?> tags. They cannot be nested. If dynamic_sidebar() is not immediately preceded with a <?php tag, leave the tags off the code you insert.

    I’m not sure how your sidebar plugin is supposed to work. It’s not needed for the template modification suggested, but a conflict of some sort could occur, I just don’t know.

    Thread Starter prodefu

    (@prodefu)

    Thanks for your help guys, I now know what to look for. I expected that I could use some PHP code in a HTML widget, but apparently this code is ignored.

    @bcworkz: good suggestion to use it in the template. I’ve done this on the front page, the easiest place. At least this gives output, but the outcome is always zero. Maybe because it’s not an integer division? I found a hint on this page and on this page, maybe we can solve it with this?

    I have also asked the plugin builder (Pronamic); hope they will come up with a solution, otherwise it’s bad luck for me and I’ll just have to update this number manually.

    Moderator bcworkz

    (@bcworkz)

    What do you get with <?php print_r( get_option('donations_total_raised')); ?>? Let’s see what we’re working with first.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Calculation in custom HTML widget’ is closed to new replies.