apply_filters( ‘author_link’, string $link, int $author_id, string $author_nicename )

Filters the URL to the author’s page.

Parameters

$linkstring
The URL to the author’s page.
$author_idint
The author’s ID.
$author_nicenamestring
The author’s nice name.

Source

$link = apply_filters( 'author_link', $link, $author_id, $author_nicename );

Changelog

VersionDescription
2.1.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Example migrated from Codex:

    If you add the following to the functions.php file of your child themes, you can modify the author link for all posts or using conditional tags.

    add_filter( 'author_link', 'modify_author_link', 10, 1 ); 	 	 
    function modify_author_link( $link ) {	 	 
        $link = 'http://example.com/';
    return $link;
    }
  2. Skip to note 4 content

    In this tutorial, you will learn how to use the apply_filters function with the author_link filter to modify the author link based on multiple conditions in WordPress. This can be useful if you want to customize the author link for specific authors, roles, or other criteria.

    Add the following code to your theme’s functions.php file or your custom plugin file to hook into the author_link filter and modify the author link based on multiple conditions.

    Step 1:

    add_filter( 'author_link', 'wpdocs_author_link', 10, 3 );

    Step 2:

    function wpdocs_author_link( $link, $author_id, $author_nicename ) {
        $specific_author_ids = array( 1, 2, 3 ); // Replace with your specific author IDs
        $author = get_user_by( 'ID', $author_id );
    
        // Condition 1: Check for a specific author by ID
        if ( in_array( $author_id, $specific_author_ids ) ) {
            $link = add_query_arg( 'custom_param', 'value', $link );
        }
    
        // Condition 2: Check if the author has a specific role
        if ( in_array( 'editor', (array) $author->roles ) ) {
            $link = add_query_arg( 'role', 'editor', $link );
        }
    
        // Condition 3: Modify link for a specific author nicename
        if ( 'john_doe' === $author_nicename ) {
            $link = home_url( '/special-author-page/' );
        }
    
        // Additional customizations can be added here
    
        return $link;
    }

    You have now successfully used the apply_filters function with the author_link filter to modify the author link based on multiple conditions in WordPress. This approach allows you to customize the author links to fit your specific requirements.

You must log in before being able to contribute a note or feedback.