Custom Fields in Bulk Edit

Not so many tutorials ago I published my tutorial about custom fields in quick edit. The current one is super-similar actually, but allows to add fields to Bulk actions – Edit.

Our goal here is to add a couple fields there (if you’re are looking for information about bulk actions, then it is a wrong tut for you, here is the right one).

That’s what we are going to create here:

How to add custom fields in WordPress bulk edit
We added a text field and a checkbox field.

1. Add a Column for Every Field

Actually we implemented the same thing completely in the Quick Edit tutorial, so please go there and copy the whole code from Step 1 to your current theme functions.php file.

2. Using bulk_edit_custom_box to Make the Fields Appear in Bulk Edit

Another fun story is that you have to go to Quick Edit tutorial again and copy the whole function misha_quick_edit_fields() from Step 2 of the tutorial. But you do not have to copy the action hook because we are going to use another one here.

add_action( 'bulk_edit_custom_box',  'misha_quick_edit_fields', 10, 2 );

3. Save Fields

That would be so funny guys, if I would ask you to copy one more step from the Quick Edit tut, but no. We are still going to use the same save_post action hook but it is a little bit different here this time – we are going to use another nonce check and get form values from $_REQUEST, not $_POST. One more thing about nonce check – we are using bulk-posts action here for Posts, for Pages it would be bulk-pages etc.

add_action( 'save_post', 'misha_bulk_edit_save' );

function misha_bulk_edit_save( $post_id ){

	// check bulk edit nonce
	if ( ! wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'bulk-posts' ) ) {
		return;
	}

	// update the price
	$price = ! empty( $_REQUEST[ 'price' ] ) ? absint( $_REQUEST[ 'price' ] ) : 0;
 	update_post_meta( $post_id, 'product_price', $price );

	// update checkbox
	$featured = ( isset( $_REQUEST[ 'featured' ] ) && 'on' == $_REQUEST[ 'featured' ] ) ? 'yes' : 'no';
	update_post_meta( $post_id, 'product_featured', $featured );

}

Of course if you’re adding custom fields to bulk edit and quick edit at the same time, you can make the function work for both of them.

That’s all, no JavaScript code this time.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter