Created
November 23, 2011 14:47
-
-
Save marushu/1388855 to your computer and use it in GitHub Desktop.
テキストボックスをカスタムフィールドへ追加する(register_meta_box_cbではない)
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 | |
//▼テキストボックスをカスタムフィールドへ追加する | |
/* フォーム表示 */ | |
function html_source_for_layout_custom_box() { | |
$c_text = get_post_meta( $_GET['post'], 'c_text' ); | |
echo '<label for="c_text">テキスト</label> '; | |
echo '<input type="text" name="c_text" value="'.$c_text[0].'">'; | |
} | |
/* メタボックスに追加 */ | |
function add_layout_custom_box() { | |
add_meta_box('text_layout', 'テキスト入力', 'html_source_for_layout_custom_box', 'one_line_comment', 'normal', 'high'); | |
} | |
/* カスタムフィールドの値をDBに保存 */ | |
function save_custom_field_postdata( $post_id ) { | |
$mydata = $_POST['c_text']; | |
if ( "" == get_post_meta( $post_id, 'c_text' )) { | |
add_post_meta( $post_id, 'c_text', $mydata, true ) ; | |
} else if ( $mydata != get_post_meta( $post_id, 'c_text' )) { | |
update_post_meta( $post_id, 'c_text', $mydata ) ; | |
} else if ( "" == $mydata ) { | |
delete_post_meta( $post_id, 'c_text' ) ; | |
} | |
} | |
add_action('admin_menu', 'add_layout_custom_box'); | |
add_action('save_post', 'save_custom_field_postdata'); | |
//▲テキストボックスをカスタムフィールドへ追加する | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
register_post_type()の引数のregister_meta_box_cbを使ってはいないけど、単純にテキストボックスを追加できる。
参考URL:http://komodo.arrow.jp/wp-cms/cms/%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%82%92%E7%8B%AC%E8%87%AA%E3%81%A7%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%9E%E3%82%A4%E3%82%BA%EF%BC%8D%E5%9F%BA%E6%9C%AC/