Just wondering how to make the caption on my metabox gallery images updatable. I have the caption on a textarea so it can be easily updated. I can change the contents of this textarea, cant seem to be able to update it on save post though.
function mytheme_show_post_gallery_metabox( $post ) {
wp_nonce_field( 'mytheme_post_gallery_metabox', '_mytheme_post_gallery_metabox' );
$gallery = get_post_meta( $post->ID, '_mytheme_post_gallery', true );
?>
<a class="gallery-add button media-button button-primary button-large media-button-select" href="#" data-uploader-title="<?php _e( 'Add gallery images', 'mytheme' ); ?>" data-uploader-button-text="<?php _e( 'Add gallery images', 'mytheme' ); ?>"><?php _e( 'Add gallery images', 'mytheme' ); ?></a>
<table>
<tr>
<td>
<ul id="gallery-metabox-list">
<?php if ( $gallery ) : ?>
<?php foreach ( $gallery as $key => $value ) : $image = wp_get_attachment_image_src( $value ); ?>
<li>
<input type="hidden" name="_mytheme_post_gallery[<?php echo $key; ?>]" value="<?php echo $value; ?>">
<img class="image-preview" src="<?php echo $image[0]; ?>">
<a class="change-image" href="#"><?php _e( 'Change | ', 'mytheme' ); ?></a>
<a class="remove-image" href="#"><?php _e( 'Remove', 'mytheme' ); ?></a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</td>
</tr>
</table>
<?php
}
The following code updates the image. How can I tweak it to also update the caption when the post is saved/updated?
function mytheme_save_image_gallery_metabox( $post_id ) {
if ( !isset($_POST['_mytheme_post_gallery'] ) || !wp_verify_nonce( $_POST['_mytheme_post_gallery_metabox'], 'mytheme_post_gallery_metabox' ) ) return;
if ( !current_user_can('edit_post', $post_id ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( isset( $_POST['_mytheme_post_gallery'] ) ) {
update_post_meta( $post_id, '_mytheme_post_gallery', wp_kses( $_POST['_mytheme_post_gallery'] ,'') );
} else {
delete_post_meta( $post_id, '_mytheme_post_gallery' );
}
}
add_action( 'save_post', 'mytheme_save_image_gallery_metabox' );