Meta Box Datepicker

public function meta_box_1($post){
// Enqueue Datepicker + jQuery UI CSS
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui-style','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/smoothness/jquery-ui.css',true);

//This is where the normal meta box stuff goes. Nonce, get_post_meta, etc

?>
<script type="text/javascript">jQuery(document).ready(function(){ jQuery('#presentation_date').datepicker({dateFormat : 'mm/dd/yy'}); });</script>
<?php
}

Meta box for custom sidebar

function ter_create_page_meta_box(){ new TerPageMetaBox(); }

if(is_admin()){
add_action('load-post.php','ter_create_page_meta_box');
add_action('load-post-new.php','ter_create_page_meta_box');
}

class TerPageMetaBox{
public function __construct(){
add_action('add_meta_boxes',array($this,'add_meta_box'));
add_action('save_post',array($this,'save'));
}

public function add_meta_box($post_type){
$post_types = array('page','locations','services','resources');
if(in_array($post_type,$post_types)) add_meta_box('ter_page_meta_box',__('Custom Sidebar HTML','terra'),array($this,'render_meta_box_content'),$post_type,'advanced','high');
}

public function save($post_id){
$post_types = array('page','locations','services','resources');
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if(!in_array($_POST['post_type'],$post_types)) return $post_id;
if(!wp_verify_nonce($_POST['ter_custom_sidebar'],plugin_basename(__FILE__))) return $post_id;
if(!current_user_can('edit_post',$post_id)) return $post_id;
update_post_meta($post_id,'ter_custom_sidebar_html',$_POST['ter_custom_sidebar_html']);
}

public function render_meta_box_content($post){
wp_nonce_field(plugin_basename(__FILE__),'ter_custom_sidebar');
echo '<label>Sidebar HTML:</label><br>';
echo '<textarea style="width:90%" name="ter_custom_sidebar_html">' . esc_attr(get_post_meta($post->ID,'ter_custom_sidebar_html',1)) . '</textarea>';
}
}

Add meta box to attachment

<?php

$hypbi_attachment_meta = new HypbiAttachmentMeta();

class HypbiAttachmentMeta{
public function __construct(){
add_filter('attachment_fields_to_edit',array($this,'add_image_meta_field'),null,2);
add_filter('attachment_fields_to_save',array($this,'save_image_meta_field'),null,2);
}
public function add_image_meta_field($form_fields,$post){
$form_fields['hypbi_option_code'] = array(
'label' => __('Build It Option Code'),
'value' => get_post_meta($post->ID,'_hypbi_option_code', true),
'helps' => __('Comma separated option code. For multiple options separate with pipe |'),
);
return $form_fields;
}
public function save_image_meta_field($post,$attachment){
if(isset($attachment['hypbi_option_code'])) update_post_meta($post['ID'],'_hypbi_option_code',$attachment['hypbi_option_code']);
return $post;
}
}

?>

Create WordPress Options Page

<?php
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;

/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}

/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'My Settings',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}

/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>My Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}

/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);

add_settings_section(
'setting_section_id', // ID
'My Custom Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);

add_settings_field(
'id_number', // ID
'ID Number', // Title
array( $this, 'id_number_callback' ), // Callback
'my-setting-admin', // Page
'setting_section_id' // Section
);

add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'my-setting-admin',
'setting_section_id'
);
}

/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['id_number'] ) )
$new_input['id_number'] = absint( $input['id_number'] );

if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );

return $new_input;
}

/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}

/**
* Get the settings option array and print one of its values
*/
public function id_number_callback()
{
printf(
'<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
);
}

/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="my_option_name[title]" value="%s" />',
isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
);
}
}

if( is_admin() )
$my_settings_page = new MySettingsPage();

Post Meta Filter

<?php
/*
Plugin Name: CFInterceptor
Plugin URI: http://hyperspatial.com
Description: CFInterceptor is used for wordpress filters that need to load early in execution
Version: 1.0
Author: Adam J Nowak
Author URI: http://hyperspatial.com
License: GPL2
*/

/*
* CFInterceptor is designed to work with the CFProjectFilters class
*
* Calling actions/filters here registers them before the plugins are loaded
* Write callbacks in the CFProjectFilters class here: /wp-content/plugins/cf-project/includes/project-filters.php
*/

//Project Type Meta Filter
add_filter( 'get_post_metadata', array( 'CFProjectFilters', 'project_type_meta_filter' ), 10, 4 );

?>

<?php
/*
* CFProjectFilters is a class to package action and filter callbacks
*
* WP add_action() or add_filter() can be called in /wp-content/mu-plugins/interceptor.php
* Calling in the mu-plugins forces the actions/filters to be registered before the plugins are loaded
*/

class CFProjectFilters{
/* ~~~~~~~~~~ Project Type Meta Filter ~~~~~~~~~~ */
public function project_type_meta_filter( $metadata, $object_id, $meta_key, $single ){
if( $meta_key != '_project_type' ) return;
$meta_cache = wp_cache_get( $object_id, 'post_meta' );
$project_type = $meta_cache['_project_type'][0];

if( $project_type == 3 ) $metadata = 'Project type 3 is now a string, yay';
return $metadata;
}
}
?>

Comment Meta Box

<?php

function my_comment_meta_box($comment){?>
<div class="meta-box-sortables" id="normal-sortables">
<div class="postbox " id="wedco-com-meta">
<div title="Click to toggle" class="handlediv"><br></div>
<h3 class="hndle"><span>My Meta</span></h3>
<div class="inside">

<?php echo 'put meta box stuff here'; ?>

</div>
</div>
</div>
<?php
}
add_action('add_meta_boxes_comment','my_comment_meta_box');

//Save Comment Meta - No form in the above example - this is extra and for future reference
function my_save_comment_meta() {
$comment_id = $_POST['comment_ID'];
$posted_meta = $_POST['my_meta_name'];
update_comment_meta($comment_id,'my_meta_name',$posted_meta);
}
add_action('edit_comment','my_save_comment_meta');

?>

Comment List Columns

function add_comment_columns($columns){
return array_merge($columns,array('metafield_name' => __('Meta 1'),'metafield_name2' => __('Meta 2')));
}
add_filter('manage_edit-comments_columns','add_comment_columns');

function add_comment_column_data($column,$comment_ID){
switch($column){
case 'metafield_name':
if($meta = get_comment_meta($comment_ID,$column,true)) echo $meta;
break;
case 'metafield_name2':
if($meta = get_comment_meta($comment_ID,$column,true)) echo $meta;
break;
default:
return;
}
}
add_filter('manage_comments_custom_column','add_comment_column_data',10,2);

?>

Change meta on all posts

<?php query_posts('showposts=-1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php

$custom_field_name = 'Sort Order';
$desired_field_value = '100';

add_post_meta($post->ID,$custom_field_name,$desired_field_value,true);
the_title();
echo ' - ' . get_post_meta($post->ID,$custom_field_name,true);
echo'<br>';

?>
<?php endwhile; endif; ?>

Default meta - Custom field

<?php

//Set Default Meta Value
function set_default_meta($post_ID){
$current_field_value = get_post_meta($post_ID,'Sort Order',true);
$default_meta = '100';

if ($current_field_value == '' && !wp_is_post_revision($post_ID)){
add_post_meta($post_ID,'Sort Order',$default_meta,true);
}
return $post_ID;
}

add_action('wp_insert_post','set_default_meta');

?>