0

I’m learning how to make plugins and I’m running into some difficulty. I’m trying to modify the bulk actions dropdown under the post and pages parts of admin. I managed to get the dropdown to display a new name but I can’t get it to do anything – not even output a simple “Hello World”. I’m quite lost and was hoping someone could help me. Here’s what I have currently:

function mycustom_bulk_action( $bulk_actions ) {

    $bulk_actions[ 'mycustom_bulk' ] = __( 'My Custom Bulk Action', 'mycustom_posts_status' );
    return $bulk_actions;

}
add_filter( 'bulk_actions-edit-post', 'mycustom_bulk_action' );
add_filter( 'bulk_actions-edit-page', 'mycustom_bulk_action' );

function mycustom_bulk_action_handler( $redirect_to, $action_name, $post_ids ) {

    if ( $doaction !== 'mycustom_bulk_action' ) {
        return $redirect_to;
    }

    foreach ( $post_ids as $post ) {
        echo "hello world";
    }

    return $redirect_to;
}
add_filter( 'handle_bulk_actions-edit-post', 'mycustom_bulk_action_handler' );

Update 1: I made the changes to my code. I managed to get some messages in the error log but not where I wanted it to be. Here is my latest version of the code:

function mycustom_bulk_action( $bulk_actions ) {

    $bulk_actions[ 'mycustom_bulk' ] = __( 'My Custom Bulk Action', 'mycustom_bulk' );
    return $bulk_actions;

}  
add_filter( 'bulk_actions-edit-post', 'mycustom_bulk_action' );  
add_filter( 'bulk_actions-edit-page', 'mycustom_bulk_action' );  

function mycustom_bulk_action_handler( $redirect_to, $action_name, $post_ids ) {

    error_log('First World!');

    if ( $action_name !== 'mycustom_bulk' ) {
        error_log('Second World!');
        return $redirect_to;
    }

    foreach ( $post_ids as $post ) {
        error_log('Hello World!');
    }

    error_log('Last World!');
    return $redirect_to;
}  
add_filter( 'handle_bulk_actions-edit-post', 'mycustom_bulk_action_handler' );

And here is the error log messages:

[28-Nov-2017 19:20:00 UTC] First World!
[28-Nov-2017 19:20:00 UTC] Second World!

So it appears that it never continued past the first conditional statement. Why is that? From what I can see, if I selected and clicked “apply” in the bulk actions dropdown, it should pass “mycustom_bulk” to $action_name, right? So it shouldn’t enter the first conditional? But it does…

Update 2: I figured out why it kept going into the first conditional. Turns out, the value passed to $action_name is 'null'. The reason is because the handler filter must include the last two parametres. So the correct code for it to work is:

add_filter( 'handle_bulk_actions-edit-post', 'mycustom_bulk_action_handler', 10, 3 );

Once that is added, then everything works and it no longer enters the first conditional. Once you add the parametres, the correct value is passed to $action_name.

0

The user is redirected away after the bulk action is performed, so any output will be on the page you're quick redirected away from.

For example, if you're bulk eiditng pages, you'll start on a URL like:

http://example.com/wp-admin/edit.php?post_type=page&paged=1

Then you perform a bulk action. Once it's done you'll end up on:

http://example.com/wp-admin/edit.php?post_type=page&paged=1

The same page, but with a confirmation message. However, in between you'r browser will go to:

http://example.com/wp-admin/edit.php?s=&post_status=all&post_type=page&_wpnonce=06d56450b9&_wp_http_referer=%2Ftheme1%2Fwp-admin%2Fedit.php%3Fpost_type%3Dpage%26paged%3D1&action=edit&m=0&paged=1&post_author=1&post_parent=-1&page_template=-1&comment_status=&_status=-1&bulk_edit=Update&post_view=list&screen=edit-page&post%5B%5D=5&post%5B%5D=6&post%5B%5D=7&post%5B%5D=2&post%5B%5D=4&action2=-1

This is where the bulk action is processed, and where your output would go. But once the action is complete you're redirected back to where you came from, so you won't have any opportunity to see anything printed to the screen.

If you're just trying to see if the action is being performed, try updating the posts whose IDs are passed with $post_ids, updating an option in the database, or sending an email. Printing stuff to the page isn't going to be useful because you're not going to see that page.

  • Hi, I made changes to my code (and I corrected some things as well). But it's not behaving as I expected it to. Not sure what I'm doing wrong with the first conditional statement in the handler. Do you have any suggestions on what I might be doing wrong? – starchy Nov 28 '17 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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