Last active
April 10, 2019 09:12
-
-
Save WordPress-Handbuch/9a3564015b5c3934a24c0a8a4632df05 to your computer and use it in GitHub Desktop.
Extending a custom post type by custom fields through the meta-box mechanism (WordPress 5 verified)
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.2 | |
*/ | |
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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment