Skip to content

Instantly share code, notes, and snippets.

@torounit
Last active February 29, 2016 14:39
Show Gist options
  • Save torounit/64d66203042459a6d25b to your computer and use it in GitHub Desktop.
Save torounit/64d66203042459a6d25b to your computer and use it in GitHub Desktop.
WordPressの投稿一覧にカスタムフィールドを表示する。並び替えにも対応。
<?php
Class Admin_Column_Custom_Field {
/** @var string */
private $field_key;
/** @var string */
private $field_label;
/** @var string */
private $post_type;
/** @var bool */
private $is_num;
/**
* @param string $post_type
* @param string $field_key
* @param string $field_label
* @param bool $is_num
*/
public function __construct( $post_type, $field_key, $field_label = '' , $is_num = false ) {
$this->post_type = $post_type;
$this->field_key = $field_key;
$this->field_label = $field_label;
if ( ! $field_label ) {
$this->field_label = $field_key;
}
$this->is_num = $is_num;
$this->add_hooks();
}
/**
*
*/
protected function add_hooks() {
add_filter( 'manage_posts_columns', [ $this, 'manage_posts_columns' ] );
add_filter( 'manage_edit-' . $this->post_type . '_sortable_columns', [ $this, 'sortable_columns' ] );
add_action( 'manage_posts_custom_column', [ $this, 'manage_posts_custom_column' ] );
add_action( 'load-edit.php', function(){
add_filter( 'request', [ $this, 'sort_edit' ] );
} );
}
/**
* @param callable $callback
*
* @param int $priority
*/
public function add_filter( callable $callback , $priority = 10 ) {
add_filter( 'admin_custom_column_'.$this->post_type.'_'.$this->field_key, $callback, $priority );
}
/**
* @param array $columns
*
* @return array
*/
public function manage_posts_columns( $columns ) {
global $post;
if ( $post->post_type == $this->post_type ) {
$date = $columns['date'];
unset( $columns['date'] );
$columns[ $this->field_key ] = $this->field_label;
$columns['date'] = $date;
}
return $columns;
}
/**
* @param string $column_name
*/
public function manage_posts_custom_column( $column_name ) {
global $post;
if ( $post->post_type == $this->post_type ) {
if ( $column_name == $this->field_key ) {
$column = get_post_meta( $post->ID, $this->field_key, true );
$column = apply_filters( 'admin_custom_column_'.$this->post_type.'_'.$this->field_key, $column );
echo esc_html( $column );
}
}
}
/**
* @param array $columns
*
* @return array
*/
public function sortable_columns( $columns ) {
$columns[ $this->field_key ] = $this->field_key;
return $columns;
}
/**
* @param array $vars $wp_query->query_vars.
*
* @return array
*/
public function sort_edit( $vars ) {
if ( isset( $vars['post_type'] ) && $this->post_type == $vars['post_type'] ) {
if ( isset( $vars['orderby'] ) && $vars['orderby'] == $this->field_key ) {
$vars = array_merge(
$vars,
[
'meta_key' => $this->field_key,
'orderby' => ( $this->is_num ) ? 'meta_value_num' : 'meta_value',
]
);
}
}
return $vars;
}
}
<?php
$cf_column = new Admin_Column_Custom_Field( 'my_post_type', 'field_key', 'field_label' );
//出力時の値を変更するフィルターを登録。
$cf_column->add_filter('capital_P_dangit');
//こっちでもフィルターできます。動作は全く一緒。
add_filter( 'admin_custom_column_my_post_type_field_key', 'capital_P_dangit' );
$cf_column->add_filter('my_filter');
function my_filter( $value ) {
return strip_tags( $value );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment