add afisha
This commit is contained in:
129
inc/afisha.php
Normal file
129
inc/afisha.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
// Регистрация кастомного типа поста
|
||||
function azozh_register_post_type() {
|
||||
register_post_type('afisha_zozh', array(
|
||||
'labels' => array(
|
||||
'name' => 'Афиша ЗОЖ',
|
||||
'singular_name' => 'Событие',
|
||||
'add_new' => 'Добавить событие',
|
||||
'add_new_item' => 'Добавить новое событие',
|
||||
'edit_item' => 'Редактировать событие',
|
||||
'new_item' => 'Новое событие',
|
||||
'view_item' => 'Посмотреть событие',
|
||||
'search_items' => 'Найти событие',
|
||||
'not_found' => 'События не найдены',
|
||||
),
|
||||
'public' => true,
|
||||
'has_archive' => true,
|
||||
'menu_icon' => 'dashicons-calendar-alt',
|
||||
'supports' => array('title', 'editor', 'thumbnail'),
|
||||
'show_in_rest' => true,
|
||||
'rewrite' => array('slug' => 'afisha'),
|
||||
));
|
||||
}
|
||||
add_action('init', 'azozh_register_post_type');
|
||||
|
||||
// Регистрация таксономии "Разделы" (иерархическая, как категории)
|
||||
function azozh_register_taxonomies() {
|
||||
// Разделы (категории): бег, йога, фитнес и т.п.
|
||||
register_taxonomy('afisha_category', 'afisha_zozh', array(
|
||||
'labels' => array(
|
||||
'name' => 'Разделы',
|
||||
'singular_name' => 'Раздел',
|
||||
'search_items' => 'Найти раздел',
|
||||
'all_items' => 'Все разделы',
|
||||
'edit_item' => 'Редактировать раздел',
|
||||
'update_item' => 'Обновить раздел',
|
||||
'add_new_item' => 'Добавить раздел',
|
||||
'new_item_name' => 'Название нового раздела',
|
||||
),
|
||||
'hierarchical' => true,
|
||||
'show_admin_column' => true,
|
||||
'show_in_rest' => true,
|
||||
'rewrite' => array('slug' => 'razdel'),
|
||||
));
|
||||
|
||||
// Компании (неиерархическая, как метки)
|
||||
register_taxonomy('afisha_company', 'afisha_zozh', array(
|
||||
'labels' => array(
|
||||
'name' => 'Компании',
|
||||
'singular_name' => 'Компания',
|
||||
'search_items' => 'Найти компанию',
|
||||
'all_items' => 'Все компании',
|
||||
'edit_item' => 'Редактировать компанию',
|
||||
'update_item' => 'Обновить компанию',
|
||||
'add_new_item' => 'Добавить компанию',
|
||||
'new_item_name' => 'Название компании',
|
||||
'separate_items_with_commas' => 'Разделяйте компании запятыми',
|
||||
'choose_from_most_used' => 'Выбрать из часто используемых',
|
||||
),
|
||||
'hierarchical' => false, // Как метки
|
||||
'show_admin_column' => true,
|
||||
'show_in_rest' => true,
|
||||
'rewrite' => array('slug' => 'kompaniya'),
|
||||
));
|
||||
}
|
||||
add_action('init', 'azozh_register_taxonomies');
|
||||
|
||||
// Добавление meta box для даты события
|
||||
function azozh_add_meta_boxes() {
|
||||
add_meta_box(
|
||||
'azozh_event_details',
|
||||
'Детали события',
|
||||
'azozh_meta_box_html',
|
||||
'afisha_zozh',
|
||||
'normal',
|
||||
'high'
|
||||
);
|
||||
}
|
||||
add_action('add_meta_boxes', 'azozh_add_meta_boxes');
|
||||
|
||||
// HTML meta box
|
||||
function azozh_meta_box_html($post) {
|
||||
wp_nonce_field('azozh_save_meta', 'azozh_nonce');
|
||||
|
||||
$event_date = get_post_meta($post->ID, '_azozh_event_date', true);
|
||||
$press_links = get_post_meta($post->ID, '_azozh_press_links', true);
|
||||
?>
|
||||
<p>
|
||||
<label for="azozh_event_date"><strong>Дата события:</strong></label><br>
|
||||
<input type="date" id="azozh_event_date" name="azozh_event_date"
|
||||
value="<?php echo esc_attr($event_date); ?>"
|
||||
style="width: 100%; max-width: 300px;">
|
||||
</p>
|
||||
<p>
|
||||
<label for="azozh_press_links"><strong>Ссылки на пресс-релизы:</strong></label><br>
|
||||
<textarea id="azozh_press_links" name="azozh_press_links"
|
||||
rows="5" style="width: 100%;"><?php echo esc_textarea($press_links); ?></textarea>
|
||||
<span class="description">Укажите ссылки на пресс-релизы, каждая с новой строки</span>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
// Сохранение meta данных
|
||||
function azozh_save_meta($post_id) {
|
||||
// Проверки
|
||||
if (!isset($_POST['azozh_nonce']) || !wp_verify_nonce($_POST['azozh_nonce'], 'azozh_save_meta')) {
|
||||
return;
|
||||
}
|
||||
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
|
||||
return;
|
||||
}
|
||||
if (!current_user_can('edit_post', $post_id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Сохранение даты события
|
||||
if (isset($_POST['azozh_event_date'])) {
|
||||
update_post_meta($post_id, '_azozh_event_date', sanitize_text_field($_POST['azozh_event_date']));
|
||||
}
|
||||
|
||||
// Сохранение ссылок на пресс-релизы
|
||||
if (isset($_POST['azozh_press_links'])) {
|
||||
update_post_meta($post_id, '_azozh_press_links', sanitize_textarea_field($_POST['azozh_press_links']));
|
||||
}
|
||||
}
|
||||
add_action('save_post_afisha_zozh', 'azozh_save_meta');
|
||||
Reference in New Issue
Block a user