I am trying to add in a website a simple support ticket function using contact form 7 and Flamingo. On the Flamingo admin entries page I need to add a new column (status) and new bulk actions (open, close). After searching on the net I have found some solutions. Bellow you can see the code I am using for now. The new column "satus" is created, the bulk action is also available in the bulk actions drop down, but the handler is never executed (I never get any message in the console) and the status never passes to "opened". Could someone please help me to understand what's happening?
Here is the code:
// Flamingo screen ID: flamingo_page_flamingo_inbound
// Add a new bulk action
add_filter('bulk_actions-flamingo_page_flamingo_inbound', 'my_bulk_action');
function my_bulk_action($bulk_actions) {
debug_to_console( 'Hello console' );
$bulk_actions['change-to-open'] = __('Change to open');
return $bulk_actions;
};
// Handle the new bulk action
add_filter( 'handle_bulk_actions-flamingo_page_flamingo_inbound', 'my_bulk_action_handler', 10, 3 );
function my_bulk_action_handler($redirect_url, $action, $post_ids) {
debug_to_console( 'Running handler' );
if ( $action !== 'change-to-open' ) {
return $redirect_url;
}
foreach ($post_ids as $post_id) {
update_post_meta($post_id, 'status', '1');
}
$redirect_url = add_query_arg('changed-to-open', count($post_ids), $redirect_url);
return $redirect_url;
}
// Admin notices
add_action('admin_notices', 'my_admin_notice');
function my_admin_notice() {
debug_to_console( 'Notice' );
if (!empty($_REQUEST['changed-to-open'])) {
debug_to_console( 'Notice opened' );
$num_changed = (int) $_REQUEST['changed-to-open'];
printf('<div id="message" class="updated notice is-dismissable"><p>' . __('Opened %d posts.', 'txtdomain') . '</p></div>', $num_changed);
}
}
// Create the status column
// (here I need the post type of Flamingo: flamingo_inbound)
add_filter('manage_flamingo_inbound_posts_columns', function($columns) {
return array_merge($columns, ['status' => 'Status']);
});
add_action('manage_flamingo_inbound_posts_custom_column', function($column_key, $post_id) {
if ($column_key == 'status') {
$status = get_post_meta($post_id, 'status', true);
if ($status) {
echo '<span style="color:green;">'; _e('Opened', 'textdomain'); echo '</span>';
} else {
echo '<span style="color:red;">'; _e('Closed', 'textdomain'); echo '</span>';
}
}
}, 10, 2);
// Display messages to the console
function debug_to_console( $data ) {
if ( is_array( $data ) )
$output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
else
$output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";
echo $output;
}
I have seen another thread where someone had a similar issue, the problem was a code error in the handler function. When I delete all the content of the handler function (except the console message) the problems remains. Any idea?
Thanks!
question from:
https://stackoverflow.com/questions/66068193/wordpress-add-custom-bulk-action-to-flamingo 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…