How to add personalized group actions to WordPress list tables?

I am developing a plugin for what I have for users of the blacklist, so I need to display another element of the Blacklist drop-down list in the Mass actions drop-down list on the Users page after the Delete option. But I can’t see where these two actions come from, or as a blacklist of a specific user.

My idea is to add another is_blacklisted field to the user table as Boolean with the default value of false and when applied the Blacklist changes to true . Any other thoughts?

0
source share
2 answers

There is a filter , but it is only useful for removing bulk actions.

From this question, WPSE answer and comments , there is the following workaround: add a custom option to the dropdown using jQuery and go to admin_action_$your-action to catch the view.

The admin_footer-$current_page hook is used to print our JavaScript on a specific admin page (configurable for use on other screens).

 add_action( 'admin_footer-users.php', 'bulk_footer_so_23541269' ); add_action( 'admin_action_black_list', 'bulk_request_so_23541269' ); function bulk_footer_so_23541269() { # global $typenow; if( $typenow != 'page' ) return; // if used on edit.php screen ?> <script type="text/javascript"> jQuery(document).ready(function($) { $('<option>').val('black_list').text('Black list') .appendTo("select[name='action'], select[name='action2']"); }); </script> <?php } function bulk_request_so_23541269() { # Array with the selected User IDs wp_die( '<pre>' . print_r( $_REQUEST['users'], true ) . '</pre>' ); // $_REQUEST['post'] if used on edit.php screen } 

Your doubt about blocking the user deserves another question, but I started the research first.

+4
source

Corresponding support with add_filter( 'bulk_actions-screenid', 'register_my_bulk_actions' ) comes in Wordpress 4.7.

Quote ad:

To add a parameter to the Bulk Actions HTML drop-down element, register the bulk_actions-{screen_id} filter bulk_actions-{screen_id} , which adds the new parameter to the array. Replace {screen_id} with the admin screen identifier to suggest a bulk action.

To add the bulk email action for Eric, we could use the following code:

 add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' ); function register_my_bulk_actions($bulk_actions) { $bulk_actions['email_to_eric'] = __( 'Email to Eric', 'email_to_eric'); return $bulk_actions; } 

To handle the bulk action feed, register the handle_bulk_actions-{screen_id} filter handle_bulk_actions-{screen_id} for the corresponding screen. The filter expects the redirect URL to be changed, so be sure to change the passed $redirect_url . This allows us to transfer the success or failure state to the next request to display a notification to the user. Other callback arguments will differ depending on the screen here to include context-sensitive data.

To add a bulk action handler to send emails, we could use the following code:

 add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 ); function my_bulk_action_handler( $redirect_to, $doaction, $post_ids ) { if ( $doaction !== 'email_to_eric' ) { return $redirect_to; } foreach ( $post_ids as $post_id ) { // Perform action for each post. } $redirect_to = add_query_arg( 'bulk_emailed_posts', count( $post_ids ), $redirect_to ); return $redirect_to; } 

Display notifications. We could use existing notification hooks to let the user know what happened, depending on the state that we set in the URL:

 add_action( 'admin_notices', 'my_bulk_action_admin_notice' ); function my_bulk_action_admin_notice() { if ( ! empty( $_REQUEST['bulk_emailed_posts'] ) ) { $emailed_count = intval( $_REQUEST['bulk_emailed_posts'] ); printf( '<div id="message" class="updated fade">' . _n( 'Emailed %s post to Eric.', 'Emailed %s posts to Eric.', $emailed_count, 'email_to_eric' ) . '</div>', $emailed_count ); } } 
+3
source

Source: https://habr.com/ru/post/970186/


All Articles