Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

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 ipokkel/dc7ae1570761009423cd2e998c741242 to your computer and use it in GitHub Desktop.
Save ipokkel/dc7ae1570761009423cd2e998c741242 to your computer and use it in GitHub Desktop.
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