Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active May 9, 2022 16:21
Show Gist options
  • Save ipokkel/b7cde45dd3e64ff0c4f271656db95ebd to your computer and use it in GitHub Desktop.
Save ipokkel/b7cde45dd3e64ff0c4f271656db95ebd to your computer and use it in GitHub Desktop.
Add required HTML attribute to required fields on PMPro Checkout Page.
<?php
/**
* Add HTML5 attribute required to all required PMPro 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_add_required_html_attribute() {
global $pmpro_pages;
if ( is_page( $pmpro_pages['checkout'] ) || is_page( $pmpro_pages['billing'] ) || is_page( $pmpro_pages['member_profile_edit'] ) ) {
?>
<script>
[...document.querySelectorAll(".pmpro_required")].forEach(element => { element.setAttribute("required", "required") });
<?php if ( is_page( $pmpro_pages['member_profile_edit'] ) ) { ?>
document.getElementById('user_email').setAttribute("required", "required");
<?php } ?>
</script>
<?php
}
}
add_action( 'wp_footer', 'my_pmpro_add_required_html_attribute', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment