Created
August 26, 2011 08:26
-
-
Save iamntz/1172977 to your computer and use it in GitHub Desktop.
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 | |
$ntz_featured_fields = array( | |
array( | |
"name" => "_ntz_is_featured", | |
"label" => "Featured?", | |
"type" => "checkbox" | |
), // checkbox | |
array( | |
"name" => "_ntz_featured_title", | |
"label" => "Featured Title (leave empty for post title)", | |
"type" => "text" | |
), // text | |
array( | |
"name" => "_ntz_featured_description", | |
"label" => "Featured description (leave emtpy for post excerpt)", | |
"type" => "textarea" | |
), // text | |
); | |
function ntz_featured_meta( $post_data, $meta_info ) { | |
global $post, $ntz_featured_fields; | |
echo '<div class="ntz_panel"><input type="hidden" name="ntz_featured_metaes_nonce" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />'; | |
foreach( $ntz_featured_fields as $o ){ | |
$val = get_post_meta( $post->ID, $o['name'], true ); | |
echo '<p>'; | |
switch ( $o['type'] ){ | |
case "checkbox": | |
$isChecked = ( $val == 1 ? 'checked="checked"' : '' ); // we store checked checkboxes as 1 | |
echo '<label>' . $o['label'] . ' <input type="checkbox" name="' . $o['name'] . '" id="' . $o['name'] . '" ' . $isChecked . ' /></label>'; | |
break; // checkbox | |
case "textarea": | |
echo '<textarea name="' . $o['name'] . '" id="' . $o['name'] . '" cols="10" rows="5" placeholder="' . $o['label'] . '">' . $val . '</textarea>'; | |
break; // textarea | |
case "text": | |
default: | |
echo '<input type="text" name="' . $o['name'] . '" id="' . $o['name'] . '" value="' . $val . '" placeholder="' . $o['label'] . '" />'; | |
break; // text & default | |
}// swtich | |
}// foreach | |
echo '</div>'; | |
?> | |
<style type="text/css" media="screen"> | |
.ntz_panel input[type="text"], | |
.ntz_panel textarea { | |
width:100%; | |
} | |
.ntz_panel .desc { | |
text-align:right; | |
} | |
</style> | |
<?php | |
} | |
function ntz_create_featured_meta() { | |
if ( function_exists( 'add_meta_box' ) ) { | |
add_meta_box( 'ntz_featured_meta', 'This post is featured?', 'ntz_featured_meta', 'post', 'side', 'high' ); | |
} | |
} | |
function ntz_save_featured_meta( $post_id ) { | |
global $post, $post_id, $ntz_featured_fields; | |
if ( in_array( $_REQUEST['post_type'], array('page') ) ) { | |
if ( !current_user_can( 'edit_page', $post_id ) ) {return $post_id;} | |
} else { | |
if ( !current_user_can( 'edit_post', $post_id ) ) {return $post_id;} | |
} | |
foreach($ntz_featured_fields as $o){ | |
if ( !wp_verify_nonce( $_REQUEST['ntz_featured_metaes_nonce'], plugin_basename(__FILE__) )) { | |
return $post_id; | |
} | |
switch ($o['type']){ | |
case "checkbox": | |
update_post_meta( $post_id, $o['name'], isset( $_REQUEST[$o['name']] ) ); | |
break; | |
default: | |
update_post_meta($post_id, $o['name'], $_REQUEST[$o['name']]); | |
break; | |
} | |
} | |
} | |
add_action( 'admin_menu', 'ntz_create_featured_meta' ); | |
add_action( 'save_post', 'ntz_save_featured_meta' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment