Skip to content

Instantly share code, notes, and snippets.

@enishant
Created May 28, 2014 10:07
Show Gist options
  • Save enishant/19fdb9984cf3e4631dc7 to your computer and use it in GitHub Desktop.
Save enishant/19fdb9984cf3e4631dc7 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Meta Test
*/
new CustomMetaTest;
class CustomMetaTest
{
function __construct()
{
add_action( 'add_meta_boxes' , array($this , 'metaboxDefination') );
add_action( 'save_post', array($this , 'metaboxSave' ) );
add_shortcode( 'getmetaval' , array($this , 'get_meta_box_value') );
}
function metaboxDefination()
{
add_meta_box( 'my-meta-box-1', 'First Meta Box', array($this , 'meta_box_one' ), 'post', 'normal', 'high' );
add_meta_box( 'my-meta-box-2', 'Second Meta Box', array($this , 'meta_box_two' ), 'post', 'normal', 'high' );
}
function meta_box_one()
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
function meta_box_two()
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text2'] ) ? esc_attr( $values['my_meta_box_text2'][0] ) : '';
?>
<p>
<label for="my_meta_box_text2">Text Label</label>
<input type="text" name="my_meta_box_text2" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
function metaboxSave( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array(
'href' => array()
)
);
if( isset( $_POST['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
if( isset( $_POST['my_meta_box_text2'] ) )
update_post_meta( $post_id, 'my_meta_box_text2', wp_kses( $_POST['my_meta_box_text2'], $allowed ) );
}
function get_meta_box_value( $atts )
{
extract( shortcode_atts( array(
'key' => '',
), $atts ) );
$values = get_post_custom( $post->ID );
$metabox_value = isset( $values[$key] ) ? esc_attr( $values[$key][0] ) : '';
return $metabox_value;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment