• 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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Read the code of plugins that do this. (or read the core code)

    Moderator bcworkz

    (@bcworkz)

    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

    (@benlong)

    Thanks @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

    (@bcworkz)

    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.