WP PLUGIN MTEE(Meta Tag etc Extend) OGPタグ設定
・タクソノミーのOGPタグ設定 ・画像アップロード機能の追加
This commit is contained in:
@@ -15,7 +15,7 @@ if (!class_exists('mtee_ogp_post_setting')) {
|
||||
public function __construct($options) {
|
||||
$this->options = $options;
|
||||
add_action('admin_menu', array($this, 'add_meta_fields'));
|
||||
//画像をアップする場合は、multipart/form-dataの設定が必要なので、post_edit_form_tagをフックしてformタグに追加
|
||||
//post_edit_form_tagをフックしてformタグにmultipart/form-data属性を追加
|
||||
add_action('post_edit_form_tag', array($this, 'custom_meta_box_edit_form_tag'));
|
||||
add_action('save_post', array($this, 'save_meta_fields'));
|
||||
}
|
||||
@@ -47,23 +47,48 @@ if (!class_exists('mtee_ogp_post_setting')) {
|
||||
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) . '"/>
|
||||
<input style="width:100%" type="text"
|
||||
name="' . MTEE_OGP_TITLE . '"
|
||||
value="' . get_post_meta($post->ID, MTEE_OGP_TITLE, true) . '"
|
||||
placeholder="' . $this->set_default_ogp_title() . '"/>
|
||||
</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) . '"/>
|
||||
<input style="width:100%" type="text"
|
||||
name="' . MTEE_OGP_DESC . '"
|
||||
value="' . get_post_meta($post->ID, MTEE_OGP_DESC, true) . '"
|
||||
placeholder="' . $this->set_default_ogp_desc() . '"/>
|
||||
</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/*"/>
|
||||
<input style="width:100%" type="file" name="' . MTEE_OGP_IMG . '" accept="image/*"/>
|
||||
<p>' . $this->set_ogp_thumb($post->ID) . '</p>
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
|
||||
|
||||
public function set_default_ogp_title() {
|
||||
return get_the_title();
|
||||
}
|
||||
|
||||
public function set_default_ogp_desc(): string {
|
||||
return get_bloginfo('name') . 'の' . $this->set_default_ogp_title() . 'です。';
|
||||
}
|
||||
|
||||
|
||||
public function set_ogp_thumb($id): string {
|
||||
$ogp_thumb = get_post_meta($id, MTEE_OGP_IMG, true);
|
||||
if (!empty($ogp_thumb)) {
|
||||
return '<img style="width: 200px;display: block;margin: 1em 0;" src="' . wp_get_attachment_url($ogp_thumb) . '">';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
// カスタムフィールドの値を保存
|
||||
public function save_meta_fields($post_id) {
|
||||
if (!empty($_POST[MTEE_OGP_TITLE])) { //meta_keywordsが入力されている場合
|
||||
@@ -76,6 +101,62 @@ if (!class_exists('mtee_ogp_post_setting')) {
|
||||
} else {
|
||||
delete_post_meta($post_id, MTEE_OGP_DESC);
|
||||
}
|
||||
if (isset($_FILES[MTEE_OGP_IMG]) && $_FILES[MTEE_OGP_IMG]['size'] !== 0) {
|
||||
$this->save_ogp_image('post', $post_id);
|
||||
}
|
||||
}
|
||||
|
||||
public function save_ogp_image($type, $id) {
|
||||
$file_name = $this->set_ogp_filename();
|
||||
$wp_upload_dir = wp_upload_dir(); //現在のuploadディクレトリのパスとURLを入れた配列
|
||||
$upload_file = $_FILES[MTEE_OGP_IMG]['tmp_name'];
|
||||
$upload_path = $wp_upload_dir['path'] . '/' . $file_name;
|
||||
//画像ファイルをuploadディクレトリに移動させる
|
||||
move_uploaded_file($upload_file, $upload_path);
|
||||
|
||||
$this->upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id);
|
||||
}
|
||||
|
||||
public function set_ogp_filename() {
|
||||
$file_name = basename($_FILES[MTEE_OGP_IMG]['name']);
|
||||
$file_name = trim($file_name);
|
||||
return str_replace(' ', '-', $file_name);
|
||||
}
|
||||
|
||||
public function upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id) {
|
||||
if (file_exists($upload_path)) {
|
||||
//添付ファイルを追加
|
||||
$attachment = $this->set_upload_file_param($wp_upload_dir, $file_name, $upload_path);
|
||||
$attach_id = wp_insert_attachment($attachment, $upload_path, $id);
|
||||
if (!function_exists('wp_generate_attachment_metadata')) {
|
||||
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
|
||||
}
|
||||
//添付ファイルのメタデータを生成し保存
|
||||
$this->save_ogp_file($type, $attach_id, $upload_path, $id);
|
||||
} else {
|
||||
//保存失敗
|
||||
echo '画像保存に失敗しました';
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function set_upload_file_param($wp_upload_dir, $file_name, $upload_path): array {
|
||||
return array(
|
||||
'guid' => $wp_upload_dir['url'] . '/' . basename($file_name),
|
||||
'post_mime_type' => $_FILES[MTEE_OGP_IMG]['type'],
|
||||
//正規表現で拡張子なしのスラッグ名を生成
|
||||
'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_path)),
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit'
|
||||
);
|
||||
}
|
||||
|
||||
public function save_ogp_file($type, $attach_id, $upload_path, $id) {
|
||||
$attach_data = wp_generate_attachment_metadata($attach_id, $upload_path);
|
||||
wp_update_attachment_metadata($attach_id, $attach_data);
|
||||
if ($type == 'post') {
|
||||
update_post_meta($id, MTEE_OGP_IMG, $attach_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
} // Exit if accessed directly
|
||||
|
||||
if (!class_exists('mtee_ogp_tax_setting')) {
|
||||
|
||||
/**
|
||||
* Class mtee_tax_setting
|
||||
*/
|
||||
class mtee_ogp_tax_setting {
|
||||
|
||||
private $options;
|
||||
|
||||
public function __construct($options) {
|
||||
$this->options = $options;
|
||||
$this->set_fields();
|
||||
add_action('create_term', array($this, 'save_terms')); //新規追加用フック
|
||||
add_action('edit_terms', array($this, 'save_terms')); //編集ページ用フック
|
||||
}
|
||||
|
||||
public function set_fields() {
|
||||
$taxs = $this->set_tax_types();
|
||||
foreach ($taxs as $tax) {
|
||||
add_action($tax . '_add_form_fields', array($this, 'insert_term_meta_add_fields'));
|
||||
add_action($tax . '_edit_form_fields', array($this, 'insert_term_meta_edit_fields'));
|
||||
//formタグにmultipart/form-data属性を追加
|
||||
add_action($tax . '_term_edit_form_tag', array($this, 'custom_meta_box_edit_form_tag'));
|
||||
add_action($tax . '_add_form', array($this, 'create_form_tag'));
|
||||
}
|
||||
}
|
||||
|
||||
function create_form_tag() {
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
jQuery(document).ready(function () {
|
||||
jQuery('#addtag').attr("enctype", "multipart/form-data");
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function custom_meta_box_edit_form_tag() {
|
||||
echo ' enctype="multipart/form-data"';
|
||||
}
|
||||
|
||||
public function set_tax_types(): array {
|
||||
$custom_tax = get_taxonomies(array('public' => true, '_builtin' => false));
|
||||
$tax_array = array_merge(array('category', 'post_tag'), $custom_tax);
|
||||
$add_tax = filter_input(INPUT_GET, 'taxonomy');
|
||||
|
||||
if (!empty($add_tax)) {
|
||||
return array_merge($tax_array, array($add_tax));
|
||||
}
|
||||
return $tax_array;
|
||||
}
|
||||
|
||||
function insert_term_meta_add_fields($tag) {
|
||||
$input_values = $this->set_input_values($tag);
|
||||
echo '
|
||||
<div class="form-field">
|
||||
OGP title<br>
|
||||
<input type="text" name="' . MTEE_OGP_TITLE . '"
|
||||
value="' . $input_values['title'] . '"
|
||||
placeholder="' . $input_values['title_pl'] . '"
|
||||
class="tax-meta-field" />
|
||||
</div>
|
||||
<div class="form-field">
|
||||
OGP description<br>
|
||||
<input type="text" name="' . MTEE_OGP_DESC . '"
|
||||
value="' . $input_values['desc'] . '"
|
||||
placeholder="' . $input_values['desc_pl'] . '"
|
||||
class="tax-meta-field" />
|
||||
<label>image
|
||||
<input style="width:100%" type="file" name="' . MTEE_OGP_IMG . '" accept="image/*"/>
|
||||
</label>
|
||||
</div>
|
||||
';
|
||||
}
|
||||
|
||||
function insert_term_meta_edit_fields($tag) {
|
||||
$input_values = $this->set_input_values($tag);
|
||||
echo '
|
||||
<tr class="form-field">
|
||||
<th scope="row">OGP</th>
|
||||
<td>
|
||||
<label>
|
||||
title:<br><input type="text" name="' . MTEE_OGP_TITLE . '"
|
||||
value="' . $input_values['title'] . '"
|
||||
placeholder="' . $input_values['title_pl'] . '"
|
||||
class="tax-meta-field" />
|
||||
</label>
|
||||
<label>
|
||||
description:<br><input type="text" name="' . MTEE_OGP_DESC . '"
|
||||
value="' . $input_values['desc'] . '"
|
||||
placeholder="' . $input_values['desc_pl'] . '"
|
||||
class="tax-meta-field" />
|
||||
</label>
|
||||
<label>image
|
||||
<input style="width:100%" type="file" name="' . MTEE_OGP_IMG . '" accept="image/*"/>
|
||||
</label>
|
||||
<p>' . $this->set_ogp_thumb($tag->term_id) . '</p>
|
||||
</td>
|
||||
</tr>
|
||||
';
|
||||
}
|
||||
|
||||
function set_input_values($tag): array {
|
||||
$title_value = '';
|
||||
$desc_value = '';
|
||||
$title_placeholder = '';
|
||||
$desc_placeholder = '';
|
||||
if (isset($tag->term_id)) {
|
||||
$title_value = get_term_meta($tag->term_id, MTEE_OGP_TITLE, true);
|
||||
$desc_value = get_term_meta($tag->term_id, MTEE_OGP_DESC, true);
|
||||
$title_placeholder = $this->set_default_title($tag);
|
||||
$desc_placeholder = $this->set_default_description($title_placeholder);
|
||||
}
|
||||
return array(
|
||||
'title' => $title_value,
|
||||
'desc' => $desc_value,
|
||||
'title_pl' => $title_placeholder,
|
||||
'desc_pl' => $desc_placeholder,
|
||||
);
|
||||
}
|
||||
|
||||
public function set_default_title($tag) {
|
||||
return $tag->name;
|
||||
}
|
||||
|
||||
public function set_default_description($title): string {
|
||||
return get_bloginfo('name') . 'の' . $title . 'です。';
|
||||
}
|
||||
|
||||
public function set_ogp_thumb($id): string {
|
||||
$ogp_thumb = get_term_meta($id, MTEE_OGP_IMG, true);
|
||||
if (!empty($ogp_thumb)) {
|
||||
return '<img style="width: 200px;display: block;margin: 1em 0;" src="' . wp_get_attachment_url($ogp_thumb) . '">';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function save_terms($term_id) {
|
||||
if (array_key_exists(MTEE_OGP_TITLE, $_POST)) {
|
||||
update_term_meta($term_id, MTEE_OGP_TITLE, $_POST[MTEE_OGP_TITLE]);
|
||||
}
|
||||
if (array_key_exists(MTEE_OGP_DESC, $_POST)) {
|
||||
update_term_meta($term_id, MTEE_OGP_DESC, $_POST[MTEE_OGP_DESC]);
|
||||
}
|
||||
if (isset($_FILES[MTEE_OGP_IMG]) && $_FILES[MTEE_OGP_IMG]['size'] !== 0) {
|
||||
$this->save_ogp_image('term', $term_id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function save_ogp_image($type, $id) {
|
||||
$file_name = $this->set_ogp_filename();
|
||||
$wp_upload_dir = wp_upload_dir(); //現在のuploadディクレトリのパスとURLを入れた配列
|
||||
$upload_file = $_FILES[MTEE_OGP_IMG]['tmp_name'];
|
||||
$upload_path = $wp_upload_dir['path'] . '/' . $file_name;
|
||||
//画像ファイルをuploadディクレトリに移動させる
|
||||
move_uploaded_file($upload_file, $upload_path);
|
||||
|
||||
$this->upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id);
|
||||
}
|
||||
|
||||
public function set_ogp_filename() {
|
||||
$file_name = basename($_FILES[MTEE_OGP_IMG]['name']);
|
||||
$file_name = trim($file_name);
|
||||
return str_replace(' ', '-', $file_name);
|
||||
}
|
||||
|
||||
public function upload_ogp_file($type, $upload_path, $wp_upload_dir, $file_name, $id) {
|
||||
if (file_exists($upload_path)) {
|
||||
//添付ファイルを追加
|
||||
$attachment = $this->set_upload_file_param($wp_upload_dir, $file_name, $upload_path);
|
||||
$attach_id = wp_insert_attachment($attachment, $upload_path, $id);
|
||||
if (!function_exists('wp_generate_attachment_metadata')) {
|
||||
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
|
||||
}
|
||||
//添付ファイルのメタデータを生成し保存
|
||||
$this->save_ogp_file($type, $attach_id, $upload_path, $id);
|
||||
} else {
|
||||
//保存失敗
|
||||
echo '画像保存に失敗しました';
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function set_upload_file_param($wp_upload_dir, $file_name, $upload_path): array {
|
||||
return array(
|
||||
'guid' => $wp_upload_dir['url'] . '/' . basename($file_name),
|
||||
'post_mime_type' => $_FILES[MTEE_OGP_IMG]['type'],
|
||||
//正規表現で拡張子なしのスラッグ名を生成
|
||||
'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_path)),
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit'
|
||||
);
|
||||
}
|
||||
|
||||
public function save_ogp_file($type, $attach_id, $upload_path, $id) {
|
||||
$attach_data = wp_generate_attachment_metadata($attach_id, $upload_path);
|
||||
wp_update_attachment_metadata($attach_id, $attach_data);
|
||||
if ($type == 'post') {
|
||||
update_post_meta($id, MTEE_OGP_IMG, $attach_id);
|
||||
} elseif ($type == 'term') {
|
||||
update_term_meta($id, MTEE_OGP_IMG, $attach_id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -62,18 +62,22 @@ class="tax-meta-field" />
|
||||
$input_values = $this->set_input_values($tag);
|
||||
echo '
|
||||
<tr class="form-field">
|
||||
<th scope="row">meta keywords</th>
|
||||
<td><input type="text" name="' . MTEE_NAME_KEYWORDS . '"
|
||||
<th scope="row">meta keywords<br>meta description</th>
|
||||
<td>
|
||||
<label>meta keywords:<br>
|
||||
<input type="text" name="' . MTEE_NAME_KEYWORDS . '"
|
||||
value="' . $input_values['keywords'] . '"
|
||||
placeholder="' . $input_values['keyword_pl'] . '"
|
||||
class="tax-meta-field" /></td>
|
||||
</tr>
|
||||
<tr class="form-field">
|
||||
<th>meta description</th>
|
||||
<td><input type="text" name="' . MTEE_NAME_DESCRIPTION . '"
|
||||
class="tax-meta-field" />
|
||||
</label>
|
||||
<label>
|
||||
meta description:<br>
|
||||
<input type="text" name="' . MTEE_NAME_DESCRIPTION . '"
|
||||
value="' . $input_values['desc'] . '"
|
||||
placeholder="' . $input_values['desc_pl'] . '"
|
||||
class="tax-meta-field" /></td>
|
||||
class="tax-meta-field" />
|
||||
</label>
|
||||
</td>
|
||||
</tr>
|
||||
';
|
||||
}
|
||||
|
||||
@@ -49,8 +49,10 @@ if ($mtee->is_enable('canonical_setting')) {
|
||||
|
||||
// enabled my OGP tag
|
||||
if ($mtee->is_enable('ogp_setting')) {
|
||||
require_once MTEE_CLASS_DIR . 'mtee_ogp_tax_setting.php';
|
||||
require_once MTEE_CLASS_DIR . 'mtee_ogp_post_setting.php';
|
||||
require_once MTEE_CLASS_DIR . 'mtee_meta_output_ogp.php';
|
||||
new mtee_ogp_tax_setting($options);
|
||||
new mtee_ogp_post_setting($options);
|
||||
new mtee_meta_output_ogp();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user