<?php
// ===== 1. Добавляем метабокс с галочкой в редактор =====
add_action('add_meta_boxes', 'add_custom_links_meta_box');
function add_custom_links_meta_box() {
add_meta_box(
'custom_links_hide', // ID
'Блок ссылок под статьей', // Заголовок
'custom_links_meta_box_callback', // Функция вывода
'post', // Где показывать (post, page, custom post type)
'side', // Позиция (side, normal, advanced)
'default'
);
}
// Выводим чекбокс в метабоксе
function custom_links_meta_box_callback($post) {
// Добавляем nonce для безопасности
wp_nonce_field('custom_links_meta_box', 'custom_links_meta_box_nonce');
// Получаем сохранённое значение
$hide_block = get_post_meta($post->ID, '_hide_custom_links_block', true);
?>
<p>
<label>
<input type="checkbox" name="hide_custom_links_block" value="1" <?php checked($hide_block, 1); ?> />
<strong>Не показывать блок ссылок</strong>
</label>
</p>
<p style="color: #666; font-size: 13px; margin-top: 10px;">
Отметьте, чтобы скрыть блок "Написать в личку" и "Создать блог" под этой статьёй.
</p>
<?php
}
// ===== 2. Сохраняем значение чекбокса =====
add_action('save_post', 'save_custom_links_meta_box');
function save_custom_links_meta_box($post_id) {
// Проверяем nonce
if (!isset($_POST['custom_links_meta_box_nonce']) ||
!wp_verify_nonce($_POST['custom_links_meta_box_nonce'], 'custom_links_meta_box')) {
return;
}
// Проверяем автосохранение
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Проверяем права
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Сохраняем значение
if (isset($_POST['hide_custom_links_block'])) {
update_post_meta($post_id, '_hide_custom_links_block', 1);
} else {
delete_post_meta($post_id, '_hide_custom_links_block');
}
}
// ===== 3. Выводим блок (с учётом галочки) =====
add_filter('the_content', 'custom_links_after_post');
function custom_links_after_post($content) {
// Показываем только на страницах одиночных записей (single)
if (is_single()) {
global $post;
// Проверяем, отмечена ли галочка "Не показывать"
$hide_block = get_post_meta($post->ID, '_hide_custom_links_block', true);
// Если галочка НЕ отмечена — показываем блок
if (!$hide_block) {
// HTML-код блока
$custom_block = '
<div style="
margin-top: 30px;
padding: 10px 15px;
background: #fffb7f2b;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
border-radius: 12px;
border: 1px dashed #ccc;
">
<a href="https://t.me/denispovaga" style="
text-decoration: none;
font-weight: bold;
font-size: 24px;
padding: 5px 0;
">📩 Написать в личку</a>
<a href="https://denispovaga.ru/blog.html" style="
text-decoration: none;
font-weight: bold;
font-size: 24px;
padding: 5px 0;
">📝 Создать блог</a>
</div>
';
// Добавляем блок в конец содержимого
$content .= $custom_block;
}
}
return $content;
}
?>