WordPress at a glance

disabled() WP 3.0.0

Compares two given values, and if they are the same, displays disabled = 'disabled' – an attribute for a form input field.

The function is usually used inside the input and textarea fields of the HTML form.

This is one of the four helper functions for forms: checked(), disabled(), selected(), readonly().

No Hooks.

Return

null/string.

  • Echoes disabled='disabled' if the given values match.
  • If the third parameter is false (default is true) then a string disabled='disabled' or an empty string will be returned.

Usage

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

Examples

#1 Demonstration of the usage

Let's say we have a group of radio form fields. One of the fields is already involved in the options and, therefore, this field cannot be selected. In this case, we can add the disabled attribute to this field. This example demonstrates how to do it conveniently with this function.

Suppose the value of the option field is "red".

<input type="radio" name="option" value="red" <?php disabled( $_POST['option'], 'red' ) ?> > Red
<input type="radio" name="option" value="green" <?php disabled( $_POST['option'], 'green' ) ?> > Green
<input type="radio" name="option" value="blue" <?php disabled( $_POST['option'], 'blue' ) ?> > Blue

Result

<input type="radio" name="option" value="red" disabled='disabled' > Red
<input type="radio" name="option" value="green"> Green
<input type="radio" name="option" value="blue"> Blue

Changelog

Since 3.0.0 Introduced.

Code of disabled: wp-includes/general-template.php WP 5.2.2

<?php
function disabled( $disabled, $current = true, $echo = true ) {
	return __checked_selected_helper( $disabled, $current, $echo, 'disabled' );
}

Related Functions

From tag: form functions

More from tag: Supporting (helper utility auxiliary)

No comments
    Hello, !     Log In . Register