Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Perks // GP Limit Choices // Display Spots Left in Choice Labels
<?php
/**
* 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_remove_choices', '__return_false' );
add_filter( 'gplc_pre_render_choice', 'my_add_how_many_left_message', 10, 5 );
function my_add_how_many_left_message( $choice, $exceeded_limit, $field, $form, $count ) {
$limit = rgar( $choice, 'limit' );
$how_many_left = max( $limit - $count, 0 );
$message = "($how_many_left spots left)";
$choice['text'] = $choice['text'] . " $message";
return $choice;
}
@dankip

This comment has been minimized.

Copy link

@dankip dankip commented Dec 29, 2015

Hi David, should this code be added to the Wordpress theme's function.php file or Child theme function.php? I have tried both and receive a parse error. Thank you for your help.

@bitdragon88

This comment has been minimized.

Copy link

@bitdragon88 bitdragon88 commented Oct 12, 2016

I added the code in the following file successfully :
wp-content/plugins/gwlimitchoices/gwlimitchoices.php

@SahilMepani

This comment has been minimized.

Copy link

@SahilMepani SahilMepani commented Jan 27, 2017

Can the same be added to dropdown?

@mike-weiner

This comment has been minimized.

Copy link

@mike-weiner mike-weiner commented Jul 17, 2020

How could this be modified to only add the " x spots left" message to certain types of fields or by certain fields only using their field id?

@mike-weiner

This comment has been minimized.

Copy link

@mike-weiner mike-weiner commented Jul 18, 2020

For anyone else that might want to restrict this to just a certain field on a certain form, here is how I achieved that:

You will need to modify the $form['id'] and $field->id that you want to target within the condition of the if-statement.

add_filter( 'gplc_remove_choices', '__return_false' ); 

add_filter( 'gplc_pre_render_choice', 'mw_add_how_many_left_message_to_limited_fields', 10, 5 );
function mw_add_how_many_left_message_to_limited_fields( $choice, $exceeded_limit, $field, $form, $count ) {

	$limit         = rgar( $choice, 'limit' );
	$how_many_left = max( $limit - $count, 0 );

	$message = "($how_many_left Spots Left)"; // Modify Message Displayed for # of "Spots Left"

	if ( $field->get_input_type() == 'select' && $field->id == '17' && $form['id'] == '5') {
		$choice['text'] = $choice['text'] . " $message";
        }

	return $choice;
}
@spivurno

This comment has been minimized.

Copy link
Owner Author

@spivurno spivurno commented Jul 18, 2020

Thanks for sharing, @mike-weiner. Another option is to include the target form and field IDs in the filter name like so:

gplc_pre_render_choice_5_17

@mike-weiner

This comment has been minimized.

Copy link

@mike-weiner mike-weiner commented Jul 18, 2020

@spivurno Thanks for the quick response! That is much easier!

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