Last active
March 24, 2026 13:47
-
-
Save dwanjuki/c11c06461f13b2e65fe387893039be7b to your computer and use it in GitHub Desktop.
Reposition a Middle Name User Field between the First Name and Last Name fields from the Add Name to Checkout Add On.
This file contains hidden or 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 | |
| /** | |
| * Reposition a Middle Name User Field between the First Name and Last Name | |
| * fields from the Add Name to Checkout Add On. | |
| * | |
| * Note: The field's name must be middle_name, label can be something else. | |
| * | |
| * 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/ | |
| */ | |
| /** | |
| * Move field on checkout page. | |
| */ | |
| function my_pmproan2c_checkout_move_middle_name_user_field() { | |
| // Bail if Add Name to Checkout Add On isn't active. | |
| if ( ! function_exists( 'pmproan2c_pmpro_checkout_after_password' ) ) { | |
| return; | |
| } | |
| // Bail if PMPro isn't active, or not on checkout page. | |
| if ( ! function_exists( 'pmpro_is_checkout' ) || ! pmpro_is_checkout() ) { | |
| return; | |
| } | |
| ?> | |
| <script> | |
| document.addEventListener('DOMContentLoaded', function () { | |
| const middleNameDiv = document.getElementById('middle_name_div'); | |
| const an2cDiv = document.getElementById('pmpro_an2c'); | |
| const firstNameField = document.querySelector('.pmpro_form_field-firstname'); | |
| if (middleNameDiv && an2cDiv) { | |
| an2cDiv.classList.replace('pmpro_cols-2', 'pmpro_cols-3'); | |
| } | |
| if (middleNameDiv && firstNameField) { | |
| firstNameField.insertAdjacentElement('afterend', middleNameDiv); | |
| } | |
| }); | |
| </script> | |
| <?php | |
| } | |
| add_action( 'wp_footer', 'my_pmproan2c_checkout_move_middle_name_user_field' ); | |
| /** | |
| * Move field on Member Profile Edit page. | |
| */ | |
| function my_pmproan2c_profile_edit_move_middle_name_user_field() { | |
| global $pmpro_pages, $post; | |
| // Bail if PMPro isn't active or Add Name to Checkout Add On isn't active. | |
| if ( ! function_exists( 'pmpro_is_checkout' ) || ! function_exists( 'pmproan2c_pmpro_checkout_after_password' ) ) { | |
| return; | |
| } | |
| // Bail if we're not on the PMPro Member Profile Edit page. | |
| if ( empty( $pmpro_pages['member_profile_edit'] ) || ! is_page( $pmpro_pages['member_profile_edit'] ) ) { | |
| return; | |
| } | |
| ?> | |
| <script> | |
| document.addEventListener('DOMContentLoaded', function () { | |
| const accountInfo = document.getElementById('pmpro_member_profile_edit-account-information'); | |
| const middleNameDiv = document.getElementById('middle_name_div'); | |
| const firstNameField = accountInfo ? accountInfo.querySelector('.pmpro_form_field-first_name') : null; | |
| const colsDiv = accountInfo ? accountInfo.querySelector('.pmpro_cols-2') : null; | |
| if (accountInfo && firstNameField && middleNameDiv && colsDiv) { | |
| colsDiv.classList.replace('pmpro_cols-2', 'pmpro_cols-3'); | |
| firstNameField.insertAdjacentElement('afterend', middleNameDiv); | |
| } | |
| }); | |
| </script> | |
| <?php | |
| } | |
| add_action( 'wp_footer', 'my_pmproan2c_profile_edit_move_middle_name_user_field' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment