Created
July 19, 2024 13:07
-
-
Save dantetesta/2b12269a015924eaaa97c61b03698ef5 to your computer and use it in GitHub Desktop.
Habilita um botao pra alterar uma taxonomia via admin columns.php
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 | |
| // Adicionar a coluna personalizada no CPT "clubes" | |
| add_filter('manage_clubes_posts_columns', 'customize_clubes_columns'); | |
| function customize_clubes_columns($columns) { | |
| $columns['destaque'] = __('Destaque', 'textdomain'); | |
| return $columns; | |
| } | |
| // Preencher a coluna com um label e um botão para marcar/desmarcar o termo "destaque" | |
| add_action('manage_clubes_posts_custom_column', 'customize_clubes_column_content', 10, 2); | |
| function customize_clubes_column_content($column_name, $post_id) { | |
| if ($column_name === 'destaque') { | |
| $is_destaque = has_term(15, 'clube', $post_id); | |
| $nonce = wp_create_nonce('toggle_destaque_nonce'); | |
| $url = admin_url('admin.php?action=toggle_destaque&post_id=' . $post_id . '&nonce=' . $nonce); | |
| // Label | |
| $label_color = $is_destaque ? 'green' : 'red'; | |
| $label_text = $is_destaque ? 'Destacado' : 'Não destacado'; | |
| echo '<span style="font-size:16px; font-weight:bold; color: ' . $label_color . ';">' . $label_text . '</span><br>'; | |
| // Botão | |
| echo '<a href="' . $url . '" class="button button-primary">Alterar</a>'; | |
| } | |
| } | |
| // Manipular a mudança de estado do termo "destaque" | |
| add_action('admin_init', 'toggle_destaque'); | |
| function toggle_destaque() { | |
| if (!isset($_GET['action']) || $_GET['action'] !== 'toggle_destaque') { | |
| return; | |
| } | |
| if (!isset($_GET['post_id']) || !isset($_GET['nonce']) || !wp_verify_nonce($_GET['nonce'], 'toggle_destaque_nonce')) { | |
| wp_die('Ação inválida.'); | |
| } | |
| $post_id = intval($_GET['post_id']); | |
| $is_destaque = has_term(15, 'clube', $post_id); | |
| if ($is_destaque) { | |
| wp_remove_object_terms($post_id, 15, 'clube'); | |
| } else { | |
| wp_set_object_terms($post_id, array(15), 'clube', true); | |
| } | |
| // Redirecionar de volta para a página original | |
| wp_redirect(wp_get_referer()); | |
| exit; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment