Password
4 min read
The Password field stores a secret that is never shown again. The edit screen always shows an empty box. Use it for things such as a partner access code, a download password or an API secret that your own code verifies or uses.
Type id |
|
Default column |
|
Returns | The stored sealed value (a hash, an encrypted string or plain text), not a password you can print |
Prints | Nothing, unless you explicitly allow it |
Type settings
Setting | Default | Effect |
|---|---|---|
Store as | Hash (recommended) | How a new value is saved: Hash (recommended), Encrypted or Plain text. Changing it applies to values saved afterwards; existing values keep their format and still verify. |
Ask for the password twice in front-end forms | Off | In a front-end form (Pro) shows a second box, Repeat the password, and refuses the value if the two differ. Has no effect on the admin edit screen. |
The three storage modes
Mode | What is in the database | Can you read it back? | Use when |
|---|---|---|---|
Hash (recommended) | A WordPress password hash such as | No. You can only check a password against it. | Visitors type a password and you compare it (a protected area, a download code). |
Encrypted |
| Yes, with | Your own code must use the secret (for example to call an API). |
Plain text | The text as typed | Yes, and | Only when the database is the sole place the value matters. Avoid this. |
Encrypted values use a key derived from your site's SECURE_AUTH_SALT. If you change the salts in wp-config.php, encrypted values can no longer be decrypted; they must be entered again. Hashes are not affected.
Rules when saving
- A password can be at most 128 bytes long. Longer values are refused.
- Min length and Max length (Validation tab) are checked on the server for this type and the value is refused, not cut. The editor sees "Use at least 6 characters." or "Use at most 10 characters.".
- The text is stored exactly as typed. It is not trimmed or stripped of tags. Only invalid characters and NUL bytes are removed.
- An empty box keeps the stored password. To remove it, tick Remove the stored password (shown only when a password is stored and the field is not required).
- A required field is satisfied by a stored password; the editor does not have to retype it.
- A value that is already a hash or an encrypted string (for example copied from an export) is stored as it is and never hashed twice.
Reading and verifying in code
// Is the typed password right? Works for hashed, encrypted and plain fields.
if ( wpmcf_check_password( 'partner', 'access_code', $_POST['code'] ?? '', $post_id ) ) {
// allow access
}
// Read an encrypted or plain value (returns null for a hashed value or an empty field).
$secret = wpmcf_get_password( 'integration', 'api_secret', $post_id );
// Set it from code. A plain string is sealed with the field's storage mode.
wpmcf_update_field( 'partner', 'access_code', 'S3cret!pass', $post_id );
wpmcf_update_field( 'partner', 'access_code', '', $post_id ); // keeps the current password
wpmcf_update_field( 'partner', 'access_code', null, $post_id ); // removes itwpmcf_update_field() returns false when the password is refused (too long, or outside Min length and Max length). It does not check capabilities: check permissions yourself before passing user input to it.
wpmcf_get_field() on a password field returns the sealed value as stored:
Mode |
|
|---|---|
Hash |
|
Encrypted |
|
Plain text |
|
Never print this. wpmcf_check_password() and wpmcf_get_password() are the safe ways to use it. Legacy columns that hold an unsalted md5 or sha1 of the password also verify with wpmcf_check_password().
Printing
wpmcf_the_field() and [wpmcf_field] print nothing for a password, in every format. Passwords are also left out of the REST API values of a post and of {{ fg.… }} variables.
To print a readable (encrypted or plain) password anyway, enable it with a filter. Do this only when the page is private.
add_filter( 'wpmcf/field/the_password', '__return_true' );A hashed password can never be printed, with or without the filter.
Where it works
- Edit screens of posts, users, terms, comments, widgets and options pages: yes. The input uses
autocomplete="new-password"so browsers do not fill it with the user's saved login. - Front-end forms (Pro): yes, with the optional confirmation box.
- As a sub-field of a Group or Repeater: no. Those store all their sub-fields as one JSON value, which cannot keep a password that was not retyped. Put the Password field at the top level of the group.
- SQL column functions: none (a password hashes itself).
Troubleshooting
- I need to show the password to the editor. You cannot; use Encrypted and show it in your own admin screen with
wpmcf_get_password(), or let the editor set a new one. wpmcf_get_password()returnsnull. The field is empty, stored as a Hash, or the encrypted value cannot be decrypted (changed salts).- The shortcode prints nothing. That is by design; see "Printing".
- "This password is too long." Passwords are limited to 128 bytes.
Still stuck? Contact support