Задача: Добавить возможность отметить все чекбоксы по клику на кнопку в списке таксономий ACF
Решение: Подключим дополнительный js и css для админки.
functions.php
function enqueue_custom_admin_scripts() {
wp_enqueue_script('custom-admin-scripts', get_template_directory_uri() . '/assets/js/custom-admin-scripts.js', array('jquery'), null, true);
wp_enqueue_style('custom-admin-styles', get_template_directory_uri() . '/assets/css/custom-admin-styles.css');
}
add_action('admin_enqueue_scripts', 'enqueue_custom_admin_scripts');
custom-admin-scripts.js
// Добавляем кнопку "Выбрать все" для таксономий
jQuery(document).ready(function ($) {
// Проверяем, что мы находимся на админской странице
if (typeof acf !== "undefined" && acf) {
// Добавляем кнопку "Выбрать все" динамически
$(".acf-taxonomy-field[data-taxonomy=\'brands\'] .categorychecklist-holder > ul, .acf-taxonomy-field[data-taxonomy='organization_cat'] .categorychecklist-holder > ul").each(function () {
$(this).before('<button class="select-all">Выбрать все</button>');
// Проверяем состояние чекбоксов при загрузке страницы
var checkboxes = $(this).find('input[type="checkbox"]');
var button = $(this).prev(".select-all");
if (checkboxes.length === checkboxes.filter(':checked').length) {
button.text("Снять все");
}
});
// Обработчик клика на кнопку "Выбрать все" и "Снять все"
$(document).on("click", ".acf-taxonomy-field .select-all", function (e) {
e.preventDefault();
var checkboxes = $(this).closest(".acf-taxonomy-field").find('input[type="checkbox"]');
checkboxes.prop("checked", !checkboxes.prop("checked"));
// Изменяем текст кнопки в зависимости от состояния чекбоксов
var button = $(this);
if (button.text() === "Выбрать все") {
button.text("Снять все");
} else {
button.text("Выбрать все");
}
});
}
});
custom-admin-styles.css
.select-all {
margin: 10px 10px 0 10px;
padding: 4px 12px;
border: 1px solid #ccd0d4;
border-radius: 3px;
background: transparent;
transition: all 0.3s;
cursor: pointer;
}
.select-all:hover {
border: 1px solid #979b9e;
}
Источники: Чат GPT.