Function and usage of make_clickable() in wordpress

Question

Answers ( 1 )

    0
    2024-01-09T02:02:38+00:00

    The make_clickable() function in WordPress is designed to convert plain text URIs (Uniform Resource Identifiers) into clickable HTML links. This function is particularly useful when you want to automatically hyperlink URLs, email addresses, and FTP links in a string of text without manually adding HTML anchor tags. Here's a detailed explanation of the function and an example of its usage:

    Function:

    make_clickable( string $text ): string

    Description:

    • Purpose: The make_clickable() function takes a string containing plain text URIs and converts them into clickable HTML links.
    • Process: It automatically identifies URIs, including those starting with "http://", "https://", "ftp://", as well as email addresses. It then wraps these URIs in the appropriate HTML anchor (<a>) tags to make them clickable.
    • Additional Handling: The function also addresses nested links, ensuring that already hyperlinked text within the input string is not reprocessed or broken.

    Parameters:

    • $text (string, required): The content (text) in which URIs need to be converted to clickable links.

    Return:

    • Type: string
    • Description: Returns the processed content with URIs converted into clickable HTML links.

    Sample Usage:

    Imagine you have a string that contains various URLs and an email address, and you want to convert them into clickable links in your WordPress post or page.

    Original Text:

    $text = "Visit our website at http://www.example.com or contact us at info@example.com. Also, check our FTP site at ftp://ftp.example.com.";
    

    Using make_clickable():

    $converted_text = make_clickable($text);
    

    Output: The $converted_text will contain the same string but with HTML links:

    This function is particularly useful for dynamically converting URLs and email addresses in user-generated content or any large body of text where manually adding links is impractical.

Leave an answer