Last active
November 7, 2022 09:16
-
-
Save ipokkel/9cb626b95e37d1a45faba69ac8fe21fa to your computer and use it in GitHub Desktop.
Move required asterisk on PMPro checkout page from after the field to inside the field label #pmpro #checkout #asterisk #jquery
This file contains 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 | |
/** | |
* Move required asterisk on PMPro checkout page from after the field to inside the field label | |
* | |
* 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_move_required_asterisk_span() { | |
global $pmpro_pages; | |
$shipping_asterisk = false; | |
if ( defined( 'PMPRO_SHIPPING_VERSION' ) && defined( 'PMPRO_SHIPPING_SHOW_REQUIRED' ) && PMPRO_SHIPPING_SHOW_REQUIRED ) { | |
$shipping_asterisk = true; | |
} | |
if ( is_page( $pmpro_pages['checkout'] ) || is_page( $pmpro_pages['billing'] ) || is_page( $pmpro_pages['member_profile_edit'] ) ) { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function () { | |
// HTML for required asterisk | |
var asteriskHtml = '<span class="pmpro_asterisk"> <abbr title="Required Field">*</abbr> </span>'; | |
// Remove asterisks | |
jQuery('.pmpro_checkout-field-required .pmpro_asterisk').remove(); | |
jQuery('.pmpro_required').next('.pmpro_asterisk').remove(); | |
<?php | |
if ( $shipping_asterisk ) { | |
?> | |
jQuery('#pmpro_shipping_address_fields .pmpro_checkout-fields #shipping-fields .pmpro_asterisk').remove(); | |
<?php | |
} | |
?> | |
// Add asterisk inside label for all User fields. For backward compatibility exclude deprecated Register Helper grouped input fields. | |
jQuery('div.pmpro_checkout-field-required label').not('.pmprorh_checkbox_label').not('.pmprorh_radio_label').append(asteriskHtml); | |
// Array of required by default fields | |
var otherFields = ['username','password','password2','bemail','bconfirmemail','first_name','last_name','bfirstname','blastname','baddress1','baddress2','bcity','bstate','bzipcode','bcountry','bphone']; | |
<?php | |
if ( $shipping_asterisk ) { | |
?> | |
var shippingFields = ['sfirstname','slastname','saddress1','saddress2','scity','sstate','szipcode','scountry','sphone']; | |
// add shipping fields to otherFields array | |
otherFields = otherFields.concat(shippingFields); | |
<?php | |
} | |
?> | |
// Add asterisk for fields in otherFields array. | |
jQuery.each(otherFields, function (i, value){ | |
jQuery('[name="' + value + '"]').next('.pmpro_asterisk').remove(); | |
jQuery('[name="' + value + '"]').prev('label').append(asteriskHtml); | |
jQuery('[name="pmpro_' + value + '"]').prev('label').append(asteriskHtml); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'wp_footer', 'my_pmpro_move_required_asterisk_span', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment