WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#10556 closed enhancement (wontfix)

Add a checked_array() to compare arrays like checked()

Reported by: sudar Owned by: nacin
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: Template Keywords: needs-patch
Focuses: Cc:

Description

The checked() function checks whether two parameters passed to it are equal and then outputs checked='checked', which can be used in forms having checkboxes or radio buttons.

Sometimes (particularly for checkboxes), we might need to check for multiple values. So I am proposing a change to the checked() function.

It can first check whether the default value is an array and if yes, it can return checked='checked' if the current value is present in the default array.

I have attached a patch with this change.

Attachments (3)

template.patch (863 bytes) - added by sudar 5 years ago.
patch for checked function
checked-test.php (3.4 KB) - added by sudar 5 years ago.
Sample Plugin to demonstrate checked array function
10556.patch (747 bytes) - added by hakre 4 years ago.

Download all attachments as: .zip

Change History (16)

sudar5 years ago

patch for checked function

comment:1 westi5 years ago

  • Keywords reporter-feedback added; checked template removed
  • Owner set to westi
  • Status changed from new to accepted

Could you give a more detailed example of what you are trying to do that the checked function doesn't support.

An example simple plugin which has an page with a check box on would be fine.

I would like to get a clear understanding of the issue that presents.

comment:2 sudar5 years ago

  • Cc sudar@… added

Here is a scenario which explains what I am try to do.

Let's say I am writing a Plugin, which is displaying all top level pages (as checkboxes) and the user will be selecting some of them. I will have the name of the checkboxes as an array (eg: selected_pages[]), so that I can easily handle the post variables when the form is submitted.

Now the next time, when the page is displayed, I have to keep the checkboxes that were already selected as checked. I will have the selected pages as an array, and instead of looping through every time to find out whether the current page is there in the array or not, I can pass the current id and the array to the checked function which can print checked='checked', if the current page id is present in the array.

I have attached a small Plugin to demonstrate this.

(sorry for the delay in reply, I didn't noticed the status change)

sudar5 years ago

Sample Plugin to demonstrate checked array function

comment:3 Denis-de-Bernardy4 years ago

  • Milestone changed from Unassigned to 3.0

comment:4 Denis-de-Bernardy4 years ago

  • Keywords reporter-feedback removed
  • Summary changed from Ability to compare arrays in checked() function to Add a checked_array() to compare arrays like checked()

hakre4 years ago

comment:5 hakre4 years ago

Updated the template patch according to the principles in the plugin. Original code had repeated parts I was able to remove. There is no need for an additional function, checked() and selected() accepted already arrays based on their definition (they converted them to strings which does not make sense).

comment:6 hakre4 years ago

  • Keywords has-patch added

comment:7 Denis-de-Bernardy4 years ago

  • Keywords needs-patch added; has-patch removed

I like neither patches, personally. On the one hand side, we're checking using the === operator, whereas on the other we're using in_array(), which amounts to using the == operator.

the === was introduced in order to distinguish an empty string from a numerical zero.

comment:8 Denis-de-Bernardy4 years ago

  • Milestone changed from 3.0 to 3.1
  • Version set to 2.9

comment:9 shidouhikari4 years ago

  • Cc shidouhikari added

So, we can´t use in_array() because of the operstor?

That's sad, I liked the 2nd patch.

hakre, I believe the (string) is meant for ints, and not for arrays.

comment:10 nacin4 years ago

in_array() has a strict parameter.

comment:11 nacin3 years ago

  • Milestone changed from Awaiting Triage to Future Release
  • Owner changed from westi to nacin

comment:12 westi3 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from accepted to closed

Until we need this in core this is WONTFIX IMHO - to much of an edge case.

comment:13 nacin3 years ago

Agreed.

Note: See TracTickets for help on using tickets.