Fundraising Totals Shortcode (GiveWP)
Adds a [wpide_give_totals] shortcode that shows the total raised and number of donations across all your GiveWP forms (or selected ones), for headers, impact pages and annual reports.
Opens WPIDE on
Code
add_shortcode( 'wpide_give_totals', function ( $atts ) {
if ( ! function_exists( 'give_get_meta' ) ) {
return '';
}
$atts = shortcode_atts( array( 'show' => 'amount', 'forms' => '' ), $atts, 'wpide_give_totals' );
$ids = array_filter( array_map( 'absint', explode( ',', $atts['forms'] ) ) );
$key = 'wpide_give_totals_' . md5( implode( ',', $ids ) );
$total = get_transient( $key );
if ( false === $total ) {
if ( ! $ids ) {
$ids = get_posts( array( 'post_type' => 'give_forms', 'post_status' => 'publish', 'numberposts' => -1, 'fields' => 'ids', 'no_found_rows' => true ) );
}
$total = array( 'amount' => 0.0, 'count' => 0 );
foreach ( $ids as $form_id ) {
$total['amount'] += (float) give_get_meta( $form_id, '_give_form_earnings', true );
$total['count'] += (int) give_get_meta( $form_id, '_give_form_sales', true );
}
set_transient( $key, $total, HOUR_IN_SECONDS );
$keys = (array) get_option( 'wpide_give_total_keys', array() );
update_option( 'wpide_give_total_keys', array_unique( array_merge( $keys, array( $key ) ) ), false );
}
if ( 'count' === $atts['show'] ) {
return esc_html( number_format_i18n( $total['count'] ) );
}
$amount = function_exists( 'give_currency_filter' ) && function_exists( 'give_format_amount' )
? give_currency_filter( give_format_amount( $total['amount'], array( 'sanitize' => false ) ) )
: number_format_i18n( $total['amount'], 2 );
return '<span class="wpide-give-total">' . wp_kses_post( $amount ) . '</span>';
} );
add_action( 'give_complete_donation', function () {
foreach ( (array) get_option( 'wpide_give_total_keys', array() ) as $key ) {
delete_transient( $key );
}
} );
Notes
Attributes: show="amount|count", forms="12,34" to limit to certain forms. Totals are cached for an hour and refresh after every completed donation.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.