Forked from rickalday/populate_v3_dropdown_from_url.php
Created
March 31, 2025 21:04
-
-
Save gspice/fba1a5c43ae46509fdeb7a07f93e46db to your computer and use it in GitHub Desktop.
Populate GiveWP v3 form dropdown from URL value
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 | |
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