Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active August 21, 2024 14:59
Show Gist options
  • Save ipokkel/8d64ab533298c36be58c787ddb5fb780 to your computer and use it in GitHub Desktop.
Save ipokkel/8d64ab533298c36be58c787ddb5fb780 to your computer and use it in GitHub Desktop.
Add the HTML5 required attribute to all checkout fields that has the pmpro_required class for front-end validation that checks that a required field is not empty before processing the form submit.
<?php
/**
* This recipe adds the HTML5 attribute "required" to all required
* fields on the checkout page that has the class "pmpro_required"
* to check that a required field is not empty before submitting.
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required
*
* 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_attribute_to_input() {
if ( pmpro_is_checkout() ) {
?>
<script>
jQuery(document).ready(function ($) {
jQuery('.pmpro_required').attr('required', 'required');
});
</script>
<?php
}
}
add_action( 'wp_footer', 'my_pmpro_add_required_attribute_to_input' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment