The ‘pre_user_description’ filter is used to modify a user’s description before the user is created or updated in a WordPress website. This allows you to customize the description as needed, providing flexibility for your WordPress site.
Usage
To use this filter, add a custom function to your theme’s functions.php file or a plugin, and then hook it to the ‘pre_user_description’ filter. Here’s a code example:
function my_custom_pre_user_description( $description ) {
// Your code to modify the description
return $description;
}
add_filter( 'pre_user_description', 'my_custom_pre_user_description' );
Parameters
$description(string): The user’s description that will be filtered.
Examples
Limit Description Length
If you want to limit the description length to a certain number of characters:
function limit_user_description_length( $description ) {
$max_length = 200;
return substr( $description, 0, $max_length );
}
add_filter( 'pre_user_description', 'limit_user_description_length' );
In this example, the user description will be truncated to a maximum of 200 characters.
Add a Prefix to User Description
To add a prefix to the user description, use the following code:
function add_prefix_user_description( $description ) {
$prefix = "About Me: ";
return $prefix . $description;
}
add_filter( 'pre_user_description', 'add_prefix_user_description' );
This code will add the “About Me: ” prefix to the user description.
Remove HTML Tags from Description
If you want to remove HTML tags from the user description:
function remove_html_tags_user_description( $description ) {
return strip_tags( $description );
}
add_filter( 'pre_user_description', 'remove_html_tags_user_description' );
This code will strip all HTML tags from the user description.
Convert URLs to Links
To convert plain URLs in the description into clickable links:
function convert_urls_to_links_user_description( $description ) {
return make_clickable( $description );
}
add_filter( 'pre_user_description', 'convert_urls_to_links_user_description' );
This code will convert any plain URLs in the user description to clickable links.
Replace Specific Words
If you want to replace specific words in the user description:
function replace_words_user_description( $description ) {
$search = array( 'bad_word1', 'bad_word2' );
$replace = array( 'good_word1', 'good_word2' );
return str_replace( $search, $replace, $description );
}
add_filter( 'pre_user_description', 'replace_words_user_description' );
In this example, the function replaces “bad_word1” with “good_word1” and “bad_word2” with “good_word2” in the user description.