Created
November 27, 2025 15:36
-
-
Save alexwcoleman/e85e150604f679514ecde9862296554d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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