Skip to content

Instantly share code, notes, and snippets.

@ethanpil
Last active August 29, 2015 14:02
Show Gist options
  • Save ethanpil/19f24021165b2b429e28 to your computer and use it in GitHub Desktop.
Save ethanpil/19f24021165b2b429e28 to your computer and use it in GitHub Desktop.
Wordpress Add Custom Meta Box to Page
//Code for template file:
global $post;
if ( get_post_meta($post->ID, 'active_page_showzipform', true ) ) :
//Checkbox is checked... do somehting
endif;
//Code for functions.php --- Begin Custom Code for Page Metabox
function active_add_meta_box() {
add_meta_box(
'active_metabox_pagesettings', // id
'Page Options', // title
'active_page_settings', // callback function
'page', // type of write screen
'side', // context
'low' // priority
);
}
add_action( 'add_meta_boxes', 'active_add_meta_box' );
function active_page_settings(){
global $post;
$custom = get_post_custom($post->ID);
$active_page_showzipform = $custom["active_page_showzipform"][0];
?>
<input type="checkbox" name="active_page_showzipform" <?php if( $active_page_showzipform == true ) { ?>checked="checked"<?php } ?> /> Hide Zipcode Form
<?php
}
add_action('save_post', 'save_active_page_settings');
function save_active_page_settings($post_ID = 0) {
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );
if ($post_type) {
update_post_meta($post_ID, "active_page_showzipform", $_POST["active_page_showzipform"]);
}
return $post_ID;
}
//End Custom Code for Page Metabox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment