Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gspice/fba1a5c43ae46509fdeb7a07f93e46db to your computer and use it in GitHub Desktop.
Save gspice/fba1a5c43ae46509fdeb7a07f93e46db to your computer and use it in GitHub Desktop.
Populate GiveWP v3 form dropdown from URL value
<?php
add_action('givewp_donation_form_schema', function($form) {
//Get field by name attribute
$field = $form->getNodeByName('field_name_here');
// 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['value'])) {
$defaultValue = give_clean($_GET['value']);
// if the value is set in the referrer query string, the form is being embed so we should use that instead
} elseif ($_SERVER['HTTP_REFERER']){
parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $query);
if ($query['value']) {
$defaultValue = give_clean($query['value']);
}
}
// 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