まだまだ調べ足りないし、説明足りないけど、備忘録として残しておく。
ベースファイル
/wp-content/plugins/event-calendar/calendar.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php /* Plugin Name: Event Calendar Plugin URI: http://www.example.com/plugin Description: イベントカレンダー Author: my name Version: 0.1 Author URI: http://www.example.com */ class EventCalendar { function __construct() { // 管理画面のサイドメニューにフック add_action('admin_menu', array($this, 'add_pages')); // プラグインを有効化したときのフック register_activation_hook (__FILE__, array($this, 'activate')); // プラグインを無効化したときのフック register_deactivation_hook(__FILE__, array($this, 'deactive')); } // 管理画面のサイドメニューに項目追加 function add_pages() { add_menu_page('カレンダー設定','カレンダー設定', 'level_8', __FILE__, array($this,'show_option_page'), '', 26); } // 表示内容の処理 function show_option_page() { // プラグインディレクトリ内のVIEWファイルを読む include(__DIR__ . '/template.php'); } // プラグインを有効化したときに実行される function activate() { // DBにテーブル作成処理など } // プラグインを無効化したときに実行される function deactive() { // DBのテーブル削除処理など } } $event_calendar= new EventCalendar; ?> |
オプション設定ページでビジュアルエディタを使用する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<table> <tr> <th>本文</th> <td> <?php wp_code_editor( $post->post_content, 'body', array( 'dfw' => true, 'drag_drop_upload' => true, 'tabfocus_elements' => 'insert-media-button,save-post', 'editor_height' => 100, 'tinymce' => array( 'resize' => false, 'wp_code_autoresize_on' => $_wp_code_editor_expand, 'add_unload_trigger' => false, ), )); ?> </td> </tr> </table> |
オプション設定ページでメディアアップローダーを使用する
上記で作成したcalendar.phpのクラスに追加する。(下記差分のみ)
calendar.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php class EventCalendar { function __construct() { add_action('admin_print_scripts', array($this, 'admin_scripts')); } function admin_scripts(){ wp_code_enqueue_media(); // メディアアップローダー用のスクリプトをロードする } } ?> |
HTML(template.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<script> jQuery(document).ready(function($){ var _custom_media = true, _orig_send_attachment = wp.media.editor.send.attachment; $('#_unique_name_button').click(function(e) { var send_attachment_bkp = wp.media.editor.send.attachment; console.log(send_attachment_bkp); var button = $(this); var id = button.attr('id').replace('_button', ''); _custom_media = true; wp.media.editor.send.attachment = function(props, attachment){ console.log(_custom_media); if ( _custom_media ) { $("#"+id).val(attachment.url); } else { return _orig_send_attachment.apply( this, [props, attachment] ); }; } wp.media.editor.open(button); return false; }); $('.add_media').on('click', function(){ _custom_media = false; }); }); </script> <div class="uploader"> <input id="_unique_name" name="settings[_unique_name]" type="text" /> <input id="_unique_name_button" class="button" name="_unique_name_button" type="text" value="Upload" /> </div> |
プラグイン用のページ作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php // Request handler function event_redirect() { // 対象のURLではない場合は処理しない if (stripos($_SERVER['REQUEST_URI'], '/event') === FALSE) { return; } // プラグイン用のページを出力 status_header(200); include(__DIR__ . '/category.php'); exit(); } // テンプレートファイルを振り分け時にフック add_action('template_redirect', 'event_redirect', 1); ?> |