Skip to content

Instantly share code, notes, and snippets.

@uruly
Created September 10, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uruly/01ed5af2d997f9b544182deac76cc9d1 to your computer and use it in GitHub Desktop.
Save uruly/01ed5af2d997f9b544182deac76cc9d1 to your computer and use it in GitHub Desktop.
WordPress Plugin.
<?php
/*
Plugin Name: Additional Post Plugin
Description: 投稿ページに追記欄を作成する
Version: 1.0
Author: Reo
*/
define( 'ADDPP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
//ファイルの読み込み
require_once( ADDPP_PLUGIN_DIR . '/class.additional-post.php' );
<?php
/**
*
* Additional Post Common Settings.
*
*/
if ( !class_exists('Additional_Post') ) {
exit;
}
$AdditionalPost = new Additional_Post;
class Additional_Post {
private const label = '追記';
private const id = 'additional';
// 保存したいデータラベル
private const title = 'additional_title';
private const content = 'additional_content';
private const time = 'additional_time';
// インスタンス生成時に呼ばれる
public function __construct() {
add_action( 'admin_menu', array($this, 'add_custom_field') );
// 投稿が保存された時に呼ばれる
add_action( 'save_post', array( $this, 'save_custom_field') );
// ショートコードを追加
add_shortcode( 'additional', array( $this, 'display_shortcode' ));
}
/**
* カスタムフィールドを追加する 追記欄
*/
public function add_custom_field() {
add_meta_box(
self::id, // 表示されるボックスのHTMLでのID名,
self::label, // ラベル
array( $this, 'insert_custom_field'), // 表示する内容の関数
array('post','page','news'), // 表示させたい投稿タイプ
'advanced' // 表示方法 normal / side / advanced
);
}
/**
* 入力欄
*/
public function insert_custom_field() {
global $post;
// nonceフィールドを追加して後でチェックする
wp_nonce_field( wp_create_nonce(__FILE__), 'nonce_custom_field' );
$title = self::title;
$content = self::content;
// 現在の値
$title_value = get_post_meta( $post->ID, $title, true );
$content_value = get_post_meta( $post->ID, $content, true );
echo "タイトル: <br><input type='text' name='{$title}' value='{$title_value}' size='50'><br>";
echo "内容: <br><textarea type='text/html' name='{$content}' cols='70' rows='10'>{$content_value}</textarea><br>";
}
/**
* 保存処理
*/
public function save_custom_field( $post_id ) {
/*
* save_postアクションは他の時にも起動する場合があるので、
* 先ほど作った編集フォームのから適切な認証とともに送られてきたデータかどうかを検証する必要がある。
*/
// nonceがセットされているかどうか確認
if ( ! isset( $_POST['nonce_custom_field'] ) ) {
return;
}
// nonceが正しいかどうか検証
if ( ! wp_verify_nonce( $_POST['nonce_custom_field'], wp_create_nonce(__FILE__) ) ) {
return;
}
// 自動保存の場合はなにもしない
// if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// return;
// }
// ユーザー権限の確認
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* 安全が確認できたのでデータを保存する */
$labels = array( self::title, self::content );
foreach ($labels as $label) {
if(!empty($_POST[$label])){
update_post_meta($post_id, $label, $_POST[$label] ); //値を保存
}else{ //題名未入力の場合
delete_post_meta($post_id, $label); //値を削除
}
}
// 追記時間を保存
update_post_meta( $post_id, self::time, date("Y/m/d") );
}
/**
* ショートコード実行時
*/
function display_shortcode() {
global $post;
$labels = array(
'タイトル' => self::title,
'内容' => self::content,
'追記時間' => self::time
);
$field_id = self::id;
$html_text = "<div class='{$field_id}'>";
// 出力
foreach ( $labels as $label_name => $label ) {
$value = get_post_meta($post->ID, $label, true);
$html_text .= "<div class='{$field_id}-{$label}'><span class='{$field_id}-name'>{$label_name}:</span>{$value}</div>";
}
$html_text .= "</div>";
return $html_text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment