Skip to content

Add move user field with JS #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions user-fields/move-user-field-to-account-info-js.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Move a custom user field from its default location to below the
* email address fields in the account information section with JavaScript.
*
* Set the field name you want to move in the `fieldName` variable on line 25.
*
* title: Move custom user field to account information section.
* layout: snippet-example
* collection: user-fields
* category: custom-fields
*
* 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/
*/
function my_pmpro_move_user_field_to_account_information() {
// Only run on the checkout page.
if ( ! function_exists( 'pmpro_is_checkout' ) || ! pmpro_is_checkout() ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var fieldName = 'field_name_here'; // The name of the user field you want to move
var userField = $('#' + fieldName + '_div');

// Check if the userField exists
if (userField.length) {
// Look for the parent div containing email fields
var emailContainer = $('.pmpro_form_field-bemail').closest('.pmpro_cols-2');

// Check if the confirm email field exists
var confirmEmailField = $('.pmpro_form_field-bconfirmemail');

// If the email container exists, append website field after it
if (emailContainer.length) {
emailContainer.after(userField);
}
}
});
</script>
<?php
}
add_action( 'wp_footer', 'my_pmpro_move_user_field_to_account_information' );