[resolved] checked ( $blah, $blah2 ) and arrays (4 posts)

  1. richarduk
    Posted 5 years ago #

    I want to echo checked = "checked"
    Since I'm writing a theme for submission to WP repository I need to use checked ( $blah, $blah2 )
    However, the theme uses an array of checkboxes
    I have been using

    if (in_array ($miscellaneous_checkbox_key, $miscellaneous_checkbox_checked))
    			echo ' checked="checked"';

    How do I do this?

  2. richarduk
    Posted 5 years ago #

    From http://codex.wordpress.org/Theme_Review

    < For checkboxes and select options, Themes are required to use the checked() and selected() functions for outputting checked="checked" and selected="selected", respectively. >

    I'm probably being daft, but how can I use that code - which echoes output, so I can't even do anything with it in php - when I have an array of checkboxes (as an example) and I have to go through that array to see if my conditions are being met? It looks to my simple mind as if that function only matches one specific condition to one specific condition.

  3. richarduk
    Posted 5 years ago #

    OK, checked the function, it only matches strings, so I've replaced my neat if ( array_key_exists () ) and if (in_array () ) with foreach loops.

    Stupid checked() function, can't see the point of it but if we have to have it perhaps it could check for the presence of a string in an array for multiple checkboxes.

  4. gfazioli
    Posted 4 years ago #

    Hi, I use:

    function bnm_checked($haystack, $current) {
    	if(is_array($haystack) && in_array($current, $haystack)) {
    		$current = $haystack = 1;
    	checked($haystack, $current);

Topic Closed

This topic has been closed to new replies.

About this Topic