Support » Developing with WordPress » bulk actions action

  • Resolved L D

    (@enochfung)


    Hello, 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' );
    
    • This topic was modified 1 year, 11 months ago by  L D. Reason: added the add_filter that I forgot. Still doesn't work. :(
Viewing 7 replies - 1 through 7 (of 7 total)
  • From a quick look at your code, what is $doaction? Where did you declare it?

    L D

    (@enochfung)

    I have no idea. I was trying to following this example: https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/

    Moderator bcworkz

    (@bcworkz)

    Replace instances of $doaction with $action_name. When you rename a parameter, you have to go through the function and rename all instances of the renamed variable πŸ˜‰

    I’m not sure an echo statement will display from an action handler callback like that. If it does, it won’t be where you would expect it. The callback is intended for changing the posts passed in $post_ids in a manner dictated by $action_name. To properly display a message like “Hello World!”, use the “admin_notices” action like the final example in your referenced “Make” document.

    To test success of the “bulk_actions-edit-post”, you should change at least one of the posts passed in some manner. Perhaps change its status or title. If you just want confirmation the callback fired, use error_log('Hello World!');. An entry will appear in your error logs if the callback fired.

    Echoing out a message is a good debugging technique for normal PHP code that is constructing webpage output. It’s very unreliable when debugging many callbacks because the browser may not be accepting output when the action fires. Error logging should always work.

    Learning a new complex feature like bulk actions can be very bewildering and frustrating. Stick with it, it’ll start making sense eventually πŸ™‚

    L D

    (@enochfung)

    Hi, I made the changes to my code. I managed to get some messages in the error log! xD But not where I wanted it to be… lol Here is my latest version of the code:

    
    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 ) {
    	
    	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…

    (BTW, thanks for the error log trick. Made the debugging much easier!)

    L D

    (@enochfung)

    I noticed an error in the register function. I corrected it (shown below) but it’s still returning once it enters the first conditional in the handler.

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

    it’s still returning once it enters the first conditional in the handler.

    Well yeah, that’s because you’ve got a return statement in there:

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

    (@enochfung)

    No, that is not it. I figured it out. 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. Without those last two parametres, the $action_name that was supposed to be passed is actually ‘null’. Once you add the parametres, the correct value is passed.

    Thanks everyone for the help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘bulk actions action’ is closed to new replies.