1

My goal is this:

I've added an additional metabox to my post page. Within this metabox there are multiple checkboxes. Each checkbox is associated with a person's name. The idea is that, if a person is featured in the post, I'll check the box associated with their name. For each checked box, an image of each person will appear in a pre-determined div in the single.php. As more checkboxes are checked, the images are still contained within this div but become thinner with the help of the css flexbox.

Metabox

My problem is this:

As it stands now, the checkboxes do nothing. Even if I check them, then update the post, they do not remain checked. This is obviously an issue with my code. I also need a way to set an image for each person. Much like the way you'd set the featured image for a post, but for each person on this checkbox list. I then need each checked box to cause an echo of HTML. This HTML will be the same for each person, but will contain different images dependant on the boxes checked.

Takes a moment to catch breath...

I'm very sorry for this lengthy explanation, but this has been destroying my brain all week...

My code for the metabox is as follows. It was copied from a template that someone was kind enough to post. I don't necessarily understand it all, but sometimes I find if easier to learn if I'm given the solution, then work back and figure out how it all works.

// Featured People Meta

add_action( 'add_meta_boxes', 'add_custom_box' );

function add_custom_box( $post ) {

add_meta_box(
        'Meta Box', // ID, should be a string.
        'Featured People', // Meta Box Title.
        'people_meta_box', // Your call back function, this is where your form field will go.
        'post', // The post type you want this to show up on, can be post, page, or custom post type.
        'side', // The placement of your meta box, can be normal or side.
        'core' // The priority in which this will be displayed.
    );

}

function people_meta_box( $post ) {

// Get post meta value using the key from our save function in the second paramater.
$custom_meta = get_post_meta($post->ID, 'people-meta-box', true);
?>

<input type="checkbox" name="people-meta-box[]" value="Bob" <?php if(isset($_POST['people-meta-box']['bob'])) echo 'checked="checked"'; ?>  /> Bob <br>
<input type="checkbox" name="people-meta-box[]" value="Bill" <?php if(isset($_POST['people-meta-box']['bill'])) echo 'checked="checked"'; ?>  /> Bill <br>
<input type="checkbox" name="people-meta-box[]" value="Steve" <?php if(isset($_POST['people-meta-box']['steve'])) echo 'checked="checked"'; ?> /> Steve <br>

// Save

<?php   

}

add_action( 'save_post', 'save_people_meta_box' );

function save_people_meta_box(){

global $post;

// Get our form field
if(isset( $_POST['people-meta-box'] )) :

    $custom = $_POST['people-meta-box'];

     // Update post meta

     foreach($ids as $id){
      update_post_meta($id, 'people-meta-box', $custom[$id]);
     wp_set_object_terms( $id,  $custom[$id], 'people' );
     }

endif;

}

I'm VERY new to WordPress and PHP in general, so this could have been answered before, but I could have overlooked it as my understanding of this subject may not have been sufficient to apply the solutions given to others, to my work.

  • 1
    Perhaps this article may help you. – Mayeenul Islam Dec 4 '14 at 16:11
  • 1
    there are no POST values when you render your meta box in people_meta_box function, your ifs should be checking the value of the saved meta data. – Milo Dec 4 '14 at 16:38
  • @Milo Sorry, I don't understand what you mean. Are you saying there should be POST values, or there shouldn't? – Bryce Dec 5 '14 at 4:26
  • Have you tried the Advanced Custom Fields plugin? It is one of the most widely used (and free) plugins in the Wordpress repository and does exactly what you're after (and more) without having to do all that custom coding. Just a thought. – Eckstein May 2 '15 at 3:07
2

I was racking my brains out with this too and was finally able to find a solution that works. This should work for you, I've tested quickly. I'll present it in 3 parts:

PART 1: ADD THE CUSTOM META BOX

add_action( 'add_meta_boxes', 'add_custom_box' );

    function add_custom_box( $post ) {
        add_meta_box(
            'Meta Box', // ID, should be a string.
            'Featured People', // Meta Box Title.
            'people_meta_box', // Your call back function, this is where your form field will go.
            'post', // The post type you want this to show up on, can be post, page, or custom post type.
            'side', // The placement of your meta box, can be normal or side.
            'core' // The priority in which this will be displayed.
        );
}

PART 2: DEFINE THE "CALLBACK" FUNCTION (spits out the html for the checkboxes)

  function people_meta_box($post) {
    wp_nonce_field( 'my_awesome_nonce', 'awesome_nonce' );    
    $checkboxMeta = get_post_meta( $post->ID );
    ?>

    <input type="checkbox" name="bob" id="bob" value="yes" <?php if ( isset ( $checkboxMeta['bob'] ) ) checked( $checkboxMeta['bob'][0], 'yes' ); ?> />Bob<br />
    <input type="checkbox" name="bill" id="bill" value="yes" <?php if ( isset ( $checkboxMeta['bill'] ) ) checked( $checkboxMeta['bill'][0], 'yes' ); ?> />Bill<br />
    <input type="checkbox" name="steve" id="steve" value="yes" <?php if ( isset ( $checkboxMeta['steve'] ) ) checked( $checkboxMeta['steve'][0], 'yes' ); ?> />Steve<br />

<?php }

PART 3: SAVE THE CHECKBOX VALUES

add_action( 'save_post', 'save_people_checkboxes' );
    function save_people_checkboxes( $post_id ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
            return;
        if ( ( isset ( $_POST['my_awesome_nonce'] ) ) && ( ! wp_verify_nonce( $_POST['my_awesome_nonce'], plugin_basename( __FILE__ ) ) ) )
            return;
        if ( ( isset ( $_POST['post_type'] ) ) && ( 'page' == $_POST['post_type'] )  ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return;
            }    
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return;
            }
        }

        //saves bob's value
        if( isset( $_POST[ 'bob' ] ) ) {
            update_post_meta( $post_id, 'bob', 'yes' );
        } else {
            update_post_meta( $post_id, 'bob', 'no' );
        }

        //saves bill's value
        if( isset( $_POST[ 'bill' ] ) ) {
            update_post_meta( $post_id, 'bill', 'yes' );
        } else {
            update_post_meta( $post_id, 'bill', 'no' );
        }

        //saves steve's value
        if( isset( $_POST[ 'steve' ] ) ) {
            update_post_meta( $post_id, 'steve', 'yes' );
        } else {
            update_post_meta( $post_id, 'steve', 'no' );
        }  
}

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.