WP PLUGIN MTEE(Meta Tag etc Extend) OGP設定追加

・OGP設定追加
This commit is contained in:
2021-05-30 14:25:57 +09:00
parent c7f4e41e1c
commit 0bc4ee1d23
2 changed files with 82 additions and 52 deletions
-52
View File
@@ -1,52 +0,0 @@
<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (!class_exists('mtee_ogp_setting')) {
class mtee_ogp_setting {
public function __construct() {
//Posts
//add_action('admin_menu', array($this, 'add_meta_fields'));
}
//-------------------------------------------------------------------------------------------
// 投稿、ページのカスタムフィールド設定
//-------------------------------------------------------------------------------------------
public function add_meta_fields() {
//add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法)
$default_built_ins = array(
'posts' => 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'
);
}
}
}
}
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (!class_exists('mtee_ogp_post_setting')) {
/**
* Class mtee_ogp_post_setting
*/
class mtee_ogp_post_setting {
private $options;
public function __construct($options) {
$this->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 '<div class="meta_key_desc_box">
<label style="font-weight: bold; display: block">title</label>
<div>
<input style="width:100%" type="text" name="' . MTEE_OGP_TITLE . '" value="' . get_post_meta($post->ID, MTEE_OGP_TITLE, true) . '"/>
</div>
</div>
<div class="meta_key_desc_box">
<label style="font-weight: bold; display: block">description</label>
<div>
<input style="width:100%" type="text" name="' . MTEE_OGP_DESC . '" value="' . get_post_meta($post->ID, MTEE_OGP_DESC, true) . '"/>
</div>
</div>
<div class="meta_key_desc_box">
<label style="font-weight: bold; display: block">image</label>
<div>
<input style="width:100%" type="file" name="' . MTEE_OGP_IMG . '" value="' . get_post_meta($post->ID, MTEE_OGP_IMG, true) . '" accept="image/*"/>
</div>
</div>';
}
// カスタムフィールドの値を保存
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);
}
}
}
}