1

It's possible change the destination of the normal author link to a post page or other link inside the website?

I have found this answer but needed do buy php code. Is possible found easier way, form the users page in the back-end?

function wpd_author_link( $link, $author_id, $author_nicename ){
    return 'http://my.blog.tld/';
}
add_filter( 'author_link', 'wpd_author_link', 20, 3 );

Thank you.

  • You can place filters and hooks in the functions.php file. That file runs on every page load. – gdaniel May 12 '17 at 14:08
2

To only edit the link associated with authors, in the functions.php of your theme:

add_filter( 'author_link', 'new_author_link', 10, 1 );

function new_author_link( $link ) {      
    $link = 'http://newlink.com/'; //set this however you wish

    return $link; //after you've set $link, return it to the filter              
}

If you're looking to do something like set each author's link to an existing wp page of the same name (untested example):

add_filter( 'author_link', 'new_author_link', 10, 3 );
function new_author_link( $link, $author_id, $author_nicename ) {

     $page = get_page_by_path( $author_nicename );

     if ($page) { 

         $page = $page->ID;
         $link = get_permalink( $page ); 
     }
     else {
        $link = ''; //some default value perhaps
     }
     return $link;
}

More from WP Codex on Filtering the Author

More from WP Codex on Filters in general.


For your updated example:

If you're trying to redirect all author links to home_url( 'link' )

add_filter( 'author_link', 'new_author_link', 10, 1 );

function new_author_link( $link ) {         
    $link = home_url( 'link' ); //set this however you wish

    return $link; //after you've set $link, return it to the filter                
}

If you're trying to achieve some other conditional if/else:

add_filter( 'author_link', 'new_author_link', 10, 1 );

function new_author_link( $link, $author_id, $author_nicename ) {         
    //send author with id one to home link
    if ($author_id == '1') {
         $link = home_url( 'link' ); //set this however you wish
    }
    //send all other authors to some other link
    else {
        $link = 'http://sitename.com/some-other-url/';
    }

    return $link; //after you've set $link, return it to the filter                
}
| improve this answer | |
  • Hi @hwl. Thank you. This work but if I have 1 author. If i have more then one how I can redirect all people to the specific page. I try to use $author_nicename but dosen't work. – Marco Romano May 15 '17 at 9:10
  • Try the first solution. What you set $link to should apply to all author links in that case. – hwl May 15 '17 at 16:18
  • I've added two examples based on your update. – hwl May 15 '17 at 16:28
0

At moment i have done in this way:

add_filter( 'author_link', 'my_multi_author_link', 10, 2 ); function my_multi_author_link( $url, $user_id ) {

if ( 1 === $user_id )
    return home_url( 'link' );

return $url;

}

add_filter( 'author_link', 'my_multi_author_link_2', 10, 2 ); function my_multi_author_link_2( $url, $user_id ) {

if ( 5 === $user_id )

    return home_url( 'link' );

return $url;

}

| improve this answer | |
  • This shouldn't be necessary, but even if you were to approach it this way, that could be combined into one filter. – hwl May 15 '17 at 16:19

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.