3

I am trying to allow my editors and admins to click to "Edit Author" as they would "Edit Post" or "Edit [Custom Post Type]" from the admin bar when they are on a page like /authors/jdoe

(Note: I do $wp_rewrite->author_base = 'people' so that my addresses are /people/jdoe ... not sure if that creates any potential issues.)

I tried to do this through functions but later read that functions.php is processed too early to get the current template or any variables or IDs from the page content.

function add_author_edit_link( $wp_admin_bar ) {
if ( is_page_template('author.php') ) {
  $args = array(
    'id' => 'author-edit',
    'title' => __( 'Edit Person' ),
    'href' => '/wp-admin/user-edit.php?user_id=' . $user->ID
  );
$wp_admin_bar->add_node($args);
  } // if is_page_template author
}
add_action( 'admin_bar_menu', 'add_author_edit_link', 500 );

Not sure how to approach this but will appreciate any thoughts.

I guess I could try to put it into the page content for certain roles only, but is there no easier way to add something like it to the admin bar?

2

You could try this modification of your code:

function add_author_edit_link( $wp_admin_bar )
{
    if ( is_author() && current_user_can( 'add_users' ) )
    {
        $args = array(
           'id'    => 'author-edit',
           'title' => __( 'Edit Author' ),
           'href'  => admin_url( sprintf( 
               'user-edit.php?user_id=%d',
               get_queried_object_id() 
           ) )
        );
        $wp_admin_bar->add_node($args);
  } 
}

add_action( 'admin_bar_menu', 'add_author_edit_link', 99 );

where we use the get_queried_object_id() function to get the author id.

Notice that you can use the admin_url() to get the url to the backend.

I use is_author() here instead of is_page_template( 'author.php' ).

This Edit Author link might not be relevant for users that can't modify other users, so I added the current_user_can( 'add_users' ) check. I couldn't find the edit_users capability so I used add_users instead.

  • There are meta caps 'edit_users' and 'edit_user' (see map_meta_cap() in "wp-includes/capabilities.php") so I think you should be able to use current_user_can( 'edit_user', get_queried_object_id() ) here... – bonger Jan 24 '15 at 6:55
  • thanks @bonger for checking it further - yesterday evening I could only check the Codex page on capabilities and not dig further on that ;-) – birgire Jan 24 '15 at 10:35
  • Thank you! This works for me. I was confused about using is_author() because I wasn't sure if that would be me or the author on the page. This clarifies it. – rktl-c Jan 26 '15 at 20:08
1

You might try this:

function add_author_edit_link( $wp_admin_bar ) {
    if ( is_author() && current_user_can( 'add_users' )) {
        $currentUserID = get_current_user_id();
        $args = array(
            'id' => 'author-edit',
            'title' => __( 'Edit Person' ),
            'href' => '/wp-admin/user-edit.php?user_id=' . $currentUserID
        );
        $wp_admin_bar->add_node($args);
    }
}
add_action( 'admin_bar_menu', 'add_author_edit_link', 500 );
  • This works for me. Thank you! I expect to see someone add it to a list of "top __ wp admin bar hacks" or something. – rktl-c Jan 26 '15 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.