Skip to content

Instantly share code, notes, and snippets.

@bravokeyl
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bravokeyl/c35b8b72c9cca8050412 to your computer and use it in GitHub Desktop.
Save bravokeyl/c35b8b72c9cca8050412 to your computer and use it in GitHub Desktop.
Post Meta Boxes Quick start
<?php
add_action( 'load-post.php', 'spi_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'spi_post_meta_boxes_setup' );
function spi_post_meta_boxes_setup() {
add_action( 'add_meta_boxes', 'spi_add_post_meta_boxes' );
add_action( 'save_post', 'spi_save_post_meta', 10, 2 );
}
function spi_add_post_meta_boxes() {
add_meta_box(
'hdsl-gallery', // Unique ID
esc_html__( 'Add gallery', 'hdsl' ), // Title
'hdsl_gallery_meta_box', // Callback function
'wg_deal', // Admin page (or post type)
'side', // Context
'default' // Priority
);
add_meta_box(
'hdsl-top-det', // Unique ID
esc_html__( 'Add Image on Top', 'hdsl' ), // Title
'hdsl_top_img_meta_box', // Callback function
'wg_deal', // Admin page (or post type)
'side', // Context
'default' // Priority
);
}
function hdsl_gallery_meta_box($object, $box) {
?>
<?php wp_nonce_field( basename( __FILE__ ), 'hdsl_gallery_nonce' ); ?>
<p>
<label for="hdsl-gallery"><?php _e( "Add ID's of images to the gallery.", 'hdsl' ); ?></label>
<br />
<?php
$field_value = get_post_meta( $object->ID, '_hdsl_gallery1', true );
$args = array (
'wpautop' => false,
'tinymce' => false,
'quicktags' => false,
'drag_drop_upload' => true,
'textarea_rows' => 1
);
wp_editor( $field_value, '_hdsl_gallery1' , $args ); ?>
</p>
<?php
}
function hdsl_top_img_meta_box($object, $box) {
?>
<?php wp_nonce_field( basename( __FILE__ ), 'hdsl_top_img_nonce' ); ?>
<p>
<label for="hdsl-top-img"><?php _e( "Add image to show up on top of deal details page.", 'hdsl' ); ?></label>
<br />
<?php
$field_value = get_post_meta( $object->ID, '_hdsl_top_img', true );
$args = array (
'wpautop' => true,
'tinymce' => false,
'quicktags' => false,
'drag_drop_upload' => true,
'textarea_rows' => 1
);
wp_editor( $field_value, '_hdsl_top_img' , $args ); ?>
</p>
<?php
}
/* Save the meta box's post metadata. */
function spi_save_post_meta( $post_id, $post ) {
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['hdsl_gallery_nonce'] ) || !wp_verify_nonce( $_POST['hdsl_gallery_nonce'], basename( __FILE__ ) ) )
return $post_id;
if ( !isset( $_POST['hdsl_top_img_nonce'] ) || !wp_verify_nonce( $_POST['hdsl_top_img_nonce'], basename( __FILE__ ) ) )
return $post_id;
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
$new_meta_value = ( isset( $_POST['_hdsl_gallery1'] ) ? $_POST['_hdsl_gallery1'] : '' );
$new_meta_value1 = ( isset( $_POST['_hdsl_top_img'] ) ? $_POST['_hdsl_top_img'] : '' );
/* Get the meta key. */
$meta_key = '_hdsl_gallery1';
$meta_key1 = '_hdsl_top_img';
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
$meta_value1 = get_post_meta( $post_id, $meta_key1, true );
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
if ( $new_meta_value1 && '' == $meta_value1 )
add_post_meta( $post_id, $meta_key1, $new_meta_value1, true );
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value1 && $new_meta_value1 != $meta_value1 )
update_post_meta( $post_id, $meta_key1, $new_meta_value1 );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( '' == $new_meta_value1 && $meta_value1 )
delete_post_meta( $post_id, $meta_key1, $meta_value1 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment