简体   繁体   中英

Checkbox `checked()` function echo's checked='checked'

I'm writing a WordPress plugin and have a checkbox in a form.

If the checkbox is checked it saves the value to the database and shows up checked in the form. However if the checkbox is checked it outputs checked='checked' in the form.

So the checkbox works like it needs to work but I cant see why it outputs checked='checked' to the form

public function display() {

    $html = '';
    // Add an nonce field so we can check for it later.
    wp_nonce_field( basename( __FILE__ ), 'nonce_check_value' );
    $html .= '<label for="CMBUserBoxName">Name metabox: </label>';
    $html .= '<input type="text" name="CMBUserBoxName" value="' . get_post_meta( get_the_ID(), 'CMBUserBoxName', true ). '">';
    $html .= '<h1>What do you need?</h1>';
    $html .= '<label for="CMBUserCheckbox">Checkbox: </label>';
    $checkedByUser = get_post_meta( get_the_ID(), 'CMBUserCheckbox', true );
    $html .= '<input type="checkbox" name="CMBUserCheckbox" value="1" '.checked( $checkedByUser, 1 ).' />';

    echo $html;
}

The output on screen is as followed
在此处输入图片说明

The checked() function echo s by default. Use false in the last parameter to return the string instead of echo.

$html .= '<input type="checkbox" name="CMBUserCheckbox" value="1" '.checked( $checkedByUser, 1, false).' />';

使用wordpress Checked()函数,您可以执行此操作

<input checked class="classname" value="checkvalue" id="" type="radio" name="" <?php echo checked( get_option('get_check_box_value'), 'checkvalue') ?>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM