Title: Echo checkbox input field
Last modified: August 31, 2016

---

# Echo checkbox input field

 *  Resolved [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/echo-checkbox-input-field/)
 * Hi,
 * Rookie question, but I’m having trouble echoing a checkbox field in php syntax:
 *     ```
       <input name="my_field" type="checkbox" id="my_field" value="yes" <?php checked('yes', $value ); ?> />
       ```
   
 * Obviously this doesn’t work:
 *     ```
       echo ' <input name="my_field" type="checkbox" id="my_field" value="yes" <?php checked('yes', $value ); ?> /> ';
       ```
   
 * Who can help me out.. or maybe I just need some sleep.. 😉
 * Guido

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/echo-checkbox-input-field/#post-7056515)
 * Normalyl I’d do it like this…
 * `<input name="my_field" type="checkbox" id="my_field" value="yes"<?php if ($value
   == 'yes') echo ' checked'; ?> />`
 *  [Wooninjas](https://wordpress.org/support/users/wooninjas/)
 * (@wooninjas)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/echo-checkbox-input-field/#post-7056547)
 * You need to escape the quotes within the `echo` and your syntax is not valid
 *     ```
       <?php echo '<input name="my_field" type="checkbox" id="my_field" value="yes" checked(\'yes\', '. $value .' ) /> '; ?>
       ```
   
 * also make your output safe by using `htmlspecialchars`. i.e
 *     ```
       <?php echo '<input name="my_field"
       type="checkbox" id="my_field" value="yes" checked(\'yes\', '.htmlspecialchars($value) .' ) /> '
       ; ?>
       ```
   
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/echo-checkbox-input-field/#post-7056552)
 * Thanks, will work with this.
 * [@wooninjas](https://wordpress.org/support/users/wooninjas/): I don’t need to
   escape it there too because I do it 1 step earlier:
 *     ```
       $value = esc_attr( get_option( 'my_field' ) );
       ```
   
 * Guido
 *  [Wooninjas](https://wordpress.org/support/users/wooninjas/)
 * (@wooninjas)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/echo-checkbox-input-field/#post-7056553)
 * Ok. [@guido](https://wordpress.org/support/users/guido/) in that case you don’t
   need `htmlspecialchars`

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Echo checkbox input field’ is closed to new replies.

## Tags

 * [input](https://wordpress.org/support/topic-tag/input/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 3 participants
 * Last reply from: [Wooninjas](https://wordpress.org/support/users/wooninjas/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/echo-checkbox-input-field/#post-7056553)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
