Skip to content

Instantly share code, notes, and snippets.

@rickalday
Created July 18, 2025 16:48
Show Gist options
  • Select an option

  • Save rickalday/4e321d0394d9f6fcdd5d9601d6cf3dbf to your computer and use it in GitHub Desktop.

Select an option

Save rickalday/4e321d0394d9f6fcdd5d9601d6cf3dbf to your computer and use it in GitHub Desktop.
Sets the donation frequency based on the url parameter. Eg. yoursite.com/?payment_type=monthly
<?php
add_action("givewp_donation_form_enqueue_scripts", function () {
?>
<script>
document.addEventListener("DOMContentLoaded", function () {
const validValues = ["one-time", "daily", "week", "month", "quarter", "year"];
const params = new URLSearchParams(window.parent.location.search);
const paymentType = params.get("payment_type");
if (!paymentType || !validValues.includes(paymentType)) return;
// Run after a short delay to ensure UI is ready
setTimeout(() => {
const radios = document.querySelectorAll('input[type="radio"][name="subscriptionPeriod"]');
radios.forEach(radio => {
if (radio.value === paymentType) {
let label = document.querySelector(`label[for="${radio.id}"]`) || radio.closest('label');
if (label) {
// Build a real-looking MouseEvent
const clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
composed: true // helps Safari treat it as trusted-like
});
// Dispatch directly on the label
label.dispatchEvent(clickEvent);
} else {
// Fallback if no label found
radio.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
composed: true
}));
}
}
});
}, 100); // small delay to let GiveWP UI finish rendering
});
</script>
<?php
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment