Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add HTML5 required attribute to fields on the Member Profile Edit page that has the pmpro_required class assigned.
<?php
/**
* Add HTML5 attribute required to all required PMPro fields on the front end
* PMPro Member Profile Edit page.
*
* 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_profile_edit_add_required_html_attribute() {
global $pmpro_pages;
// We only want to do this on the member profile edit page.
if ( ! is_page( $pmpro_pages['member_profile_edit'] ) ) {
return;
}
// Add JavaScript to the footer that adds the required attribute to al fields with the class pmpro_required.
?>
<script>
[...document.querySelectorAll(".pmpro_required")].forEach(element => { element.setAttribute("required", "required") });
document.getElementById('user_email').setAttribute("required", "required");
</script>
<?php
}
add_action( 'wp_footer', 'my_pmpro_profile_edit_add_required_html_attribute', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment