wp_readonly()
Outputs the HTML readonly attribute.
Compares the first two arguments and if identical marks as readonly.
No Hooks.
Return
String
. HTML attribute or empty string.
Usage
wp_readonly( $readonly_value, $current, $display );
- $readonly_value(mixed) (required)
- One of the values to compare.
- $current(mixed)
- The other value to compare if not just true.
Default: true - $display(true|false)
- Whether to echo or just return the string.
Default: true
Examples
#1 Demo of use
Suppose we have code that defines the $is_readonly variable and if it is true, we need to display the readonly attribute for the form field:
<?php // some logic ... $is_readonly = true; ?> <form ...> ... <input type="text" value="Some string" <?php wp_readonly( $is_readonly ) ?> /> ... </form>
#2 Add the readonly attribute
Suppose you want to add a readonly value to the field if the current user can't edit post 25:
<input type='text' <?php readonly( current_user_can('edit_post', 25 ) ?> name='myname' value='Value' />
#3 More examples of how the function works
$is_readonly = true; wp_readonly( $is_readonly ); // print readonly='readonly'
$is_readonly = false; wp_readonly( $is_readonly ); // it won't print anything
$readonly = wp_readonly( '1', true, false ); var_dump( $readonly ); // string(20) " readonly='readonly'"
Changelog
Since 5.9.0 | Introduced. |
wp_readonly() wp readonly code WP 6.7.1
function wp_readonly( $readonly_value, $current = true, $display = true ) { return __checked_selected_helper( $readonly_value, $current, $display, 'readonly' ); }