Converting URLs to links in WordPress using make_clickable
The WordPress make_clickable
function is a part of the formatting API. It is designed to scan through a given text and automatically convert any plain URLs, email addresses, and FTP links into clickable links. This function is applicable in scenarios where text is received from an untrusted source and needs to be displayed in a clickable format.
Once the make_clickable
function is applied to a string, it scans through the entire text. If it identifies a sequence of characters that forms a URL, email address, or FTP link, it automatically converts these into clickable links. The links are then returned as HTML anchor tags, making them actionable for the user.
This function can be beneficial in enhancing user experience by providing clickable links without requiring manual creation of HTML anchor tags for each link. It can be particularly useful when displaying large amounts of text data that may contain multiple links.
Parameters
$text
(string), required. Content to transform URIs.
Return Value
The function returns a string
containing the content with transformed URIs.
Examples
Example 1: How to make URLs in a string clickable
$text = "Visit https://www.example.com for more information.";
$clickable_text = make_clickable($text);
echo $clickable_text;
In this example, the make_clickable
function is used to convert the URL in the string $text
into a clickable link. The result is then stored in the variable $clickable_text
and displayed on the screen. The URL “https://www.example.com” will be converted into a clickable link.
Example 2: How to make multiple URLs in a string clickable
$text = "Visit https://www.example.com or http://www.anotherexample.com for more information.";
$clickable_text = make_clickable($text);
echo $clickable_text;
In this example, the make_clickable
function is used to convert multiple URLs in the string $text
into clickable links. The result is then stored in the variable $clickable_text
and displayed on the screen. The URLs “https://www.example.com” and “http://www.anotherexample.com” will be converted into clickable links.
Example 3: How to handle a string without URLs
$text = "There are no URLs in this string.";
$clickable_text = make_clickable($text);
echo $clickable_text;
In this example, the make_clickable
function is used on a string that doesn’t contain any URLs. Since there are no URLs to convert into clickable links, the function will return the original string unchanged. The result is then stored in the variable $clickable_text
and displayed on the screen. The output will be “There are no URLs in this string.”.
Conclusion
The make_clickable
function in WordPress is a built-in utility that automatically converts plain text URLs and email addresses into clickable links. This function can be particularly useful when dealing with user-generated content, where links may not be properly formatted or may be input as plain text. By utilizing the make_clickable
function, developers can ensure that these URLs and email addresses are presented in a clickable format, improving the overall user experience.
Related WordPress Functions
- Decoding HTML in WordPress using wp_specialchars_decode
- Using wp_strip_all_tags to strip all HTML tags from content in WordPress
- Sanitizing user input in WordPress with wp_kses
- Escaping HTML in WordPress: How to use esc_html function to prevent XSS attacks
- Escaping and sanitizing URLs in WordPress with esc_url
- How to escape and sanitize attributes using esc_attr in WordPress
- Sanitizing user-submitted content in WordPress using wp_kses_post
- How to sanitize text input in WordPress using sanitize_text_field