How to add custom actions to Bulk Actions – Learn WordPress from a to z

Tutorials 0 lượt xem

More than just deleting and editing, you want to add a custom action to Bulk Actions? So this article will help you do just that.

Here is the result you will get:

Add custom actions

In this article, I will create an action that changes the status of posts in bulk. More specifically, I want to change the status of the posts to “Draft”.

You add the code below to the file functions.php of the child or child theme.

add_filter( 'bulk_actions-edit-post', 'hk_custom_bulk_actions' );
function hk_custom_bulk_actions( $bulk_array ) {
	$bulk_array['hk_make_draft'] = 'Sửa thành Bản nháp';
	return $bulk_array;
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
</div>

In the above code, I use hook bulk_action-eidt-{$screen}  If you don’t want to create custom action for Post, change the value of $screen .

If it’s a Page, $screen will be page . More specifically bulk_actions-edit-page . Same goes for other custom post types.

Action handling

After successfully adding the above action Bulk Actions. The next thing you have to do is handle that action. In this part, I will use the filter hook handle_bulk_actions-{$screen} . Similar to the above, you must change the variable $screen to suit your needs.

Add the code below below the code above.

function hk_bulk_action_handler( $redirect, $doaction, $object_ids ) {
	$redirect = remove_query_arg( array( 'hk_make_draft_done' ), $redirect );

	if ( $doaction == 'hk_make_draft' ) {

		foreach ( $object_ids as $post_id ) {
			wp_update_post( array(
				'ID' =&gt; $post_id,
				'post_status' =&gt; 'draft' // set status
			) );
		}

		$redirect = add_query_arg(
			'hk_make_draft_done',
			count( $object_ids ),
		$redirect );

	}

	return $redirect;
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span>
</div>

Notice after taking action

This is the final step, you will create a custom message after your action is applied. Similar to the two above, add the following code to the file functions.php .

add_action( 'admin_notices', 'hk_bulk_action_notices' );
function hk_bulk_action_notices() {
	if ( ! empty( $_REQUEST['hk_make_draft_done'] ) ) {
		echo '<div id="message" class="updated notice is-dismissible">
			<p>Trạng thái bài viết đã được cập nhập.</p>
		</div>';
	}
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span>
</div>

Here is the result you will get:

Epilogue

I hope the above article will help you better in managing WordPress posts. If this article was helpful and saved your time, please help me share it. Also if you are interested in similar topics, read other WordPress Tips articles and follow Fanpage so you don’t miss new posts from me.

Bài viết liên quan