Forked from ipokkel/lock-down-first-and-last-name.php
Created
March 17, 2026 11:26
-
-
Save dwanjuki/497c0db3a5a0445d499d45cea27deab6 to your computer and use it in GitHub Desktop.
Make First Name, Last Name and a custom field read-only for non-admin users
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 | |
| /** | |
| * This recipe assigns the readonly attribute to the fields | |
| * first_name, last_name, and date_de_naissance on profile edit pages | |
| * for non-admin users. | |
| * | |
| * 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/ | |
| */ | |
| /** | |
| * Output the JS to make fields readonly for non-admins. | |
| */ | |
| function my_pmpro_member_read_only_fields_js() { | |
| // Bail if admin or not logged in user. | |
| if ( ! is_user_logged_in() || current_user_can( 'manage_options' ) ) { | |
| return; | |
| } | |
| ?> | |
| <script>jQuery(document).ready(function ($) { | |
| $('#first_name').attr('readonly', true); | |
| $('#last_name').attr('readonly', true); | |
| $('#date_de_naissance').attr('readonly', true); | |
| }); </script> | |
| <?php | |
| } | |
| /** | |
| * Add the readonly field JS to the backend profile update page. | |
| */ | |
| function my_pmpro_wp_profile_update_readonly_fields() { | |
| // We're on the WP Dashboard but not an admin. | |
| global $pagenow; | |
| if ( is_admin() && 'profile.php' === $pagenow ) { | |
| add_action( 'admin_footer', 'my_pmpro_member_read_only_fields_js' ); | |
| } | |
| } | |
| add_action( 'init', 'my_pmpro_wp_profile_update_readonly_fields' ); | |
| /** | |
| * Add the readonly field JS to the frontend Member Profile Edit page. | |
| */ | |
| function my_pmpro_member_profile_edit_readonly_fields() { | |
| // Bail if PMPro Core not active. | |
| if ( ! defined( 'PMPRO_VERSION' ) ) { | |
| return; | |
| } | |
| // We're on the front end profile edit page. | |
| global $pmpro_pages; | |
| if ( is_page( $pmpro_pages['member_profile_edit'] ) ) { | |
| add_action( 'wp_footer', 'my_pmpro_member_read_only_fields_js' ); | |
| } | |
| } | |
| add_action( 'wp', 'my_pmpro_member_profile_edit_readonly_fields' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment