wp_readonly()WP 5.9.0

Outputs the HTML readonly attribute.

Compares the first two arguments and if identical marks as readonly.

No Hooks.


String. HTML attribute or empty string.


wp_readonly( $readonly_value, $current, $display );
$readonly_value(mixed) (required)
One of the values to compare.
The other value to compare if not just true.
Default: true
Whether to echo or just return the string.
Default: true



#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:

// some logic ...
$is_readonly = true;

<form ...>
	<input type="text" value="Some string" <?php wp_readonly( $is_readonly ) ?> />

#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'"


Since 5.9.0 Introduced.

wp_readonly() code WP 6.5.2

function wp_readonly( $readonly_value, $current = true, $display = true ) {
	return __checked_selected_helper( $readonly_value, $current, $display, 'readonly' );