Created
March 10, 2026 10:49
-
-
Save dwanjuki/5bc0b687608b9077602a3e2d664acab7 to your computer and use it in GitHub Desktop.
Make the Member Directory search field required.
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 // 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' ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above targets the Member Directory page only.
Target the directory search field on any page: https://gist.github.com/dwanjuki/88a4958c64cd0ec6e2f275e869656e1a