Created
March 9, 2012 20:34
-
-
Save FernE97/2008538 to your computer and use it in GitHub Desktop.
PHP: WordPress Custom Meta Box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// custom meta box | |
add_action( 'admin_menu', 'create_meta_box' ); | |
add_action( 'save_post', 'save_meta_box' ); | |
function create_meta_box() { | |
// add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); | |
add_meta_box( 'new_meta_box', 'Meta Box Title', 'new_meta_box', 'post_type', 'side', 'low' ); | |
} | |
function new_meta_box() { | |
global $post; | |
$custom_fields = get_post_custom( $post->ID ); | |
$input = $custom_fields['input'][0]; | |
$textarea = $custom_fields['textarea'][0]; | |
?> | |
<p><strong>Input</strong></p> | |
<p> | |
<label class="screen-reader-text" for="input">Input</label> | |
<input style="width:97%;" id="input" name="input" value="<?php echo $input; ?>" /> | |
</p> | |
<p><strong>Text Area</strong></p> | |
<p> | |
<label class="screen-reader-text" for="textarea">Text Area</label> | |
<textarea style="width:100%;" id="textarea" name="textarea" rows="6"><?php echo $textarea; ?></textarea> | |
</p> | |
<?php | |
} | |
function save_meta_box($post_id) { | |
global $post; | |
if ( ! isset( $_POST['input'] ) ) return; | |
if ( ! ( get_post_type( $post_id ) == 'post_type' ) ) return; | |
update_post_meta( $post_id, 'input', $_POST['input'] ); | |
update_post_meta( $post_id, 'textarea', $_POST['textarea'] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment