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' )
*/