Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to add a custom bulk action to a custom post type. I came across the filter bulk_actions-screenid, which according to its documentation, would do exactly as I wish. However, after about two hours of debugging I found the following comment // This filter can currently only be used to remove actions. on line 278 of class-wp-list-table.php - great!

I figured I could hack it by using jQuery to inject the action as an option

/**
 * Hack to add a custom bulk action. 
 */
public function admin_footer() {
    if($_GET['post_type'] != self::POST_TYPE) return;
    ?> 
    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('<option>').val('create_invoice').text('Bill').appendTo("select[name='action']");
        });
    </script>
    <?php
}

This works. The action now appears in the bulk actions menu. I was under the assumption I could then add some logic into admin_init to do the necessary processing - however, it appears that create_invoice is never posted. Is there something I'm doing wrong?

=== UPDATE ===

I updated the code to use the load-* hook. When I apply the bulk action on users.php - I see create_invoice is passed through the request. However, on edit.php create_invoice is never printed.

function a39x2_admin_footer() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('<option>').val('create_invoice').text('Bill').appendTo("select[name='action']");
            jQuery('<option>').val('create_invoice').text('Bill').appendTo("select[name='action2']");
        });
    </script>
    <?php
}
add_action('admin_footer', 'a39x2_admin_footer');


function a39x2_load() {
    echo "<pre>" . print_r($_REQUEST, true) . "</pre>";
}
add_action('load-edit.php', 'a39x2_load');
add_action('load-users.php', 'a39x2_load');
share|improve this question
1  
May be this answer will help someone in the future. Here is a way to do it: Add a WordPress Custom Bulk Action –  Ayoub Mar 24 '13 at 23:19
add comment

1 Answer

up vote 6 down vote accepted

The bulk_actions-* filter doesn't allow you to add custom bulk actions precisely because it's tricky to then add a handler for a that action. Updated the Codex.

I would suggest using the load-* action instead of admin_init. Note that you have to do all the security checks like check_ajax_referrer() and current_user_can().

share|improve this answer
    
Thanks! The load-* hook appears to be working. Also, thanks for updating the codex and reopening the trac ticket. –  v0idless Sep 29 '11 at 16:16
1  
I may have prematurely accepted the answer. I updated the code to use the load-* hook. When I apply the bulk action on users.php - I see create_invoice is passed through the request. However, on edit.php create_invoice is never printed. –  v0idless Sep 29 '11 at 16:40
    
Try adding an exit; call after echo "<pre>" . print_r($_REQUEST, true) . "</pre>"; –  scribu Sep 29 '11 at 17:22
    
Ah, okay. That worked. Thanks! –  v0idless Sep 29 '11 at 21:15
    
I think there is a better action hook than load-* : the admin_action_* (admin_action_create_invoice in your case). It is called just after the load-* hook, and only when the custom bulk action is triggered. –  Fabien Quatravaux Feb 8 at 17:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.