Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 11 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:


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 12 years ago.
patch for checked function
checked-test.php (3.4 KB) - added by sudar 12 years ago.
Sample Plugin to demonstrate checked array function
10556.patch (747 bytes) - added by hakre 12 years ago.

Download all attachments as: .zip

Change History (16)

12 years ago

patch for checked function

#1 @westi
12 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.

#2 @sudar
12 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)

12 years ago

Sample Plugin to demonstrate checked array function

#3 @Denis-de-Bernardy
12 years ago

  • Milestone changed from Unassigned to 3.0

#4 @Denis-de-Bernardy
12 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()

12 years ago

#5 @hakre
12 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).

#6 @hakre
12 years ago

  • Keywords has-patch added

#7 @Denis-de-Bernardy
12 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.

#8 @Denis-de-Bernardy
12 years ago

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

#9 @shidouhikari
12 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.

#10 @nacin
12 years ago

in_array() has a strict parameter.

#11 @nacin
11 years ago

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

#12 @westi
11 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.

#13 @nacin
11 years ago


Note: See TracTickets for help on using tickets.