// zarejestruj kolumne
function add_custom_column_to_post_list($columns) {
$columns['your_column_name'] = 'Column Name';
return $columns;
}
add_filter('manage_posts_columns', 'add_custom_column_to_post_list');
//wyswietl w kolumnie taxo
function display_custom_column_content($column, $post_id) {
if ($column === 'your_column_name') {
$terms = wp_get_post_terms($post_id, 'your_taxonomy_name', array("fields" => "all"));
if (!empty($terms) && !is_wp_error($terms)) {
$term_names = array();
foreach ($terms as $term) {
$term_names[] = $term->name;
}
echo '<span class="custom-column-content">' . implode(', ', $term_names) . '</span>';
echo '<button class="button edit-custom-taxonomy" data-post-id="' . $post_id . '">Edit</button>';
} else {
echo '-';
}
}
}
add_action('manage_posts_custom_column', 'display_custom_column_content', 10, 2);
// ajax do edycji
function handle_custom_taxonomy_edit() {
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
$term_value = isset($_POST['term_value']) ? sanitize_text_field($_POST['term_value']) : '';
if ($post_id && $term_value) {
$term = term_exists($term_value, 'your_taxonomy_name');
if ($term) {
wp_set_post_terms($post_id, $term['term_id'], 'your_taxonomy_name');
echo 'success';
} else {
echo 'error';
}
} else {
echo 'error';
}
die();
}
add_action('wp_ajax_custom_taxonomy_edit', 'handle_custom_taxonomy_edit');
// dodaj JS do ajaxa
function custom_taxonomy_edit_script() {
?>
<script>
jQuery(document).ready(function($) {
$('.edit-custom-taxonomy').on('click', function() {
var post_id = $(this).data('post-id');
var term_value = prompt('Enter new taxonomy value:');
if (term_value !== null && term_value !== '') {
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'custom_taxonomy_edit',
post_id: post_id,
term_value: term_value
},
success: function(response) {
if (response === 'success') {
alert('Taxonomy value updated successfully.');
location.reload();
} else {
alert('Error occurred while updating taxonomy value.');
}
}
});
}
});
});
</script>
<?php
}
add_action('admin_footer-edit.php', 'custom_taxonomy_edit_script');
Dodaj komentarz