Skip to content

Instantly share code, notes, and snippets.

@alexwcoleman
Created November 27, 2025 15:36
Show Gist options
  • Select an option

  • Save alexwcoleman/e85e150604f679514ecde9862296554d to your computer and use it in GitHub Desktop.

Select an option

Save alexwcoleman/e85e150604f679514ecde9862296554d to your computer and use it in GitHub Desktop.
<?php
// URLs make values lower case, so values in the select field must be lowercase
// CSS can somewhat help those values
add_action('givewp_donation_form_schema', function($form) {
// 1. Define the variable here
$target_param = 'test_dropdown';
// Get field by name attribute using the variable
$field = $form->getNodeByName($target_param);
// if the field is not found, return
if (!$field) {
return;
}
// get the current default value
$defaultValue = $field->getDefaultValue();
// if the value is set in the query string, use that as the default
// eg. yoursite.com/?value=somevalue
if (isset($_GET[$target_param])) {
$defaultValue = give_clean($_GET[$target_param]);
// if the value is set in the referrer query string, the form is being embed so we should use that instead
} elseif (isset($_SERVER['HTTP_REFERER'])) {
parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $query);
// Added isset() here to prevent "Undefined index" warnings
if (isset($query[$target_param])) {
$defaultValue = give_clean($query[$target_param]);
}
}
// we don't want to set the default value if it's the same as the field's default value
if ($defaultValue === $field->getDefaultValue()){
return;
}
$field->defaultValue($defaultValue);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment