0

Can't get my checkbox for metafield set true by default.

Everything is working fine, except default value.

I know there is a lot of questions and answers for it,

But I tried everything and still can't get it.

Here is my code:

function theme_add_meta_box() {
    add_meta_box( 'theme_post_sidebar_option', esc_html__( 'Additional Options', 'theme' ), 'theme_post_sidebar_settings', 'post', 'normal', 'high' );
}

add_action( 'add_meta_boxes', 'theme_add_meta_box' );

Then my options:

function theme_post_sidebar_settings($post) {

    $sidebar = get_post_meta($post->ID, '_theme_post_meta_sidebar', true);
    wp_nonce_field( 'theme_update_post_sidebar_settings', 'theme_update_post_sidebar_nonce' );
    ?>

    <input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="1" <?php checked($sidebar); ?> />
    <label for="theme_post_meta_sidebar_field"><?php esc_html_e( 'Sidebar', 'theme' ); ?></label>

    <?php
}

Then my save function:

    function theme_save_post_sidebar_settings($post_id, $post) {

        $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
        if( !current_user_can( $edit_cap, $post_id )) {
            return;
        }
        if( !isset( $_POST['theme_update_post_sidebar_nonce']) || !wp_verify_nonce( $_POST['theme_update_post_sidebar_nonce'], 'theme_update_post_sidebar_settings' )) {
            return;
        }
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }

        if(array_key_exists('theme_post_meta_sidebar_field', $_POST)) {
            update_post_meta( 
                $post_id, 
                '_theme_post_meta_sidebar', 
                sanitize_text_field($_POST['theme_post_meta_sidebar_field'])
            );
        } else {
            update_post_meta( 
                $post_id, 
                '_theme_post_meta_sidebar', null);
        }
    }
    add_action( 'save_post', 'theme_save_post_sidebar_settings', 10, 2 );

Can someone help me, please?

3 Answers 3

0

To get checkbox by default true in metabox, You can use checked Attribute for checkbox. So i have made two changes in your code.

  • In option, I have added new condition, by default it will checked checkbox and if meta_value 1 , If updated value of meta_value is unchecked and not empty then it will uncheck checkbox.
  • In save Function, i have change value from null to unchecked.

So, by default checkbox will be checked, when you save with checked checkbox it will checked if you uncheck and save it will unchecked.

options function:

function theme_post_sidebar_settings($post) {

    $sidebar = get_post_meta($post->ID, '_theme_post_meta_sidebar', true);
    wp_nonce_field( 'theme_update_post_sidebar_settings', 'theme_update_post_sidebar_nonce' );

    $checked = 'checked';
    if($sidebar == 'unchecked' && !empty($sidebar))
    {   
        $checked = ''; 
    }
    ?>
    <input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="1" <?php echo $checked;  ?> />
    <label for="theme_post_meta_sidebar_field"><?php esc_html_e( 'Sidebar', 'theme' ); ?></label>

    <?php
}

Save Function :

function theme_save_post_sidebar_settings($post_id, $post) {

        $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
        if( !current_user_can( $edit_cap, $post_id )) {
            return;
        }
        if( !isset( $_POST['theme_update_post_sidebar_nonce']) || !wp_verify_nonce( $_POST['theme_update_post_sidebar_nonce'], 'theme_update_post_sidebar_settings' )) {
            return;
        }
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }

        if(array_key_exists('theme_post_meta_sidebar_field', $_POST)) {
            update_post_meta( 
                $post_id, 
                '_theme_post_meta_sidebar', 
                sanitize_text_field($_POST['theme_post_meta_sidebar_field'])
            );
        } else {
            update_post_meta( 
                $post_id, 
                '_theme_post_meta_sidebar', 'unchecked');
        }
    }
    add_action( 'save_post', 'theme_save_post_sidebar_settings', 10, 2 );
1
  • 1
    Thanks, mate! Now it's working perfect!
    – Aslan
    Commented Mar 13, 2020 at 12:34
0

Can't leave a comment so I'll leave what you may find to be the answer.

What's this?

<?php checked($sidebar); ?>

Is that a function that's returning either 'checked' or nothing?

Because this shows your checkbox and it's checked by default:

<input type="checkbox" name="theme_post_meta_sidebar_field" 
id="theme_post_meta_sidebar_field" value="1" checked>

I THINK this will work.

$sidebar = get_post_meta($post->ID, '_theme_post_meta_sidebar', true);
if ($sidebar) {
        $checkboxchecked = 'checked';
    } else { 
        $checkboxchecked = null;     
    }
wp_nonce_field( 'theme_update_post_sidebar_settings', 'theme_update_post_sidebar_nonce' );
?>

<input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="1" <?php $checkboxchecked; ?>/>

In order to get a checkbox to be checked by default you have to put either 'checked' or 'checked="checked"' at the end of the input tag. I'm only guessing that your code was not doing that.

https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_checked

5
  • I'm sorry, but I can't understand what do you mean. What should I do? @uPrompt
    – Aslan
    Commented Mar 12, 2020 at 21:20
  • Answer updated. Not sure if that will work but I think it will
    – uPrompt
    Commented Mar 12, 2020 at 21:48
  • now I understand what you mean, checked() is making exactly the same function as your condition, sorry that didn't get the point. So it's not working, because my $sidebar value is empty string, that's why checkbox by default isn't true. @uPrompt
    – Aslan
    Commented Mar 12, 2020 at 22:48
  • Glad you got it figured out.
    – uPrompt
    Commented Mar 13, 2020 at 0:21
  • sorry for my English, it still doesn't work properly
    – Aslan
    Commented Mar 13, 2020 at 7:01
0

You Can add This below Snippet code for :

change your input value 'true' and add after <?php echo $sidebar?'checked': 'unchecked'; ?> in input element

function theme_post_sidebar_settings($post) {

    $sidebar = get_post_meta($post->ID, '_theme_post_meta_sidebar', true);
    wp_nonce_field( 'theme_update_post_sidebar_settings', 'theme_update_post_sidebar_nonce' );
    ?>

    <input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="true" <?php  echo $sidebar?'checked': 'unchecked'; ?> />
    <label for="theme_post_meta_sidebar_field"><?php esc_html_e( 'Sidebar', 'theme' ); ?></label>

    <?php
}

Change this below function

function theme_save_post_sidebar_settings($post_id, $post) {

        $edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
        if( !current_user_can( $edit_cap, $post_id )) {
            return;
        }
        if( !isset( $_POST['theme_update_post_sidebar_nonce']) || !wp_verify_nonce( $_POST['theme_update_post_sidebar_nonce'], 'theme_update_post_sidebar_settings' )) {
            return;
        }
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }

            update_post_meta( 
                $post_id, 
                '_theme_post_meta_sidebar', 
                sanitize_text_field($_POST['theme_post_meta_sidebar_field'])
            );

    }
    add_action( 'save_post', 'theme_save_post_sidebar_settings', 10, 2 );
5
  • Thanks, but the same result that I have.
    – Aslan
    Commented Mar 13, 2020 at 7:40
  • You can check your post meta is update
    – HK89
    Commented Mar 13, 2020 at 7:59
  • the problem that $sidebar is by default false, so it will be unchecked.
    – Aslan
    Commented Mar 13, 2020 at 8:06
  • Add new above change code try it
    – HK89
    Commented Mar 13, 2020 at 8:10
  • the same result
    – Aslan
    Commented Mar 13, 2020 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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