Last active
August 29, 2015 14:21
-
-
Save kurozumi/b7c54e0c908c2d234047 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: Wp Metabox Template | |
Version: 0.1-alpha | |
Description: metabox template | |
Author: kurozumi | |
Author URI: http://a-zumi.net | |
Plugin URI: http://a-zumi.net | |
Text Domain: wp-metabox | |
Domain Path: /languages | |
*/ | |
$meta_box = new WP_Meta_Box(); | |
$meta_box->register(); | |
class WP_Meta_Box { | |
private $option_name = "wp_meta_box"; | |
private $menu_slug = "wp_meta_box_slug"; | |
private $section_id = "wp_meta_box_section_id"; | |
public function register() | |
{ | |
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); | |
} | |
public function plugins_loaded() | |
{ | |
add_action('admin_init', array($this, 'admin_init')); | |
} | |
public function admin_init() | |
{ | |
add_action('add_meta_boxes', array($this, 'add_meta_boxes')); | |
add_action('save_post', array($this, 'save_post' ) ); | |
} | |
public function add_meta_boxes() | |
{ | |
$post_types = get_post_types(); | |
foreach($post_types as $post_type) | |
{ | |
/** | |
* Add a meta box to an edit form. | |
* | |
* @param string $id String for use in the 'id' attribute of tags. | |
* @param string $title Title of the meta box. | |
* @param callback $callback Function that fills the box with the desired content. | |
* @param string|WP_Screen $screen Optional. The screen on which to show the box | |
* @param string $context Optional. The context within the screen where the boxes | |
* @param string $priority Optional. The priority within the context where the boxes | |
* @param array $callback_args Optional. Data that should be set as the $args property | |
*/ | |
add_meta_box( | |
'meta-box-id', | |
__('Meta Box Title'), | |
array($this, 'add_meta_box'), | |
$post_type, | |
'normal', | |
'default' | |
); | |
} | |
} | |
public function add_meta_box() | |
{ | |
/** | |
* Add a new section to a settings page. | |
* | |
* @param string $id セクションID | |
* @param string $title セクション名 | |
* @param string $callback セクションの説明などを出力するための関数 | |
* @param string $page 設定ページのslug ※add_menu_page()の$menu_slugと同じものにする | |
*/ | |
add_settings_section( $this->section_id, "", "", $this->menu_slug ); | |
/** | |
* Add a new field to a section of a settings page | |
* | |
* @param string $id 入力項目ID | |
* @param string $title 入力項目名 | |
* @param string $callback 入力項目のHTMLを出力する関数 | |
* @param string $page 設定ページのslug ※addd_menu_page()の$menu_slugと同じものにする | |
* @param string $section セクションID add_settings_section()の$idと同じものにする | |
* @param array $args $callbackの追加引数 | |
*/ | |
add_settings_field('message', 'メッセージ', array($this, 'text_callback'), $this->menu_slug, $this->section_id, array('name' => 'message')); | |
add_settings_field('message2', 'メッセージ2', array($this, 'text_callback'), $this->menu_slug, $this->section_id, array('name' => 'message2')); | |
do_settings_sections($this->menu_slug); | |
} | |
public function text_callback($args) | |
{ | |
global $post_id; | |
$value = get_post_meta($post_id, sprintf("%s_%s", $this->option_name, $args['name']), true); | |
?> | |
<input type="text" id="<?php _e($args['name']);?>" name="<?php printf("%s[%s]", $this->option_name, $args['name']);?>" value="<?php esc_attr_e($value) ?>" /> | |
<?php | |
} | |
public function save_post($post_id) | |
{ | |
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) | |
return false; | |
if(!current_user_can('edit_page', $post_id)) | |
return false; | |
if(!isset($_POST[$this->option_name])) | |
return false; | |
foreach($_POST[$this->option_name] as $meta_key => $mete_value) | |
{ | |
/** | |
* Update post meta field based on post ID. | |
* | |
* @param int $post_id Post ID. | |
* @param string $meta_key Metadata key. | |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. | |
* @param mixed $prev_value Optional. Previous value to check before removing.Default empty. | |
*/ | |
update_post_meta( | |
$post_id, | |
sprintf("%s_%s", $this->option_name, $meta_key), | |
$mete_value, | |
get_post_meta($post_id, sprintf("%s_%s", $this->option_name, $meta_key), true) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment