make_clickable()
Converts non-clickable links in the text - http://ссылка into clickable ones (an HTML link). Also converts text starting with: www, ftp, email into links.
Converts strings starting with http://, www, ftp, email into the corresponding HTML link code.
Takes into account cases when the URI is already clickable in the text and does not replace it again.
Does not process text inside tags: <code>, <pre>, <script>, <style>.
Passes the returned string through the PHP function trim() - removes spaces at the ends of the string. I once had difficulties with this behavior. Since version 3.8. trailing spaces are not removed.
By default in WP it is applied to comment text:
add_filter( 'comment_text', 'make_clickable', 9 );
No Hooks.
Returns
String. Modified text with HTML links.
Usage
$text = make_clickable( $text );
- $text(string) (required)
- The text to be filtered.
Examples
#1 Turn a link (URL) in the text into a clickable HTML link:
<?php $ret = "Some text http://example.com/page-108 and some other text."; echo make_clickable( $ret ); ?>
As a result of this example we get:
Some text <a href="http://example.com/page-108" rel="nofollow">http://example.com/page-108</a> and some other text.
#2 Make the links (URLs) clickable and with target blank
Let's turn all URLs in the content to clickable links and with target=_blank attribute (to open the links in a new tab). Use links_add_target().
<?php $content = "Some text http://example.com/page-108 and some other text"; echo links_add_target( make_clickable( $content ) ); ?>
As a result of this example we get:
Some text <a href="http://example.com/page-108" rel="nofollow" target="_blank">http://example.com/page-108</a> and some other text.
Changelog
| Since 0.71 | Introduced. |