Adding Custom Action Button to Post Type Table in WordPress

WordPress provides a user-friendly interface to manage various content types through the admin dashboard. One such content type is the custom post type, allowing users to create and manage their own content. By default, WordPress provides actions like ‘Edit’ and ‘Trash’ in the post type table that allow users to perform basic actions on their content. However, there may be times when you want to add custom actions to perform specific tasks on your custom post type. In this article, we will explore how to add a custom button action in a post type table in WordPress.

Step 1: Define Custom Column

The first step is to define the custom column that will contain the custom action button. To do this, we will use the manage_{$post_type}_posts_columns filter. Here is the code to define the custom column:

function wpd_add_button_column($columns){
    $columns['wpd_button'] = 'WPD Button';
    return $columns;
}
add_filter( 'manage_product_posts_columns', 'wpd_add_button_column' );

In this code, we are using the manage_product_posts_columns filter to add a custom column named ‘wpd_button’ to the post type table.

Step 2: Define Custom Action

The next step is to define the custom action that will be performed when the button is clicked. To do this, we will use the manage_{$post_type}_posts_custom_column action. Here is the code to define the custom action:

function wpd_product_column($column, $post_id){
    switch ( $column ) {
        case 'wpd_button' :
            $url = add_query_arg(
                array(
                    'post_id' => $post_id,
                    'wpd_button' =>'wpd_action',
                )
            );
            printf( '<a href="%s" class="button">%s</a>',esc_url( $url ), esc_attr( __( 'Button','wp-daily' ) ) );
            break;
        
    }
}
add_action( 'manage_product_posts_custom_column', 'wpd_product_column', 10, 2  );

In this code, we are using the manage_product_posts_custom_column action to define the custom action that will be performed when the button is clicked. In this example, we are simply adding a button to the ‘wpd_button’ column with the label ‘Button’.

Step 3: Define Custom Action Handler

Now, we need to define the custom action that our button will perform. We can use the “admin_init” action to handle the button click event. In the following code snippet, we are checking if the “wpd_button” parameter is set to “wpd_action”. If it is, we get the current post object and perform some action on it. If the action is successful, we set the “wpd_error_update” parameter to a success message. Otherwise, we set it to a failure message.

function wpd_handle_action(){
	if ( !isset( $_REQUEST['wpd_button'] ) || 'wpd_action' != $_REQUEST['wpd_button']  ) {
		return;
	}
	$product = wc_get_product(absint($_REQUEST['post_id']));
	$bs_reg_errors = new WP_Error;
	try {
		//some actions here
		$response = true;
		if ($response){
			$bs_reg_errors->add( 'wpd_error_update', 'Success!' );
		}else{
			$bs_reg_errors->add( 'wpd_error_update', 'Fail!' );
		}
	}catch (Exception $exception){
		$bs_reg_errors->add( 'wpd_error_update', $exception->getMessage() );
	}
	$_REQUEST['wpd_error_update'] = $bs_reg_errors->get_error_message();
	return  add_action( 'admin_notices', 'wpd_display_admin_notice' );
}
add_action( 'admin_init', 'wpd_handle_action' );

Step 4: Display Admin Notice

After defining the custom action and handling it, it is important to provide feedback to the user about the result of the action. This can be done by displaying an admin notice.

WordPress provides an admin_notices action that can be used to display a notice on the admin screen. The wpd_display_admin_notice function will be hooked to this action to display the notice.

Here’s the code for displaying the admin notice:

function wpd_display_admin_notice()
{
    if (get_current_screen()->id != 'edit-product') {
        return;
    }
    ?>

    <div class="notice notice-success is-dismissible">
        <p><?php echo $_REQUEST['wpd_error_update'] ?? ''?></p>
    </div>

    <?php
}

add_action('admin_notices', 'wpd_display_admin_notice');

This function checks if the current screen is the product post type screen. If it is, then it displays a success or failure notice depending on the result of the action.

The is-dismissible class adds a dismiss button to the notice, allowing the user to hide it.

Conclusion

In this article, we have seen how to add a custom action button to the posts table in WordPress. We have used the manage_{$post_type}_posts_columns filter to add a new column to the table and the manage_{$post_type}_posts_custom_column action to display the custom button in the new column.

We have also seen how to handle the custom action using the admin_post and admin_init hooks and how to display an admin notice to provide feedback to the user.

By using these techniques, you can easily extend the functionality of the posts table and provide your users with a more convenient and powerful interface for managing their content.

Leave a Comment

Your email address will not be published. Required fields are marked *