Snippi
A super awesome snippet tool.
- 1.
<?php
- 2.
/**
- 3.
* Gravity Wiz // Gravity Forms // Entry Count Shortcode
- 4.
*
- 5.
* Extends the [gravityforms] shortcode, providing a custom action to retrieve the total entry count and
- 6.
* also providing the ability to retrieve counts by entry status (i.e. 'trash', 'spam', 'unread', 'starred').
- 7.
*
- 8.
* @version 1.0
- 9.
* @author David Smith <[email protected]>
- 10.
* @license GPL-2.0+
- 11.
* @link http://gravitywiz.com/...
- 12.
*
- 13.
* added alternate text to display until a specified minimum count threshold is achieved.
- 14.
* Dean Suhr 2021-09-20
- 15.
* [gravityforms action="entry_count" id="1" display_min="500" display_min_alt_text="this text is displayed until the display_min threshold is reached"]
- 16.
/
- 17.
*
- 18.
add_filter( 'gform_shortcode_entry_count', 'gwiz_entry_count_shortcode', 10, 2 );
- 19.
function gwiz_entry_count_shortcode( $output, $atts ) {
- 20.
- 21.
extract( shortcode_atts( array(
- 22.
'id' => false,
- 23.
'status' => 'total', // accepts 'total', 'unread', 'starred', 'trash', 'spam'
- 24.
'format' => false, // should be 'comma', 'decimal'
- 25.
'display_min' => false, // minimum value before count is displayed
- 26.
'display_min_alt_text' => false // alternate text if display_min is not reached
- 27.
), $atts ) );
- 28.
- 29.
$valid_statuses = array( 'total', 'unread', 'starred', 'trash', 'spam' );
- 30.
- 31.
if( ! $id || ! in_array( $status, $valid_statuses ) ) {
- 32.
return current_user_can( 'update_core' ) ? __( 'Invalid "id" (the form ID) or "status" (i.e. "total", "trash", etc.) parameter passed.' ) : '';
- 33.
}
- 34.
- 35.
$counts = GFFormsModel::get_form_counts( $id );
- 36.
$output = rgar( $counts, $status );
- 37.
- 38.
if ( $display_min > 0 and $output >= $display_min ) {
- 39.
// have reached numeric display minimum threshold
- 40.
if( $format ) {
- 41.
$format = $format == 'decimal' ? '.' : ',';
- 42.
$output = number_format( $entries_left, 0, false, $format );
- 43.
}
- 44.
} else {
- 45.
// have NOT reached numeric display minimum threshold
- 46.
$output = $display_min_alt_text;
- 47.
}
- 48.
- 49.
return $output;
- 50.
}