diff --git a/class/mtee-ogp-setting.php b/class/mtee-ogp-setting.php deleted file mode 100644 index bcd5c3f..0000000 --- a/class/mtee-ogp-setting.php +++ /dev/null @@ -1,52 +0,0 @@ - array('page', 'post',), - ); - - foreach ($default_built_ins['posts'] as $built_in) { - add_meta_box( - 'canonical_setting', - 'Canonical設定', - array($this, 'insert_meta_fields'), - $built_in, - 'normal' - ); - } - - if (isset($this->options['custom_posts'])) { - foreach ($this->options['custom_posts'] as $custom_post => $post_chk) { - if ($post_chk == 1) { - add_meta_box( - 'canonical_setting', - 'Canonical設定', - array($this, 'insert_meta_fields'), - $custom_post, 'normal' - ); - } - } - } - } - - } - -} \ No newline at end of file diff --git a/class/mtee_ogp_post_setting.php b/class/mtee_ogp_post_setting.php new file mode 100644 index 0000000..5e3b8d4 --- /dev/null +++ b/class/mtee_ogp_post_setting.php @@ -0,0 +1,82 @@ +options = $options; + add_action('admin_menu', array($this, 'add_meta_fields')); + //画像をアップする場合は、multipart/form-dataの設定が必要なので、post_edit_form_tagをフックしてformタグに追加 + add_action('post_edit_form_tag', array($this, 'custom_meta_box_edit_form_tag')); + add_action('save_post', array($this, 'save_meta_fields')); + } + + public function custom_meta_box_edit_form_tag() { + echo ' enctype="multipart/form-data"'; + } + + //add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法) + public function add_meta_fields() { + $custom_posts = array_values(get_post_types(array('public' => true, '_builtin' => false))); + $target_posts = array_merge(array('page', 'post',), $custom_posts); + foreach ($target_posts as $target_post) { + add_meta_box( + 'ogp_setting', + 'OGP設定', + array( + $this, + 'insert_meta_fields'), + $target_post, + 'normal' + ); + } + } + + // カスタムフィールドの入力エリア + public function insert_meta_fields() { + global $post; + echo '
+ +'; + } + + // カスタムフィールドの値を保存 + public function save_meta_fields($post_id) { + if (!empty($_POST[MTEE_OGP_TITLE])) { //meta_keywordsが入力されている場合 + update_post_meta($post_id, MTEE_OGP_TITLE, $_POST[MTEE_OGP_TITLE]); //値を保存 + } else { //未入力の場合は値を削除 + delete_post_meta($post_id, MTEE_OGP_TITLE); + } + if (!empty($_POST[MTEE_OGP_DESC])) { + update_post_meta($post_id, MTEE_OGP_DESC, $_POST[MTEE_OGP_DESC]); + } else { + delete_post_meta($post_id, MTEE_OGP_DESC); + } + } + } + +} \ No newline at end of file