Last active
May 9, 2022 16:21
-
-
Save ipokkel/b7cde45dd3e64ff0c4f271656db95ebd to your computer and use it in GitHub Desktop.
Add required HTML attribute to required fields on PMPro Checkout Page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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