ID) ) { $data = ad_column_custom_get_column( $p->post_type, $column ); if( isset($data['name']) ) { $value = get_post_meta( $post_id, $column, true ); echo esc_attr( $value != '' ? $value : '-' ); } } } /* * Since 1.0.0 */ function ad_column_custom_add_post_column( $columns = array(), $post_type = 'page' ) { $data = get_option( 'ad_column_' . $post_type ); if( is_array($data) == false || count($data) == 0 || count($columns) == 0 ) { return $columns; } $list = array(); foreach( $data as $i => $item ) { if( $item['after'] == 'first' ) { $list[ $item['name'] ] = esc_attr( $item['title'] ); unset($data[$i]); } } foreach ( $columns as $key => $value ) { $list[ $key ] = $value; foreach( $data as $i => $item ) { if( $item['after'] == $key ) { $list[ $item['name'] ] = esc_attr( $item['title'] ); unset($data[$i]); } } } return $list; } function ad_column_custom_list_page() { foreach( ad_column_custom_get_post_types() as $post_type => $title ) { add_action( 'manage_'. $post_type .'s_custom_column' , 'ad_column_custom_post_column', 10, 2 ); add_filter( 'manage_'. $post_type .'s_columns' , 'ad_column_custom_add_post_column', 10, 2); } } add_action( 'admin_init' , 'ad_column_custom_list_page'); /* Users */ /* * Since 1.0.0 */ function ad_column_custom_add_user_id_column($columns) { if( is_array($columns) && count($columns) ) { $new = array(); foreach ( $columns as $key => $value ) { if( $key == 'email' ) { $new['display_name'] = esc_attr( __( 'Display Name', 'admin-column-custom' ) ); } $new[$key] = $value; } $columns = $new; } return $columns; } add_filter('manage_users_columns', 'ad_column_custom_add_user_id_column'); /* * Since 1.0.0 */ function ad_column_custom_show_user_id_column_content($value, $column_name, $user_id) { $user = get_userdata( $user_id ); if ( $column_name == 'display_name' ) { return esc_attr( $user->display_name ); } else if ( $column_name == 'custom_field' ) { return esc_attr( get_user_meta( $user_id, 'custom_field', true ) ); } return $value; } add_filter('manage_users_custom_column', 'ad_column_custom_show_user_id_column_content', 10, 3); /* * Since 1.0.1 */ function ad_column_custom_manage_columns( $columns = array() ) { // $columns['url'] = 'URL'; return $columns; } add_filter( 'manage_edit-page_columns', 'ad_column_custom_manage_columns' ); /** * Can't hide these for they are special. * * wp-admin/includes/class-wp-screen.php * * Line 1164 in function `render_list_table_columns_preferences`; * */ function ad_column_custom_get_special_columns() { $special = array( '_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname' ); return $special; } /* * Since 1.0.1 */ function ad_column_custom_get_default_columns( $type = '' ) { $columns = array(); $names = array( 'title', 'author' ); if( $type == 'post' ) { $names = array_merge( $names, array( 'categories', 'tags' ) ); } $names = array_merge( $names, array( 'comments', 'date', 'modified' ) ); foreach( $names as $name ) { $columns[] = array( 'title' => __($name), 'name' => $name, 'default' => 1 ); } return $columns; } /* * Since 1.0.1 */ function ad_column_custom_get_columns( $type = '', $columns = array() ) { if( count($columns) == 0 ) { $columns = ad_column_custom_get_default_columns( $type ); } $data = get_option( 'ad_column_' . $type ); if( is_array($data) && count($data) ) { $list = array(); foreach( $data as $i => $item ) { if( $item['after'] == 'first' ) { $item['default'] = 0; $list[] = $item; unset($data[$i]); } } foreach( $columns as $column ) { $list[] = $column; foreach( $data as $i => $item ) { if( $item['after'] == $column['name'] ) { $item['default'] = 0; $list[] = $item; unset($data[$i]); } } } $columns = $list; } return $columns; } /* * Since 1.0.1 */ function ad_column_custom_get_column( $type = '', $name = '', $key = '' ) { global $ad_column; if( empty($ad_column_data[ $type ]) ) { $ad_column_data[ $type ] = get_option( 'ad_column_' . $type ); } $data = $ad_column_data[ $type ]; if( is_array($data) && count($data)>0 ) { foreach( $data as $item ) { if( $item['name'] == $name ) { if( $key!='' && isset($item[$key]) ) { return $item[$key]; } return $item; } } return $data; } if( $key!='' ) { return ''; } return array(); }