Forked from ipokkel/appsumo-code-redeem-with-pmpro-signup-shortcode.php
Last active
October 26, 2022 15:25
-
-
Save michaelbeil/6d99dc26a6540bd3bd2f8e68e9c73f19 to your computer and use it in GitHub Desktop.
An example of a customized AppSumo Code Redeem page using PMPro Signup Shortcode.
This file contains 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 | |
/** | |
* This recipe creates a customized easy signup and code redeem page | |
* for AppSumo promotions. | |
* | |
* This recipe assumes the following: | |
* PMPro Signup Shortcode is installed and active. | |
* @link https://www.paidmembershipspro.com/add-ons/pmpro-signup-shortcode/ | |
* | |
* You have a page with [pmpro_signup] shortcode and it is configured correctly. | |
* You have a custom confirmation page to redirect to after the code was redeemed (optional) | |
* The level for the AppSumo redeem code is level 3 | |
* | |
* 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/ | |
*/ | |
/* | |
Suggested shorcode configuration, change "Redeem Now" for the submit button to suit your requirements. | |
If you would like to show a "Log In" link below the submit button | |
set change the login option from 0 to 1 in the shortcode, | |
e.g. login="1" | |
[pmpro_signup level="3" short="emailonly" hidelabels="true" submit_button="Redeem Now" custom_fields="true" login="0"] | |
*/ | |
/* Hide "Confirm Password" field (Optional) */ | |
add_filter( 'pmpro_checkout_confirm_password', '__return_false' ); | |
/* Hide "Confirm E-mail Address" field (Optional) */ | |
add_filter( 'pmpro_checkout_confirm_email', '__return_false' ); | |
/** | |
* Change the Discount Code text strings if a specific | |
* level is specified as a querystring parameter in the URL. | |
*/ | |
function my_pmpro_discount_code_text_strings_1610643091698( $translated_text, $text, $domain ) { | |
// Dont change text if we're not on the level checkout for the AppSumo deal. | |
if ( isset( $_REQUEST['level'] ) && 5 != $_REQUEST['level'] ) { | |
return $translated_text; | |
} | |
// Do you have a discount code? | |
// Click here to enter your discount code | |
switch ( $translated_text ) { | |
case 'Discount Code': | |
$translated_text = __( 'AppSumo Code', 'paid-memberships-pro' ); | |
break; | |
case 'Do you have a discount code?': | |
$translated_text = __( 'Do you have an AppSumo redemption code?', 'paid-memberships-pro' ); | |
break; | |
case 'Click here to enter your discount code': | |
$translated_text = __( 'Click here to redeem your AppSumo code', 'paid-memberships-pro' ); | |
break; | |
} | |
return $translated_text; | |
} | |
add_filter( 'gettext', 'my_pmpro_discount_code_text_strings_1610643091698', 20, 3 ); | |
/** | |
* Creates custom fields for membership registration. | |
* | |
* @requires Register Helper Add On | |
* @link https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/ | |
*/ | |
function my_pmprorh_init_custom_registration_fields_1610524957() { | |
// don't break if Register Helper is not loaded | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return false; | |
} | |
// define the fields | |
$fields = array(); | |
// TEXT FIELD | |
$fields[] = new PMProRH_Field( | |
'first_name', // input field name, used as meta key | |
'text', // field type | |
array( | |
'label' => 'First Name', // display custom label, if not used field name will be used | |
'html_attributes' => array( | |
'placeholder' => 'First Name', | |
'required' => 'required', | |
), | |
'levels' => array( 3 ), // levels to display field for | |
'class' => 'first_name', // custom class for input field | |
'profile' => true, // show on profile | |
'memberslistcsv' => true, // include when using export members to csv | |
'addmember' => true, // include when using add member from admin | |
'required' => true, // make field required | |
'save_function' => 'save_first_and_last_name_1610524713986', | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'last_name', // input field name, used as meta key | |
'text', // field type | |
array( | |
'label' => 'Last Name', // display custom label, if not used field name will be used | |
'html_attributes' => array( | |
'placeholder' => 'Last Name', | |
'required' => 'required', | |
), | |
'levels' => array( 3 ), // levels to display field for | |
'class' => 'last_name', // custom class for input field | |
'profile' => true, // show on profile | |
'memberslistcsv' => true, // include when using export members to csv | |
'addmember' => true, // include when using add member from admin | |
'required' => true, // make field required | |
'save_function' => 'save_first_and_last_name_1610524713986', | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'discount_code', // input field name, used as meta key | |
'text', // field type | |
array( | |
'label' => 'Redeem code', // display custom label, if not used field name will be used | |
'html_attributes' => array( | |
'placeholder' => 'Redeem code', | |
'required' => 'required', | |
), | |
'levels' => array( 3 ), // levels to display field for | |
'class' => 'discount_code', // custom class for input field | |
'memberslistcsv' => true, // include when using export members to csv | |
'addmember' => true, // include when using add member from admin | |
'required' => true, // make field required | |
) | |
); | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( | |
'after_email', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
} | |
unset( $field ); | |
// that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'init', 'my_pmprorh_init_custom_registration_fields_1610524957' ); | |
function save_first_and_last_name_1610524713986( $user_id ) { | |
global $current_user; | |
if ( ! empty( $_REQUEST['first_name'] ) ) { | |
$first_name = trim( sanitize_text_field( $_REQUEST['first_name'] ) ); | |
} elseif ( ! empty( $_SESSION['first_name'] ) ) { | |
$first_name = trim( sanitize_text_field( $_SESSION['first_name'] ) ); | |
} elseif ( is_user_logged_in() ) { | |
$first_name = $current_user->first_name; | |
} else { | |
$first_name = ''; | |
} | |
if ( ! empty( $_REQUEST['last_name'] ) ) { | |
$last_name = trim( sanitize_text_field( $_REQUEST['last_name'] ) ); | |
} elseif ( ! empty( $_SESSION['last_name'] ) ) { | |
$last_name = trim( sanitize_text_field( $_SESSION['last_name'] ) ); | |
} elseif ( is_user_logged_in() ) { | |
$last_name = $current_user->last_name; | |
} else { | |
$last_name = ''; | |
} | |
update_user_meta( $user_id, 'first_name', $first_name ); | |
update_user_meta( $user_id, 'last_name', $last_name ); | |
update_post_meta( $user_id, 'billing_name', $first_name . ' ' . $last_name ); | |
} | |
add_action( 'pmpro_after_checkout', 'save_first_and_last_name_1610524713986' ); | |
/* | |
* Redirect members to a custom confirmation page after checkout | |
* for a specific level. | |
* | |
* Remove or disable this to redirect to the default PMPro Confirmation page | |
* after checkout. | |
*/ | |
function my_pmpro_confirmation_url_per_post_slug( $rurl, $user_id, $pmpro_level ) { | |
$page_id = 136; //change this to your custom confirmation page ID | |
$page_url = get_permalink( $page_id ); | |
if ( $page_url && 3 == $pmpro_level->id ) { | |
$rurl = $page_url; | |
} | |
return $rurl; | |
} | |
add_filter( 'pmpro_confirmation_url', 'my_pmpro_confirmation_url_per_post_slug', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment