Last active
April 10, 2019 09:29
-
-
Save WordPress-Handbuch/9a1bb261311833ab0c943820246c31d9 to your computer and use it in GitHub Desktop.
Adding a custom taxonomy wh_eventtype to the custom post type wh_event (WordPress 5 needs show_in_rest for the metaboxes to appear)
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.4 | |
*/ | |
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 ); | |
function add_wh_eventtype() { | |
$labels = array( | |
'name' => 'Veranstaltungsart', | |
'singular_name' => 'Veranstaltungsart', | |
'search_items' => 'Veranstaltungsarten durchsuchen', | |
'all_items' => 'alle Veranstaltungsarten', | |
'parent_item' => 'Übergeordnete Veranstaltungsart', | |
'parent_item_colon' => 'Übergeordnete Veranstaltungsart:', | |
'edit_item' => 'Veranstaltungsart bearbeiten', | |
'update_item' => 'Veranstaltungsart aktualisieren', | |
'add_new_item' => 'Veranstaltungsart hinzufügen', | |
'new_item_name' => 'Neue Veranstaltungsart', | |
'not_found' => 'Keine Veranstaltungsarten gefunden', | |
'menu_name' => 'Veranstaltungsarten' | |
); | |
$args = array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'show_in_rest' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'genre' ), | |
); | |
register_taxonomy( 'eventtype', array('wh_event', 'page'), $args ); | |
} | |
add_action( 'init', 'add_wh_eventtype', 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment