wp_readonly()WP 5.9.0

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

0

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

#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' />
0

#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() code WP 6.5.2

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