While working on a user badge system for the CG Cookie network, I came across the need to add a custom link to the action links, such as Edit and Delete, present for each user on the WordPress Users page. After a quick google search, I was able to find a simple filter than can be used for doing exactly this.

The user row action links I’m referring to are those shown in the screenshot below:

WordPress has a filter setup that we can use to add our own custom links, or even modify existing ones, and it’s called “user_row_actions”. You can see the source for the core function that sets up the action links here.

I wanted to add a “Edit Badges” link to the list of actions, and this was quite simply done with the following function:

1
2
3
4
5
function cgc_ub_action_links($actions, $user_object) {
	$actions['edit_badges'] = "<a class='cgc_ub_edit_badges' href='" . admin_url( "users.php?page=cgc-badges&action=cgc_edit_badges&amp;user=$user_object->ID") . "'>" . __( 'Edit Badges', 'cgc_ub' ) . "</a>";
	return $actions;
}
add_filter('user_row_actions', 'cgc_ub_action_links', 10, 2);

Really quite simple, and the result looks like this:

Note, similar functions also exist for posts and pages:

post_row_actions
page_row_actions
  1. designerken

    Nice little tip there pippin, I have also found a way to create a user table by extending the class for WP_List_Table. In this you can also add in your own links.

    • Pippin

      The WP_List_Table is really great, but be careful with it, since it has not yet been finalized; it could change a lot.

  2. emeraldjava

    Hi,

    Do you have an example where you handle the ‘edit_badges’ action?

    P

    • Pippin

      The edit_badges action would be handled from the function that you have setup to render your custom admin page (note the ?page= part). Inside of that function, you can do something like this (taken directly from the system I wrote):

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      
      if(isset($_GET['cgc_ub_page']) && wp_verify_nonce($_GET['_wpnonce'], 'cgc_ub_edit')) { 
      	switch($_GET['cgc_ub_page']) {
      		case 'edit_badge' :
      			include(CGC_UB_PLUGIN_DIR . '/includes/edit-badge.php');
      			break;
      		case 'edit_users_badges' :
      			include(CGC_UB_PLUGIN_DIR . '/includes/edit-users-badges.php');
      			break;
      	}
      }

      Does that help?

    • emeraldjava

      thanks for that. The logic for the form processing is OK it just i’m not sure what is the correct action hook to link to. I’m using the ‘init’ action and at the end of the function i use wp_mail

      add_filter(‘user_row_actions’,array( &$this,’bhaa_runner_renew_link’),10,2);
      add_action(‘init’,array(&$this,’bhaa_runner_renew_action’),11);

      but I think the ‘init’ action is being called multiple times. I’m wondering is there a more specific action that is only called once by wordpress.

    • Pippin

      Great to hear you found it 🙂

  3. Lucianno

    good tip, but I’m wondering how to add those actions on a custom post type table… any idea?

Comments are closed.