Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Last active February 20, 2023 13:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save justintadlock/7346860 to your computer and use it in GitHub Desktop.
Save justintadlock/7346860 to your computer and use it in GitHub Desktop.
Saving multiple meta values for a single key with a meta box.
<?php
/*
Plugin Name: Fruity Fruit
Description: An example plugin to test saving multiple meta values.
*/
add_action( 'add_meta_boxes_post', 'fruity_fruit_add_meta_boxes' );
add_action( 'save_post', 'fruity_fruit_save_meta', 10, 2 );
function fruity_fruit_add_meta_boxes() {
add_meta_box(
'fruity-fruit',
__( 'Fruity Fruit', 'fruity-fruit' ),
'fruity_fruit_meta_box',
'post',
'side',
'core'
);
}
function fruity_fruit_meta_box( $object, $box ) {
$fruits = get_post_meta( $object->ID, '_fruity_fruit' );
foreach ( $fruits as $fruit ) echo $fruit;
wp_nonce_field( basename( __FILE__ ), 'fruity-fruit-nonce' ); ?>
<ul>
<li>
<label>
<input type="checkbox" name="fruity-fruit[]" value="apple" <?php checked( in_array( 'apple', $fruits ), true ); ?> />
<?php _e( 'Apple', 'example' ); ?>
</label>
</li>
<li>
<label>
<input type="checkbox" name="fruity-fruit[]" value="banana" <?php checked( in_array( 'banana', $fruits ), true ); ?> />
<?php _e( 'Banana', 'example' ); ?>
</label>
</li>
<li>
<label>
<input type="checkbox" name="fruity-fruit[]" value="kiwi" <?php checked( in_array( 'kiwi', $fruits ), true ); ?> />
<?php _e( 'Kiwi', 'example' ); ?>
</label>
</li>
<li>
<label>
<input type="checkbox" name="fruity-fruit[]" value="orange" <?php checked( in_array( 'orange', $fruits ), true ); ?> />
<?php _e( 'Orange', 'example' ); ?>
</label>
</li>
<li>
<label>
<input type="checkbox" name="fruity-fruit[]" value="watermelon" <?php checked( in_array( 'watermelon', $fruits ), true ); ?> />
<?php _e( 'Watermelon', 'example' ); ?>
</label>
</li>
</ul>
<?php }
function fruity_fruit_save_meta( $post_id, $post = '' ) {
if ( !is_object( $post ) )
$post = get_post();
if ( !isset( $_POST['fruity-fruit-nonce'] ) || !wp_verify_nonce( $_POST['fruity-fruit-nonce'], basename( __FILE__ ) ) )
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
if ( defined( 'DOING_CRON' ) && DOING_CRON )
return;
if ( 'revision' === $post->post_type )
return;
$fruit_options = array( 'apple', 'banana', 'kiwi', 'orange', 'watermelon' );
$meta_values = get_post_meta( $post_id, '_fruity_fruit' );
if ( isset( $_POST['fruity-fruit'] ) && is_array( $_POST['fruity-fruit'] ) ) {
foreach ( $_POST['fruity-fruit'] as $new_fruit ) {
if ( !in_array( $new_fruit, $meta_values ) )
add_post_meta( $post_id, '_fruity_fruit', $new_fruit, false );
}
foreach ( $fruit_options as $old_fruit ) {
if ( !in_array( $old_fruit, $_POST['fruity-fruit'] ) && in_array( $old_fruit, $meta_values ) )
delete_post_meta( $post_id, '_fruity_fruit', $old_fruit );
}
}
elseif ( !empty( $meta_values ) ) {
delete_post_meta( $post_id, '_fruity_fruit' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment