Sandeep Kumar Mishra
Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.
Using WordPress hooks can be a game-changer for your development process. Let’s dive into the media_row_actions filter which allows you to modify the action links for each attachment in the Media list table. This hook is quite handy when you need to customize media actions without altering core files.
To get started with the media_row_actions filter, you first need to register it using add_filter. You can do this in your theme’s functions.php file or in a custom WordPress plugin. At WePlugins, we always recommend creating a custom plugin for hooks to avoid issues when updating your theme.
Below are three live examples demonstrating how to use the media_row_actions filter.
This example shows how to modify the media row actions by adding a new action link.
function weplugins_modify_media_row_actions($actions, $post, $detached) {
// Add a custom action link
$actions['custom_action'] = 'Custom Action';
return $actions;
}
add_filter('media_row_actions', 'weplugins_modify_media_row_actions', 10, 3);
In this example, we’ll add a conditional action link that only appears for image attachments.
function weplugins_modify_media_row_actions_conditional($actions, $post, $detached) {
if ($post->post_mime_type == 'image/jpeg') {
$actions['custom_image_action'] = 'Custom Image Action';
}
return $actions;
}
add_filter('media_row_actions', 'weplugins_modify_media_row_actions_conditional', 10, 3);
Sometimes you might need to remove a previously added action. This example shows how to do that.
function weplugins_modify_media_row_actions_remove($actions, $post, $detached) {
// Remove a custom action link
unset($actions['custom_action']);
return $actions;
}
add_filter('media_row_actions', 'weplugins_modify_media_row_actions_remove', 10, 3);
If you need to remove a registered hook, use the remove_filter function as shown below:
remove_filter('media_row_actions', 'weplugins_modify_media_row_actions', 10, 3);
Be sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.
If you’re having any trouble using this hook or need customizations, please contact our team. We’re here to help!
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.