Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/14cb5f0f65a8308c085c3e9a7128cc53 to your computer and use it in GitHub Desktop.
Save dwanjuki/14cb5f0f65a8308c085c3e9a7128cc53 to your computer and use it in GitHub Desktop.
Donate to specific categories or causes using PMPro User Fields
<?php
/**
* Donate to specific categories or causes using PMPro User Fields. This is different and independent from the PMPro Donations Add On.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_checkout_donations($level)
{
$donation = 0;
if (!empty($_REQUEST['donation_to_emma']) && is_numeric($_REQUEST['donation_to_emma'])) {
$donation = $donation + $_REQUEST['donation_to_emma'];
}
if (!empty($_REQUEST['donation_to_piper']) && is_numeric($_REQUEST['donation_to_piper'])) {
$donation = $donation + $_REQUEST['donation_to_piper'];
}
$level->initial_payment = $level->initial_payment + $donation;
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_donations");
function my_pmpro_checkout_donation_fields()
{
// Don't break if PMPro is out of date or not loaded.
if (!function_exists('pmpro_add_user_field')) {
return false;
}
// membership level IDs to show donation fields on
$show_donation_levels = array(1,2,3);
//define the fields
$fields = array();
$fields[] = new PMPro_Field('donation_to_emma', 'text', array(
'label' => 'Donation to Emma',
'size' => 7,
'profile' => 'admin_only',
'levels' => $show_donation_levels
));
$fields[] = new PMPro_Field('donation_to_piper', 'text', array(
'label' => 'Donation to Piper',
'size' => 7,
'profile' => 'admin_only',
'levels' => $show_donation_levels
));
pmpro_add_field_group("donations", "Donations");
//add the fields into the donations field group
foreach ($fields as $field) {
pmpro_add_user_field('donations', $field);
}
}
add_action('init', 'my_pmpro_checkout_donation_fields');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment