Adding custom actions to users in WordPress

These last weeks we have worked on a WordPress project that required adding a functionality to notify users of the site in a personalized and simple way for the person who is going to be responsible for the administration of the site.

Basically it consisted of sending an SMS to some users to notify them certain contents. The text to be sent via SMS is the same over time, so the client only needs to send that SMS to users who need to receive it without having to enter each of their profiles for it. It needs to be an agile and simple task.

To do this we have chosen to include in the row of quick actions of each user a new link, which performs this new action of sending the SMS. We will not go into detail here, because basically the process that follows the function when clicking is to make a call to the API of the SMS sending platform so that it is made, and the configuration is not very interesting. However, the part of how to add a custom action to WordPress users can be really useful.

WordPress filter to edit the row of user actions

Fortunately in WordPress there is a filter to modify and add new links to this row of actions. It is called user_row_actionsuser_row_actions and in the codex you have all the information about it.

The actions to which we are referring are the following:

create-action-fast-wordpress
These are the actions for WordPress users, but you can add your own.

The same row can be found with other actions in the post window or pages and are those that give access to the option to edit, quick edit, delete, etc…

To add our custom action link we will add the following function in our functions file of our WordPress template (it is better if there is a child theme) or the plugin file that we use for our own functions.

function silocreativo_new_action( $actions, $user ) {
    $actions['new_action'] = "<a class='new_action' href='" . admin_url( "users.php?&action=new_action&amp;user=$user->ID") . "'>" . esc_html__( 'New action', 'silocreativo' ) . "</a>";
    return $actions;
}
add_filter('user_row_actions', 'silocreativo_new_action', 10, 2);

As you see, we added a new link with the action “new_action.” Change the name of it and customize it so that it can serve you in your project. Note well that we also have the users ID so we can use them later (in our case to get the phone number and send the SMS.)

Designing and UX for custom actions

Each action is a world and in each project you need it, the content or function to be executed will be very different. However, all of them have something in common, and that is that after clicking on the link we need some kind of confirmation so that the user knows that everything has gone ok, a bit of UX can never be missing.

For this we will add the following function with its corresponding add_action:

function silocreativo_new_action_notice() {
?>
<div class="updated">
<p><?php esc_html_e( 'Action has been done successfully', 'silocreativo' ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'silocreativo_new_action_notice' );

With this we indicate that everything has gone ok, using the default WordPress notices, in our case the success one with a personalized text:

wordpress-action

Other examples of custom links for users

The idea of using these links to perform simple actions without having to go through the editing page of each user is used by several plugins. In fact, the idea was suggested by the client because he used a plugin in another project that added a custom link to send the new user confirmation email with the link to establish the password, it is called Resend Welcome email.

I hope you found it helpful and apply this tutorial to your WordPress when you need a simple way to associate actions to each user in a simple click.

Leave a Reply

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