Skip to content

Instantly share code, notes, and snippets.

@miya0001
Forked from marushu/gist:1389104
Created November 23, 2011 16:32
Show Gist options
  • Save miya0001/1389146 to your computer and use it in GitHub Desktop.
Save miya0001/1389146 to your computer and use it in GitHub Desktop.
register_post_type()の引数であるregister_meta_box_cbのコールバック巻数
<?php
/****************************************************************************/
/* ! ▼register_post_typeのregister_meta_box_cbのコールバックここから
参考URL:http://wpxtreme.jp/how-to-use-custom-post-types-in-wordpress-3-0-beta1
http://wpdocs.sourceforge.jp/関数リファレンス/add_meta_box
http://ja.forums.wordpress.org/topic/4693?replies=4
*/
/****************************************************************************/
function my_limit_day_meta_box($post){
add_meta_box('my_limit_day_meta', '表示期限', 'my_limit_day_meta_html', 'one_line_comment', 'normal', 'high');
}
function my_limit_day_meta_html($post, $box){
$days = get_post_meta($post->ID, 'days', true);
//var_dump( $post );
echo wp_nonce_field('my_limit_day_meta', 'my_meta_nonce');
echo '<p style="font-weight: bold; font-size: 1.4em; color: #016701;">この投稿を表示させる日数は、';
echo '<input style="font-weight: bold; font-size: 1.8em; width: 5em; text-align: center;" type="text" name="days" value="' . $days . '" size="10"> 日間です。</p>';
//echo '<p>ココは日にちが入るはずなんだけどなぁ…' . $days . '</p>';
}
add_action('save_post', 'my_limit_day_meta_update');
function my_limit_day_meta_update($post_id){
if(!wp_verify_nonce( $_POST['my_meta_nonce'], 'my_limit_day_meta'))
return $post_id;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if (isset($_POST['action']) && $_POST['action'] == 'inline-save') {
return $id;
}
if('one_line_comment' == $_POST['post_type']){
if(!current_user_can('edit_post', $post_id))
return $post_id;
}else{
return $post_id;
}
if (isset($_POST['days']) && intval($_POST['days'])) {
update_post_meta( $post_id, 'days', intval($_POST['days'])) ;
} else {
delete_post_meta( $post_id, 'days' ) ;
}
}
//
add_filter('manage_edit-one_line_comment_columns', 'my_limit_day_columns');
function my_limit_day_columns($columns){
$columns = array(
'cb' => '<input type="checkbox"/>',
'title' => '1行コメントタイトル',
'days' => '設定表示期間(日)',
'date' => '日付'
);
return $columns;
}
//
add_action('manage_posts_custom_column', 'my_limit_day_column');
function my_limit_day_column($column){
global $post;
if('image' == $column) the_post_thumbnail(array(64, 64), 'class=featured-image'); //あったら出る
elseif ("tag" == $column) the_terms(0, 'span'); //あったら出る
elseif ("days" == $column) echo get_post_meta($post->ID, 'days', true); //今回はコレが出る
}
// ▲register_post_typeのregister_meta_box_cbのコールバックここまで
?>
@miya0001
Copy link
Author

1)クイックフォームからの投稿に対する対策
2)$_POST['days'] をそのまま使うのは気持ち悪い。数字が欲しいならintva()でフィルターをかける。
3)post_metaのキーはアンダーバーで始めるようにすると幸せになれます。 http://wpxtreme.jp/meta-key-and-value-hidden-on-custom-fields

@marushu
Copy link

marushu commented Nov 23, 2011

アンダーバーってそんな意味があったんすね…^^;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment