Skip to content

Instantly share code, notes, and snippets.

@leanda
Last active December 16, 2015 04:49
Show Gist options
  • Save leanda/5379772 to your computer and use it in GitHub Desktop.
Save leanda/5379772 to your computer and use it in GitHub Desktop.
WordPress: Conditional Text Area Meta Box
// custom meta box
add_action( 'admin_menu', 'create_meta_box' );
add_action( 'save_post', 'save_meta_box' );
function create_meta_box() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
// check for a template type
if ($template_file == 'homepage.php') {
// add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
add_meta_box( 'new_meta_box', 'Meta Box Title', 'new_meta_box', 'page', 'side', 'low' );
}
}
function new_meta_box() {
global $post;
$custom_fields = get_post_custom( $post->ID );
$textarea = $custom_fields['textarea'][0];
?>
<p><strong>Enter text</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 ( ! ( get_post_type( $post_id ) == 'page' ) ) return;
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