Using WordPress ‘pre_comment_author_url’ PHP filter

pre_comment_author_url is a WordPress PHP filter that allows you to modify the comment author’s URL cookie before it is set.

Usage

add_filter('pre_comment_author_url', 'your_custom_function');
function your_custom_function($author_url_cookie) {
    // your custom code here
    return $author_url_cookie;
}

Parameters

  • $author_url_cookie (string) – The comment author URL cookie to be filtered.

More information

See WordPress Developer Resources: pre_comment_author_url

Examples

Remove http:// or https:// from the URL

Remove the http:// or https:// prefix from the comment author’s URL.

add_filter('pre_comment_author_url', 'remove_url_scheme');
function remove_url_scheme($author_url_cookie) {
    return preg_replace('(https?://)', '', $author_url_cookie);
}

Add nofollow attribute to author’s URL

Add a nofollow attribute to the comment author’s URL to discourage search engines from following the link.

add_filter('pre_comment_author_url', 'add_nofollow_attribute');
function add_nofollow_attribute($author_url_cookie) {
    return $author_url_cookie . '" rel="nofollow';
}

Convert author’s URL to lowercase

Convert the entire comment author’s URL to lowercase.

add_filter('pre_comment_author_url', 'convert_url_to_lowercase');
function convert_url_to_lowercase($author_url_cookie) {
    return strtolower($author_url_cookie);
}

Add a prefix to the author’s URL

Add a custom prefix to the comment author’s URL.

add_filter('pre_comment_author_url', 'add_custom_prefix');
function add_custom_prefix($author_url_cookie) {
    return 'custom-prefix-' . $author_url_cookie;
}

Replace a specific domain with another

Replace a specific domain in the comment author’s URL with another domain.

add_filter('pre_comment_author_url', 'replace_specific_domain');
function replace_specific_domain($author_url_cookie) {
    return str_replace('olddomain.com', 'newdomain.com', $author_url_cookie);
}