Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Created March 10, 2026 10:49
Show Gist options
  • Select an option

  • Save dwanjuki/5bc0b687608b9077602a3e2d664acab7 to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/5bc0b687608b9077602a3e2d664acab7 to your computer and use it in GitHub Desktop.
Make the Member Directory search field required.
<?php // copy from below.
/*
* Make the Member Directory search field required.
* Display a custom validation message if the field is left blank.
*
* 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_pmpromd_search_field_required_js() {
?>
<script>
jQuery(function() {
var field = jQuery( '#ps' );
if ( field.length) {
// Make Member Directory search input required.
field.prop('required', true);
// Set a custom validation message.
field.on( 'invalid', function() {
this.setCustomValidity( 'Please enter a Member Number.' );
});
// Clear the message once the user starts typing.
field.on( 'input', function() {
this.setCustomValidity( '' );
});
}
});
</script>
<?php
}
add_action( 'pmpro_member_directory_before', 'my_pmpromd_search_field_required_js' );
@dwanjuki
Copy link
Author

The above targets the Member Directory page only.
Target the directory search field on any page: https://gist.github.com/dwanjuki/88a4958c64cd0ec6e2f275e869656e1a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment