Last active
August 22, 2023 16:11
-
-
Save greenhornet79/8893e8ca508b8a81afcb244bf92fcec6 to your computer and use it in GitHub Desktop.
add custom fields to the Leaky Paywall registration form
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 | |
// add fields to registration form | |
add_action( 'leaky_paywall_after_password_registration_field', 'zeen101_custom_registration_fields' ); | |
function zeen101_custom_registration_fields() { | |
?> | |
<div class="form-row"> | |
<label>Company</label> | |
<input type="text" value="" name="company"> | |
</div> | |
<div class="form-row"> | |
<input type="checkbox" checked id="subscribeNews" name="newsletter_subscribe" value="newsletter"> <label for="subscribeNews">Yes, sign me up for the newsletter</label> | |
</div> | |
<?php | |
} | |
// save fields if user is created | |
add_action( 'leaky_paywall_form_processing', 'zeen101_custom_registration_fields_save', 10, 6 ); | |
function zeen101_custom_registration_fields_save( $post_data, $user_id, $price, $mode, $site, $level_id ) { | |
if ( $post_data['company'] ) { | |
update_user_meta( $user_id, '_company', sanitize_text_field( $post_data['company'] ) ); | |
} | |
if ( isset( $_POST['newsletter_subscribe'] ) ) { | |
update_user_meta( $user_id, '_newsletter_subscribe', 'yes' ); | |
} else { | |
update_user_meta( $user_id, '_newsletter_subscribe', 'no' ); | |
} | |
} | |
// display the field in the user's WP admin profile | |
add_action( 'show_user_profile', 'zeen101_admin_custom_fields', 99 ); | |
add_action( 'edit_user_profile', 'zeen101_admin_custom_fields', 99 ); | |
function zeen101_admin_custom_fields( $user ) { | |
$company = get_user_meta( $user->ID, '_company', true ); | |
$newsletter = get_user_meta( $user->ID, '_newsletter_subscribe', true ); | |
?> | |
<table class="form-table"> | |
<tr> | |
<th><label>Company</label></th> | |
<td> | |
<?php echo $company; ?> | |
</td> | |
</tr> | |
<tr> | |
<th><label>Signed Up For Newsletter</label></th> | |
<td> | |
<?php echo $newsletter; ?> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
// add custom field data to Leaky Paywall Reporting Tool export | |
add_filter( 'leaky_paywall_reporting_tool_meta', 'zeen101_reporting_tool_custom_fields_headers' ); | |
function zeen101_reporting_tool_custom_fields_headers( $meta ) { | |
$meta[] = 'company'; | |
$meta[] = 'newsletter_signup'; | |
return $meta; | |
} | |
add_filter( 'leaky_paywall_reporting_tool_user_meta', 'zeen101_reporting_tool_custom_fields_values', 10, 2 ); | |
function zeen101_reporting_tool_custom_fields_values( $user_meta, $user_id ) { | |
$company = get_user_meta( $user_id, '_company', true ); | |
$newsletter = get_user_meta( $user_id, '_newsletter_subscribe', true ); | |
$user_meta[$user_id]['company'] = $company; | |
$user_meta[$user_id]['newsletter_signup'] = $newsletter; | |
return $user_meta; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment