Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Created February 10, 2023 07:03
Show Gist options
  • Save dwanjuki/fef0afbfa2e05b94ac504125dcd7ed14 to your computer and use it in GitHub Desktop.
Save dwanjuki/fef0afbfa2e05b94ac504125dcd7ed14 to your computer and use it in GitHub Desktop.
Add First Name field to checkout, after username
/**
* Add first_name field to checkout, after username
*
* 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_add_firstname_to_checkout() {
// Don't break if PMPro is out of date or not loaded.
if(!function_exists( 'pmpro_add_user_field' )) {
return false;
}
// Show fields on initial checkout only
if( is_user_logged_in() ) {
return false;
}
// This is where our fields code will go.
$fields = array();
// PMPRo_Field object for first_name
$fields[] = new PMPro_Field(
"first_name",
"text",
array(
"label" => "First Name",
"profile" => true,
"required" => true,
)
);
foreach($fields as $field) {
pmpro_add_user_field(
'after_username', // Location of fields on checkout page
$field // PMPRo_Field object
);
}
}
add_action( 'init', 'my_pmpro_add_firstname_to_checkout' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment