Skip to content

Instantly share code, notes, and snippets.

@spivurno
Created October 23, 2014 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spivurno/73ad5599740a20521165 to your computer and use it in GitHub Desktop.
Save spivurno/73ad5599740a20521165 to your computer and use it in GitHub Desktop.
(Admin-only) Display how many spots are left in the choice label when using the GP Limit Choices perk
<?php
/**
* (Admin-only) Display how many spots are left in the choice label when using the GP Limit Choices perk
* http://gravitywiz.com/gravity-perks/
*/
add_filter( 'gplc_pre_render_choice', 'my_add_how_many_left_message', 10, 4 );
function my_add_how_many_left_message( $choice, $exceeded_limit, $field, $form ) {
if( ! current_user_can( 'administrator' ) ) {
return $choice;
}
$choice_counts = GWLimitChoices::get_choice_counts( $form['id'], $field );
$count = rgar( $choice_counts, $choice['value'] ) ? rgar( $choice_counts, $choice['value'] ) : 0;
$limit = rgar($choice, 'limit');
$how_many_left = $limit - $count;
$message = "($how_many_left spots left)";
$choice['text'] = $choice['text'] . " $message";
return $choice;
}
@Wordna1
Copy link

Wordna1 commented Jan 15, 2020

This snippet conflicts with the Display Spots Left in Choice Labels snippet. To fix the issue, simply rename the my_add_how_many_left_message in either snippet.

@spivurno
Copy link
Author

@Wordna1 I haven't seen these combined before. If you're showing the limit for all users, do you need to show it specifically for admins as well?

@Wordna1
Copy link

Wordna1 commented Jan 15, 2020

@spivurno
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment