Today's Donations in the Admin Bar (GiveWP)
Shows administrators the number and total of donations received today in the toolbar, linking to GiveWP's donations list, for a quick pulse during campaigns.
Opens WPIDE on
Code
add_action( 'admin_bar_menu', function ( $bar ) {
if ( ! function_exists( 'give_donation_amount' ) || ! current_user_can( 'view_give_reports' ) ) {
return;
}
$data = get_transient( 'wpide_give_today' );
if ( false === $data ) {
$ids = get_posts( array(
'post_type' => 'give_payment',
'post_status' => 'publish',
'numberposts' => 500,
'fields' => 'ids',
'no_found_rows' => true,
'date_query' => array( array( 'after' => wp_date( 'Y-m-d 00:00:00' ), 'inclusive' => true ) ),
) );
$data = array( 'count' => count( $ids ), 'total' => 0.0 );
foreach ( $ids as $id ) {
$data['total'] += (float) give_donation_amount( $id );
}
set_transient( 'wpide_give_today', $data, 5 * MINUTE_IN_SECONDS );
}
$total = function_exists( 'give_currency_filter' ) ? give_currency_filter( give_format_amount( $data['total'], array( 'sanitize' => false ) ) ) : number_format_i18n( $data['total'], 2 );
$bar->add_node( array(
'id' => 'wpide-give-today',
'title' => esc_html( sprintf( 'Today: %1$d donations, %2$s', $data['count'], html_entity_decode( wp_strip_all_tags( $total ) ) ) ),
'href' => admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ),
) );
}, 90 );
add_action( 'give_complete_donation', function () {
delete_transient( 'wpide_give_today' );
} );
Notes
Cached for five minutes; counts completed donations only.
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.