Last active
April 10, 2019 09:22
-
-
Save WordPress-Handbuch/ece16ed911351d277bdb8b543bac1e9e to your computer and use it in GitHub Desktop.
Replace custom post type overview columns with custom fields from the meta-box configuration (WordPress 5 example for event dates and locations)
This file contains hidden or 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: WH Custom Post Type | |
* Version: 0.3 | |
*/ | |
function add_wh_events() { | |
$labels = array( | |
'name' => 'Veranstaltungen', | |
'singular_name' => 'Veranstaltung', | |
'add_new' => 'Erstellen', | |
'add_new_item' => 'Neue Veranstaltung erzeugen', | |
'edit_item' => 'Veranstaltung bearbeiten', | |
'new_item' => 'Neue Veranstaltung', | |
'view_item' => 'Veranstaltung ansehen', | |
'view_items' => 'Veranstaltungen ansehen', | |
'search_items' => 'Nach Veranstaltungen suchen', | |
'not_found' => 'Nichts gefunden', | |
'not_found_in_trash' => 'Nichts im Papierkorb gefunden', | |
'all_items' => 'Alle Veranstaltungen', | |
'archives' => 'Veranstaltungsarchiv', | |
'attributes' => 'Veranstaltungsattribute', | |
'insert_into_item' => 'Einfügen', | |
'uploaded_to_this_item' => 'Medien für Veranstaltungen', | |
'featured_image' => 'Veranstaltungs-Flyer', | |
'set_featured_image' => 'Veranstaltungs-Flyer festlegen', | |
'remove_featured_image' => 'Veranstaltungs-Flyer entfernen', | |
'use_featured_image' => 'Veranstaltungs-Flyer verwenden', | |
'menu_name' => 'Veranstaltungen', | |
'filter_items_list' => 'Veranstaltungen', | |
'items_list_navigation' => 'Veranstaltungen', | |
'items_list' => 'Weitere Veranstaltungen', | |
'name_admin_bar' => 'Veranstaltung', | |
); | |
$args = array( | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', ), | |
'taxonomies' => array( 'category', 'post_tag' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_in_rest' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => 5, | |
'can_export' => false, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'rewrite' => array('slug' => 'veranstaltung'), | |
'capability_type' => 'page', | |
); | |
register_post_type( 'wh_event', $args ); | |
} | |
add_action( 'init', 'add_wh_events', 0 ); | |
function add_wh_event_fields() { | |
add_meta_box( 'location', 'Ort', 'add_wh_events_location', 'wh_event', 'side', 'high' ); | |
add_meta_box( 'eventdate', 'Datum', 'add_wh_events_eventdate', 'wh_event', 'side', 'high' ); | |
} | |
function add_wh_events_location() { | |
global $post; | |
$custom = get_post_custom( $post->ID ); | |
$location = $custom['location'][0]; | |
echo '<input type="text" name="location" value="' . $location . '" />'; | |
} | |
function add_wh_events_eventdate( $post ) { | |
wp_enqueue_script( 'jquery-ui-datepicker' ); | |
wp_enqueue_style( 'jquery-ui-style', '//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css', true); | |
$eventdate = get_post_meta( $post->ID, 'eventdate', true ); | |
?> | |
<script> | |
jQuery(document).ready(function() { | |
jQuery('#eventdateid').datepicker({ | |
dateFormat : 'yy-mm-dd' | |
}); | |
}); | |
</script> | |
<input type="text" name="eventdate" id="eventdateid" value="<?php echo $eventdate; ?>" /></td> | |
<?php | |
} | |
function save_wh_event_fields( $post_id ) { | |
$post_type = get_post_type( $post_id ); | |
if ( $post_type != 'wh_event' ) { | |
return; | |
} | |
update_post_meta( $post_id, 'location', $_POST['location'] ); | |
update_post_meta( $post_id, 'eventdate', $_POST['eventdate'] ); | |
} | |
add_action( 'admin_init', 'add_wh_event_fields' ); | |
add_action( 'save_post', 'save_wh_event_fields' ); | |
function define_wh_events_list_columns( $columns ) { | |
$columns = array ( | |
'cb' => '<input type="checkbox" />', | |
'title' => 'Veranstaltung', | |
'location' => 'Ort', | |
'eventdate' => 'Datum' | |
); | |
return $columns; | |
} | |
function output_wh_events_list_field( $column, $post_id ) { | |
$custom = get_post_custom( $post_id ); | |
if ( $column == 'location' ) { | |
echo $custom['location'][0]; | |
} | |
elseif ( $column == 'eventdate' ) { | |
$custom = get_post_custom(); | |
echo date_i18n( get_option( 'date_format' ), strtotime( $custom['eventdate'][0] ) ); | |
} | |
} | |
add_filter( 'manage_edit-wh_event_columns', 'define_wh_events_list_columns' ); | |
add_action( 'manage_posts_custom_column', 'output_wh_events_list_field', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment