1

This is code generated here and I made a few very small adjustments for customization. The problem I'm having is that I can't get the default value for the checkbox to be checked. If I manually add checked="checked", it always loads checked even if it's saved as checked off.

I need help defining a default value of checked="checked" unless it has been checked off, then it should remain that way.

/**
* Generated by the WordPress Meta Box generator
* at http://jeremyhixon.com/tool/wordpress-meta-box-generator/
*/

function display_sharing_buttons_get_meta( $value ) {
    global $post;
    $field = get_post_meta( $post->ID, $value, true );
    if ( ! empty( $field ) ) {
        return is_array( $field ) ? stripslashes_deep( $field ) : 
            stripslashes( wp_kses_decode_entities( $field ) );
    } else {
        return false;
    }
}

function display_sharing_buttons_add_meta_box() {
    add_meta_box(
        'display_sharing_buttons-display-sharing-buttons',
        __( 'Display UST Sharing Buttons', 'display_sharing_buttons' ),
        'display_sharing_buttons_html',
        'post',
        'side',
        'default'
    );
    add_meta_box(
        'display_sharing_buttons-display-sharing-buttons',
        __( 'Display UST Sharing Buttons', 'display_sharing_buttons' ),
        'display_sharing_buttons_html',
        'page',
        'side',
        'default'
    );
}
add_action( 'add_meta_boxes', 'display_sharing_buttons_add_meta_box' );

function display_sharing_buttons_html( $post) {
    wp_nonce_field( '_display_sharing_buttons_nonce', 'display_sharing_buttons_nonce' ); ?>
    <p>
        <input type="checkbox" name="display_sharing_buttons_display" id="display_sharing_buttons_display" value="display" <?php 
            echo ( display_sharing_buttons_get_meta( 'display_sharing_buttons_display' ) === 'display' ) ? 'checked' : ''; ?>>
        <label for="display_sharing_buttons_display"><?php _e( 'Display', 'display_sharing_buttons' ); ?><img src="<?php 
            echo get_template_directory_uri() ?>/images/sharing-button-preview.png"></label>    
    </p>
<?php
}

function display_sharing_buttons_save( $post_id ) { 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( ! isset( $_POST['display_sharing_buttons_nonce'] ) || 
        ! wp_verify_nonce( $_POST['display_sharing_buttons_nonce'], '_display_sharing_buttons_nonce' ) ) return;
    if ( ! current_user_can( 'edit_post', $post_id ) ) return;

    if ( isset( $_POST['display_sharing_buttons_display'] ) )
        update_post_meta( $post_id, 'display_sharing_buttons_display', esc_attr( $_POST['display_sharing_buttons_display'] ) );
    else
        update_post_meta( $post_id, 'display_sharing_buttons_display', null );
}
add_action( 'save_post', 'display_sharing_buttons_save' );

/*
Usage: display_sharing_buttons_get_meta( 'display_sharing_buttons_display' )
*/
2

Try

metadata_exists( 'post', $post->ID, 'display_sharing_buttons_save' )

This functions determine if meta-key exists and return true even with NULL value. So in your case, if it returns FALSE you can show CHECKED by default.

Source

2

WordPress has a core function checked() you can use for handling the output.

https://codex.wordpress.org/Function_Reference/checked

Basically it checks the first to arguments and if they match (second arg is true by default), it echos out the checked="checked" HTML.

checked( $a_true_value );

That would output checked="checked" as long as $a_true_value is TRUE ... if you specify the second argument it checks if it matches the first.

1

I regenerated the code based on what I saw in the code you shared and it's working on my local machine:

/**
 * Generated by the WordPress Meta Box generator
 * at http://jeremyhixon.com/tool/wordpress-meta-box-generator/
 */

function display_ust_sharing_buttons_get_meta( $value ) {
    global $post;

    $field = get_post_meta( $post->ID, $value, true );
    if ( ! empty( $field ) ) {
        return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
    } else {
        return false;
    }
}

function display_ust_sharing_buttons_add_meta_box() {
    add_meta_box(
        'display_ust_sharing_buttons-display-ust-sharing-buttons',
        __( 'Display UST Sharing Buttons', 'display_ust_sharing_buttons' ),
        'display_ust_sharing_buttons_html',
        'post',
        'side',
        'default'
    );
    add_meta_box(
        'display_ust_sharing_buttons-display-ust-sharing-buttons',
        __( 'Display UST Sharing Buttons', 'display_ust_sharing_buttons' ),
        'display_ust_sharing_buttons_html',
        'page',
        'side',
        'default'
    );
}
add_action( 'add_meta_boxes', 'display_ust_sharing_buttons_add_meta_box' );

function display_ust_sharing_buttons_html( $post) {
    wp_nonce_field( '_display_ust_sharing_buttons_nonce', 'display_ust_sharing_buttons_nonce' ); ?>

    <p>

        <input type="checkbox" name="display_ust_sharing_buttons_display" id="display_ust_sharing_buttons_display" value="display" <?php echo ( display_ust_sharing_buttons_get_meta( 'display_ust_sharing_buttons_display' ) === 'display' ) ? 'checked' : ''; ?>>
        <label for="display_ust_sharing_buttons_display"><?php _e( 'Display', 'display_ust_sharing_buttons' ); ?> <img src="<?php 
            echo get_template_directory_uri() ?>/images/sharing-button-preview.png"></label>    </p><?php
}

function display_ust_sharing_buttons_save( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( ! isset( $_POST['display_ust_sharing_buttons_nonce'] ) || ! wp_verify_nonce( $_POST['display_ust_sharing_buttons_nonce'], '_display_ust_sharing_buttons_nonce' ) ) return;
    if ( ! current_user_can( 'edit_post', $post_id ) ) return;

    if ( isset( $_POST['display_ust_sharing_buttons_display'] ) )
        update_post_meta( $post_id, 'display_ust_sharing_buttons_display', esc_attr( $_POST['display_ust_sharing_buttons_display'] ) );
    else
        update_post_meta( $post_id, 'display_ust_sharing_buttons_display', null );
}
add_action( 'save_post', 'display_ust_sharing_buttons_save' );

/*
    Usage: display_ust_sharing_buttons_get_meta( 'display_ust_sharing_buttons_display' )
*/
  • Can you elaborate on what you changed? – jgraup Dec 15 '15 at 19:40
  • I didn't personally change anything. I read through the code provided and used the names of the meta box and field provided to fill in the generator's form and re-generated it. The only thing I did myself was copy/paste the image used after the label from the submitted code to the same place in the newly-generated code. – Jeremy Dec 16 '15 at 13:52
0

The generated code seems pointlessly complicated to me. (Maybe someone can explain why it's necessary or recommendable in this instance, or in other ones.)

Not vouching for the rest of the code, of course, but you could try the following (getting rid of the display_sharing_buttons_get_meta_() function):

function display_sharing_buttons_html( $post) {

    wp_nonce_field( '_display_sharing_buttons_nonce', 'display_sharing_buttons_nonce' ); ?>

    //get meta value   
    $display = get_post_meta( $post->ID , 'display_sharing_buttons_display', true );

    <p>

    /*use checked() function to test meta value
    breaking it up to make usage easier to see */
    <input type="checkbox" name="display_sharing_buttons_display" id="display_sharing_buttons_display" value="display" 
    <?php checked( $display, 'display' ); ?> 
    >

        <label for="display_sharing_buttons_display">

            <?php _e( 'Display', 'display_sharing_buttons' ); ?><img src="<?php echo get_template_directory_uri() ?>/images/sharing-button-preview.png">

       </label>    

   </p>

<?php 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.