Created
February 7, 2016 20:01
-
-
Save ryozi-tn/ad4785bb44cf8e6c9e79 to your computer and use it in GitHub Desktop.
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 | |
/* | |
Plugin Name: My Custom Type | |
Plugin URI: 独自の投稿タイプを定義しますん | |
Description: | |
Author: ryozi-tn | |
Version: 0.1 | |
Author URI: | |
*/ | |
class My_Custom_Type { | |
private static $instance; | |
public static function getInstance(){ | |
if (!self::$instance){ | |
$myclass = __CLASS__; | |
self::$instance = new $myclass; | |
} | |
return self::$instance; | |
} | |
public function __construct(){ | |
// 投稿タイプを増やす | |
add_action('init', array($this, 'action_register_post_type')); | |
// 編集画面のヘッダをいじるフック | |
add_action('admin_head-post-new.php', array($this, 'register_custom_post_for_admin')); | |
add_action('admin_head-post.php', array($this, 'register_custom_post_for_admin')); | |
// 編集画面のヘッダ/フッタの埋め込み | |
add_action('admin_head-post-new.php', array($this, 'register_edit_post_header')); | |
add_action('admin_footer-post-new.php', array($this, 'register_edit_post_footer')); | |
add_action('admin_head-post.php', array($this, 'register_edit_post_header')); | |
add_action('admin_footer-post.php', array($this, 'register_edit_post_footer')); | |
// プレビュー画面のヘッダ/フッタの埋め込み | |
add_action('wp_head', array($this, 'register_preview_header')); | |
add_action('wp_footer', array($this, 'register_preview_footer')); | |
add_action('wp', array($this, 'register_hide_admin_bar_from_preview') ); | |
} | |
public function register_custom_post_for_admin(){ | |
global $post; | |
// error_log("--- show ---"); | |
// error_log(print_r($post, true)); | |
} | |
public function action_register_post_type(){ | |
register_post_type('manual', | |
array( | |
'labels' => array( | |
'name' => '投稿(マニュアル)', | |
'sigular_name' => 'マニュアル一覧' | |
), | |
'public' => true, | |
'menu_position' => 5, | |
'has_archive' => 'manual/archive', | |
// 'supports' => array(''), | |
) | |
); | |
} | |
/* | |
* プレビュー時は管理ヘッダを隠す | |
*/ | |
public function register_hide_admin_bar_from_preview(){ | |
if(is_preview()){ | |
add_filter( 'show_admin_bar', '__return_false' ); | |
} | |
} | |
/* | |
* プレビュー時のヘッダにhtmlを埋め込む処理。 | |
*/ | |
public function register_preview_header(){ | |
if(!is_preview()){ | |
return; | |
} | |
?> | |
<!-- <div style="position:absolute; top:0; left:0; border: 1px solid #F00; z-index:9999;">プレビュー画面でだけ表示するもの(ヘッダ)</div> --> | |
<?php | |
} | |
/* | |
* プレビュー時のフッタ部分にhtmlを埋め込む処理。 | |
*/ | |
public function register_preview_footer(){ | |
if(!is_preview()){ | |
return; | |
} | |
?> | |
<!-- <div style="position:absolute; top:0; left:0; border: 1px solid #F00; z-index:9999;">プレビュー画面でだけ表示するもの(フッタ)</div> --> | |
<?php | |
} | |
/* | |
* 編集画面のヘッダ部分にhtmlを埋め込む処理。 | |
*/ | |
public function register_edit_post_header(){ | |
?> | |
<!-- <div style="position:absolute; top:0; left:0; border: 1px solid #F00; z-index:9999;">編集画面でだけ表示するもの(ヘッダ)</div> --> | |
<?php | |
} | |
/* | |
* 編集画面のフッタ部分にhtmlを埋め込む処理。 | |
*/ | |
public function register_edit_post_footer(){ | |
global $post; | |
?> | |
<!-- | |
TODO:新規作成時は少なくともタイトル・コンテンツがないとプレビューできない。 | |
なので、フレームを活性化しないようにすべき。 | |
何らかのトリガで動的に読み込む方がよさそう。ボタンクリックなど。 | |
--> | |
<div style="position:absolute; top:0; left:0; border: 1px solid #F00; z-index:9999;"> | |
<iframe id="preview-frame" width="100%" height="100%" frameborder="0" name="preview-frame" src="<?php echo esc_url( get_preview_post_link( $post ) ); ?>" sandbox="allow-forms allow-scripts allow-top-navigation allow-same-origin"> | |
</iframe> | |
</div> | |
<script type="text/javascript"> | |
// ウィンドウのリサイズに合わせてプレビューの大きさも変更する | |
jQuery(function($){ | |
var resize_window = function (){ | |
var $window = $(window); | |
var $admin_bar = $('#wpadminbar'); | |
var $frame = $('#preview-frame'); | |
var window_h = $window.innerHeight(); | |
var window_w = $window.width(); | |
var admin_bar_h = $admin_bar.height(); | |
$frame.height(window_h - admin_bar_h); | |
$frame.width(window_w); | |
} | |
$(window).resize(resize_window); | |
resize_window(); | |
}); | |
</script> | |
<?php | |
} | |
} | |
// ここでインスタンスを作り、フックを読み込む | |
My_Custom_Type::getInstance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment