Created
July 29, 2022 13:18
-
-
Save nextab/f0a391f6e55accd78ea02810d3689e48 to your computer and use it in GitHub Desktop.
Bulk Edit Optionen in WordPress für Custom Post Type erweitern
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
| #region Add Bulk Edit for Applications | |
| // We are adding custom bulk actions in the WP Admin Backend for the post type "application" | |
| function nxt_custom_bulk_actions($bulk_array) { | |
| $application_status_fields = get_field_object('field_62daaf75105aa'); | |
| foreach($application_status_fields['choices'] as $status_option_key => $status_option_value) { | |
| $bulk_array[$status_option_key] = 'Set application status to ' . $status_option_key; | |
| } | |
| return $bulk_array; | |
| } | |
| add_filter('bulk_actions-edit-application', 'nxt_custom_bulk_actions'); | |
| // Now we start implementing the functionality for the use of the custom bulk edit options defined above | |
| function nxt_bulk_action_handler( $redirect, $doaction, $object_ids ) { | |
| $application_status_fields = get_field_object('field_62daaf75105aa'); | |
| // let's remove our query arg that was added after bulk editing applications first | |
| $redirect = remove_query_arg( ['nxt_application_status_changed', 'nxt_application_updated_status'], $redirect ); | |
| // dynamically check for each possible application status value whether it was selected and update the affected posts | |
| foreach($application_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_62daaf75105aa', $status, $post_id); | |
| } | |
| $redirect = add_query_arg( ['nxt_application_status_changed' => count( $object_ids ), 'nxt_application_updated_status' => $status], $redirect ); | |
| } | |
| } | |
| // do something for each application status bulk action | |
| return $redirect; | |
| } | |
| add_filter('handle_bulk_actions-edit-application', 'nxt_bulk_action_handler', 10, 3); | |
| // Display a status update in the backend after applications have been bulk edited | |
| function nxt_bulk_application_action_notices() { | |
| if ( ! empty( $_REQUEST['nxt_application_status_changed'] ) ) { | |
| // depending on how many applications were changed, create a fitting status update | |
| printf( '<div id="nxt_application_update" class="updated notice is-dismissible"><p>' . | |
| _n( 'Status of %s application has been changed to ' . $_REQUEST["nxt_application_updated_status"] . '.', | |
| 'Status of %s applications has been changed to ' . $_REQUEST["nxt_application_updated_status"] . ".", | |
| intval( $_REQUEST['nxt_application_status_changed'] ) | |
| ) . '</p></div>', intval( $_REQUEST['nxt_application_status_changed'] ) ); | |
| } | |
| } | |
| add_action('admin_notices', 'nxt_bulk_application_action_notices'); | |
| #endregion Add Bulk Edit for Applications |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment