Title: Creating an Admin Bar button that does multiple things
Last modified: October 20, 2019

---

# Creating an Admin Bar button that does multiple things

 *  [benlong](https://wordpress.org/support/users/benlong/)
 * (@benlong)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/creating-an-admin-bar-button-that-does-multiple-things/)
 * Hi, using some code that I found laying around the web, I’ve added a Move To 
   Trash button to my admin bar. It works fine – when I click on it, it does in 
   fact delete the current post. However, there are some files associated with each
   post that I would also like to delete. My Move To Trash button uses an href argument
   to call get_delete_post_link:
 *     ```
       { $wp_admin_bar->add_node(
                       array( 'id' => 'delete',
                             'title' => __('Move to Trash'),
                             'href' => get_delete_post_link($current_object->term_id) ) );
       ```
   
 * What I don’t know how to do is execute additional commands. Is there a way that
   I can get this link to execute a subroutine of some kind, rather than a single
   href link?
 * Thanks!
    -  This topic was modified 6 years, 7 months ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/creating-an-admin-bar-button-that-does-multiple-things/#post-12047179)
 * Read the code of plugins that do this. (or read the core code)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/creating-an-admin-bar-button-that-does-multiple-things/#post-12047668)
 * I suggest you create a PHP function that does everything you need to do, using
   the post ID passed as an URL parameter. Add the function as an action callback,
   the action tag will be dependent upon the “action” URL parameter passed and which
   way you choose to cause the action to be fired. You can either submit the request
   via Ajax, or submit through _/wp-admin/admin-post.php_.
 * For security, be sure you verify a nonce and verify current user capability.
 *  Thread Starter [benlong](https://wordpress.org/support/users/benlong/)
 * (@benlong)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/creating-an-admin-bar-button-that-does-multiple-things/#post-12052161)
 * Thanks [@bcworkz](https://wordpress.org/support/users/bcworkz/), I had started
   to piece some of that together by researching some more, but you laid it out 
   very clearly. I have no experience with AJAX, so this is gonna take some work,
   but I appreciate the help.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/creating-an-admin-bar-button-that-does-multiple-things/#post-12054828)
 * You’re welcome. Unless you’re keen to learn Ajax in the context of WP, I’d suggest
   sending the request through _admin-post.php_. GET requests work equally well 
   despite the filename. No JS or jQuery is required this way. I mentioned Ajax 
   more for completeness rather than suggesting you should use it. The menu’s URL
   could be obtained by something like:
 *     ```
       $nonce = wp_create_nonce('my_delete');
       $url = admin_url( "admin-post.php?action=my_delete&del_id={$current_object->term_id}&_wp_nonce=$nonce", 'https' );
       ```
   
 * Then add your callback with `add_action('admin_post_my_delete', 'my_post_delete');`
   
   Your my_post_delete() callback function would verify the passed nonce, check 
   user capability, then if all is valid, delete the post whose ID is passed in `
   $_GET['del_id']`. Be sure the passed value is an integer before using it to avoid
   SQL injection attacks.
 * This will result in a new page being loaded. If that is undesirable, you do need
   Ajax. I’d suggest the new page look similar to the WP update progress pages, 
   with a link back to the referring page at the end. But you’re free to do as you
   please 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Creating an Admin Bar button that does multiple things’ is closed to 
new replies.

## Tags

 * [admin_bar](https://wordpress.org/support/topic-tag/admin_bar/)
 * [move to trash](https://wordpress.org/support/topic-tag/move-to-trash/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [6 years, 6 months ago](https://wordpress.org/support/topic/creating-an-admin-bar-button-that-does-multiple-things/#post-12054828)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
