Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nextab/d2fe594b8d392a262a3bb76088ac568b to your computer and use it in GitHub Desktop.

Select an option

Save nextab/d2fe594b8d392a262a3bb76088ac568b to your computer and use it in GitHub Desktop.
In this example, we're using the custom post type "message" that is being used for a front end form powered by the Plugin "Advanced Custom Fields" to generate custom posts in our backend. We have now added a functionality to bulk edit the message status.
// We are adding custom bulk actions in the WP Admin Backend for the post type "message"
function dc_custom_bulk_actions( $bulk_array ) {
$message_status_fields = get_field_object('field_5f3ba86a9409e');
// echo '<!-- nxt debug --><pre>'; print_r($message_status_fields); echo '</pre>';
foreach($message_status_fields['choices'] as $status_option_key => $status_option_value) {
$bulk_array[$status_option_key] = 'Set message status to ' . $status_option_key;
}
return $bulk_array;
}
add_filter( 'bulk_actions-edit-message', 'dc_custom_bulk_actions' );
// Now we start implementing the functionality for the use of the custom bulk edit options defined above
add_filter( 'handle_bulk_actions-edit-message', 'dc_bulk_action_handler', 10, 3 );
function dc_bulk_action_handler( $redirect, $doaction, $object_ids ) {
$message_status_fields = get_field_object('field_5f3ba86a9409e');
// let's remove our query arg that was added after bulk editing messages first
$redirect = remove_query_arg( ['dc_message_status_changed', 'dc_message_updated_status'], $redirect );
// dynamically check for each possible message status value whether it was selected and update the affected posts
foreach($message_status_fields['choices'] as $status => $status_label) {
if ( $doaction == $status ) {
foreach ( $object_ids as $post_id ) {
// update_post_meta( $post_id, 'product_price', 1000 );
update_field('field_5f3ba86a9409e', $status, $post_id);
}
$redirect = add_query_arg( ['dc_message_status_changed' => count( $object_ids ), 'dc_message_updated_status' => $status], $redirect );
}
}
// do something for each message status bulk action
return $redirect;
}
// Display a status update in the backend after messages have been bulk edited
add_action( 'admin_notices', 'dc_bulk_message_action_notices' );
function dc_bulk_message_action_notices() {
if ( ! empty( $_REQUEST['dc_message_status_changed'] ) ) {
// depending on how many messages were changed, create a fitting status update
printf( '<div id="dc_message_update" class="updated notice is-dismissible"><p>' .
_n( 'Status of %s message has been changed to ' . $_REQUEST["dc_message_updated_status"] . '.',
'Status of %s messages has been changed to ' . $_REQUEST["dc_message_updated_status"] . ".",
intval( $_REQUEST['dc_message_status_changed'] )
) . '</p></div>', intval( $_REQUEST['dc_message_status_changed'] ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment