Skip to content

Instantly share code, notes, and snippets.

@danmaby
Created October 5, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmaby/d521dc67258ff7d5711a67a10baf1a91 to your computer and use it in GitHub Desktop.
Save danmaby/d521dc67258ff7d5711a67a10baf1a91 to your computer and use it in GitHub Desktop.
Populate GiveWP fields via query string
// Hooking into the single form view.
add_action( 'give_post_form_output', 'give_populate_amount_name_email' );
function give_populate_amount_name_email() {
?>
<script>
// use an enclosure so we don't pollute the global space
(function(window, document, $, undefined){
'use strict';
var giveCustom = {};
giveCustom.init = function() {
// Get the amount from the URL
var getamount = giveCustom.getQueryVariable("amount");
var amount = '1.00';
// Set fallback in case URL variable isn't set
if ( getamount !== false ) {
amount = getamount;
}
var firstname = ( giveCustom.getQueryVariable("first") !== false ) ? decodeURI(giveCustom.getQueryVariable("first")) : '';
var lastname = ( giveCustom.getQueryVariable("last") !== false ) ? decodeURI(giveCustom.getQueryVariable("last")) : '';
var email = ( giveCustom.getQueryVariable("email") !== false ) ? decodeURI(giveCustom.getQueryVariable("email")) : '';
var company = ( giveCustom.getQueryVariable("company") !== false ) ? decodeURI(giveCustom.getQueryVariable("company")) : '';
var currency = ( giveCustom.getQueryVariable("currency") !== false ) ? decodeURI(giveCustom.getQueryVariable("currency")) : '';
// Populate the amount field, then update the total
if ( $('#give-amount').length > 0 ) {
$('#give-amount')
.val(amount)
.focus()
.trigger('blur');
}
if ( firstname !== false && $('#give-first-name-wrap input.give-input').length > 0 ) {
$('#give-first-name-wrap input.give-input')
.val(firstname);
}
if ( lastname !== false && $('#give-last-name-wrap input.give-input').length > 0 ) {
$('#give-last-name-wrap input.give-input')
.val(lastname);
}
if ( email !== false && $('#give-email-wrap input.give-input').length > 0 ) {
$('#give-email-wrap input.give-input')
.val(email);
}
if ( company !== false && $('#give-company-wrap input.give-input').length > 0 ) {
$('#give-company-wrap input.give-input')
.val(company);
}
if ( currency !== false && $('.give-total-wrap select.give-cs-select-currency').length > 0 ) {
$('.give-total-wrap select.give-cs-select-currency')
.val(currency);
}
}
giveCustom.getQueryVariable = function( variable ) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
giveCustom.init();
})(window, document, jQuery);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment