Skip to content

Instantly share code, notes, and snippets.

@amdrew
Last active December 23, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amdrew/6631495 to your computer and use it in GitHub Desktop.
Save amdrew/6631495 to your computer and use it in GitHub Desktop.
Creates 2 additional fields (company and title) on Easy Digital Download's register account form, and saves them to the newly registered user's profile
<?php
/**
* Add company field to EDD's registration form
*/
function my_child_theme_register_account_fields() { ?>
<p id="edd-user-company-wrap">
<label for="edd-company">
<?php _e( 'Company', 'my-child-theme' ); ?>
<?php if( edd_no_guest_checkout() ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'Your company name', 'my-child-theme' ); ?></span>
<input name="edd_company" id="edd-company" class="edd-input" type="text" placeholder="<?php _e( 'Company', 'my-child-theme' ); ?>" title="<?php _e( 'Company', 'my-child-theme' ); ?>"/>
</p>
<p id="edd-user-company-title-wrap">
<label for="edd-company-title">
<?php _e( 'Title', 'my-child-theme' ); ?>
<?php if( edd_no_guest_checkout() ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'Enter your company title', 'my-child-theme' ); ?></span>
<input name="edd_company_title" id="edd-company-title" class="edd-input" type="text" placeholder="<?php _e( 'Title', 'my-child-theme' ); ?>" title="<?php _e( 'Title', 'my-child-theme' ); ?>"/>
</p>
<?php }
add_action( 'edd_register_account_fields_after', 'my_child_theme_register_account_fields' );
/**
* Make both the company and title field required at checkout
*/
function my_child_theme_validate_account_fields( $valid_data, $data ) {
if( ! edd_no_guest_checkout() )
return;
if( empty( $data['edd_company'] ) )
edd_set_error( 'invalid_company', __( 'Please enter a company.', 'my-child-theme' ) );
if( empty( $data['edd_company_title'] ) )
edd_set_error( 'invalid_company_title', __( 'Please enter a company title.', 'my-child-theme' ) );
}
add_action( 'edd_checkout_error_checks', 'my_child_theme_validate_account_fields', 10, 2 );
/**
* Add new section to WP profile and create custom fields
*/
function my_child_theme_extra_profile_fields( $user ) { ?>
<h3><?php _e( 'Additional Information', 'my-child-theme' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="company"><?php _e( 'Company', 'my-child-theme' ); ?></label></th>
<td>
<input type="text" name="company" id="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
<tr>
<th><label for="company"><?php _e( 'Title', 'my-child-theme' ); ?></label></th>
<td>
<input type="text" name="title" id="title" value="<?php echo esc_attr( get_the_author_meta( 'title', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
add_action( 'show_user_profile', 'my_child_theme_extra_profile_fields', 10 );
add_action( 'edit_user_profile', 'my_child_theme_extra_profile_fields', 10 );
/**
* Update the user's profile with the new field values on user registration
*/
function my_child_theme_user_register( $user_id ) {
if( ! edd_no_guest_checkout() )
return;
if ( isset( $_POST['edd_company'] ) )
update_user_meta( $user_id, 'company', $_POST['edd_company'] );
if ( isset( $_POST['edd_company_title'] ) )
update_user_meta( $user_id, 'title', $_POST['edd_company_title'] );
}
add_action( 'user_register', 'my_child_theme_user_register' );
/**
* Save the fields when the values are changed on the profile page
*/
function my_child_theme_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, 'company', $_POST['company'] );
update_user_meta( $user_id, 'title', $_POST['title'] );
}
add_action( 'personal_options_update', 'my_child_theme_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_child_theme_save_extra_profile_fields' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment