Skip to content

Instantly share code, notes, and snippets.

@MaryOJob
Forked from ronalfy/pmpro-extra-fields.php
Created September 22, 2022 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaryOJob/834a1f42194988e4576cd2ffa2366ee5 to your computer and use it in GitHub Desktop.
Save MaryOJob/834a1f42194988e4576cd2ffa2366ee5 to your computer and use it in GitHub Desktop.
PMPro - Add Extra Fields - Sync with Add User from Admin
<?php
/**
* Modified customer gist. Adds profile fields and saves them.
* Compatible with Add User from Admin 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/
*/
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<?php
if ( ! $user ) {
$user = new stdClass();
$user->ID = 0;
}
?>
<h3><?php _e( 'Extra profile information', 'blank' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e( 'Address' ); ?></label></th>
<td>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your address . ' ); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e( 'City' ); ?></label></th>
<td>
<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your city . ' ); ?></span>
</td>
</tr>
<tr>
<th><label for="province"><?php _e( 'Province' ); ?></label></th>
<td>
<input type="text" name="province" id="province" value="<?php echo esc_attr( get_the_author_meta( 'province', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your province . ' ); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e( 'Postal Code' ); ?></label></th>
<td>
<input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( 'postalcode', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your postal code . ' ); ?></span>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false; }
update_user_meta( $user_id, 'address', $_POST['address'] );
update_user_meta( $user_id, 'city', $_POST['city'] );
update_user_meta( $user_id, 'province', $_POST['province'] );
update_user_meta( $user_id, 'postalcode', $_POST['postalcode'] );
}
function pmpro_add_admin_extra_profile_fields() {
?>
<tr>
<th><label for="address"><?php _e( 'Address' ); ?></label></th>
<td>
<input type="text" name="address" id="address" value="" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your address . ' ); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e( 'City' ); ?></label></th>
<td>
<input type="text" name="city" id="city" value="" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your city . ' ); ?></span>
</td>
</tr>
<tr>
<th><label for="province"><?php _e( 'Province' ); ?></label></th>
<td>
<input type="text" name="province" id="province" value="" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your province . ' ); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e( 'Postal Code' ); ?></label></th>
<td>
<input type="text" name="postalcode" id="postalcode" value="" class="regular-text" /><br />
<span class="description"><?php _e( 'Please enter your postal code . ' ); ?></span>
</td>
</tr>
<?php
}
/**
* Add profile fields to add from admin.
*/
function pmpro_add_admin_profile_fields( $user, $user_id ) {
pmpro_add_admin_extra_profile_fields();
}
add_action( 'pmpro_add_member_fields', 'pmpro_add_admin_profile_fields', 10, 2 );
/**
* Save custom member data.
*/
function pmpro_add_admin_member_added_save( $user_id, $user ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false; }
update_user_meta( $user_id, 'address', sanitize_text_field( $_POST['address'] ) );
update_user_meta( $user_id, 'city', sanitize_text_field( $_POST['city'] ) );
update_user_meta( $user_id, 'province', sanitize_text_field( $_POST['province'] ) );
update_user_meta( $user_id, 'postalcode', sanitize_text_field( $_POST['postalcode'] ) );
}
add_action( 'pmpro_add_member_added', 'pmpro_add_admin_member_added_save', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment