Add Snippet To Project
In managing an e-commerce site, sometimes you might find it necessary to limit certain actions to prevent accidental mishaps. One such action could be deleting a payment option. It may sound daunting, but the reality is quite the opposite. It’s nowhere near as complex as it sounds. In this guide, we will walk you through a step-by-step process on how to remove the delete option from the payment list quick action in your website’s backend. Whether you’re a seasoned web developer or a beginner, this article will come in handy. Let’s dive in.
add_filter( 'post_row_actions', 'wpturbo_remove_delete_quick_action', 10, 2 );
function wpturbo_remove_delete_quick_action( $actions, $post ) {
if ( $post->post_type == 'shop_order' ) {
unset( $actions['delete'] );
}
return $actions;
}
Let’s break down what the code snippet does to remove the ‘Delete’ quick action option from the payment list in WordPress.
The first line of the snippet uses the add_filter
function, which is a WordPress-specific function that allows you to ‘hook’ your own functionality onto an existing bit of functionality. In this case, the add_filter
function is used to modify the list of quick action links for each row in a post list table in the admin area.
add_filter( 'post_row_actions', 'wpturbo_remove_delete_quick_action', 10, 2 );
The first argument, ‘post_row_actions’, is the filter hook that WordPress calls. The second argument, ‘wpturbo_remove_delete_quick_action’, is the name of the function that we will define to customize the actions.
The following lines define the wpturbo_remove_delete_quick_action
function. The function accepts two parameters: $actions
, which is an array containing the action links for the post, and $post
, which represents the current post object.
function wpturbo_remove_delete_quick_action( $actions, $post ) {
if ( $post->post_type == 'shop_order' ) {
unset( $actions['delete'] );
}
return $actions;
}
Inside this function, we check if the $post->post_type
is ‘shop_order’. ‘shop_order’ is the post type for WooCommerce orders, so this check confirms that we’re looking at the payment list specifically.
If the post type is indeed ‘shop_order’, then we use the unset
function to remove the ‘delete’ option from the $actions
array. unset
is a PHP function that destroys a specified variable. In this case, it’s being used to remove the ‘delete’ element from the $actions
array, which has the effect of removing the ‘Delete’ quick action link from the payment list.
Finally, the function returns $actions
, which now includes all the original action links for the post, minus the ‘delete’ option. This updated array will be used by WordPress to render the list of quick action links for each payment.
Therefore, anyone with ‘shop_order’ post type cannot delete it from the quick action menu, ensuring a higher level of data integrity in your WooCommerce store. It is also important to mention that this doesn’t remove a user’s capability to delete the post, just the quick action link.