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 Answers
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.