apply_filters( ‘author_rewrite_rules’, string[] $author_rewrite )

Filters rewrite rules used for author archives.

Description

Likely author archives would include /author/author-name/, as well as pagination and feed paths for author archives.

Parameters

$author_rewritestring[]
Array of rewrite rules for author archives, keyed by their regex pattern.

Source

$author_rewrite = apply_filters( 'author_rewrite_rules', $author_rewrite );

Changelog

VersionDescription
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    In this tutorial, you will learn how to use the apply_filters function with the author_rewrite_rules filter to modify the author rewrite rules based on multiple conditions in WordPress. This can be useful if you want to customize the URL structure for author archives.

    Step 1 Hook into the Filter:

    add_filter( 'author_rewrite_rules', 'wpdocs_author_rewrite_rules' );

    Step 2 Define the Callback Function:

    function wpdocs_author_rewrite_rules( $author_rewrite ) {
        // Condition 1: Add a custom rewrite rule for specific author nicenames
        $author_rewrite['author/([a-zA-Z0-9-]+)/custom'] = 'index.php?author_name=$matches[1]&custom_param=value';
    
        // Condition 2: Modify the priority of an existing rewrite rule
        if ( isset( $author_rewrite['author/([a-zA-Z0-9-]+)/?$'] ) ) {
            $author_rewrite['author/([a-zA-Z0-9-]+)/?$'] = 'index.php?author_name=$matches[1]&priority=high';
        }
    
        // Condition 3: Remove a specific rewrite rule
        unset( $author_rewrite['author/([0-9]+)/?$'] );
    
        // Additional customizations can be added here
    
        return $author_rewrite;
    }

    Step 3: Flush Rewrite Rules
    After modifying the rewrite rules, you need to flush the rewrite rules to ensure the changes take effect. You can do this by visiting the Permalinks settings page in the WordPress admin area (Settings > Permalinks) and simply clicking “Save Changes”.

    Alternatively, you can flush the rewrite rules programmatically. Add this code to your theme’s functions.php file or your custom plugin file:

    function wpdocs_flush_rewrite_rules() {
        flush_rewrite_rules();
    }
    add_action( 'init', 'wpdocs_flush_rewrite_rules' );

    You have now successfully used the apply_filters function with the author_rewrite_rules filter to modify the author rewrite rules based on multiple conditions in WordPress. This approach allows you to customize the URL structure for author archives to fit your specific requirements.

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