Created
October 5, 2019 17:22
-
-
Save danmaby/d521dc67258ff7d5711a67a10baf1a91 to your computer and use it in GitHub Desktop.
Populate GiveWP fields via query string
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
// 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