We'll create fresh WordPress site with Linkify Text installed. You have 20 minutes to test the plugin after that site we'll be deleted.
This plugin allows you to define words or phrases that, whenever they appear in your posts or pages, get automatically linked to the URLs of your choosing. For instance, wherever you may mention the word “WordPress”, that can get automatically linked as “WordPress“.
Additional features of the plugin controlled via settings and filters:
You can also link multiple terms to the same link and only define that link once in the settings via use of a special link syntax.
A number of filters exist to programmatically customize the behavior of the plugin, all of which are documented.
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
The plugin exposes a number of filters for hooking. Typically, code making use of filters should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain). Bear in mind that most of the features controlled by these filters are configurable via the plugin’s settings page. These filters are likely only of interest to advanced users able to code.
c2c_linkify_text_filters (filter)
The ‘c2c_linkify_text_filters’ hook allows you to customize what hooks get text linkification applied to them.
Arguments:
Example:
/**
* Enables text linkification for custom fields.
*
* @param array $filters The filters handled by the Linkify Text plugin.
*/
function more_text_replacements( $filters ) {
$filters[] = 'the_meta'; // Here you could put in the name of any filter you want
return $filters;
}
add_filter( 'c2c_linkify_text_filters', 'more_text_replacements' );
c2c_linkify_text_comments (filter)
The ‘c2c_linkify_text_comments’ hook allows you to customize or override the setting indicating if text linkification should be enabled in comments.
Arguments:
Example:
// Prevent text linkification from ever being enabled in comments.
add_filter( 'c2c_linkify_text_comments', '__return_false' );
c2c_linkify_text (filter)
The ‘c2c_linkify_text’ hook allows you to customize or override the setting defining all of the text phrases and their associated links.
Arguments:
Example:
/**
* Programmatically adds more text to be linked.
*
* @param array $text_to_links Array of text and their associated URLs.
*/
function my_text_linkifications( $text_to_links ) {
// Add text link
$text_to_links['Matt Mullenweg'] => 'https://ma.tt';
// Unset a text link that we never want defined
if ( isset( $text_to_links['WordPress'] ) ) {
unset( $text_to_links['WordPress'] );
}
// Important! Return the changes.
return $text_to_links;
}
add_filter( 'c2c_linkify_text', 'my_text_linkifications' );
c2c_linkify_text_case_sensitive (filter)
The ‘c2c_linkify_text_case_sensitive’ hook allows you to customize or override the setting indicating if text matching for potential text linkification should be case sensitive or not.
Arguments:
Example:
// Prevent text matching from ever being case sensitive.
add_filter( 'c2c_linkify_text_case_sensitive', '__return_false' );
c2c_linkify_text_replace_once (filter)
The ‘c2c_linkify_text_replace_once’ hook allows you to customize or override the setting indicating if text linkification should be limited to once per term per piece of text being processed regardless of how many times the term appears.
Arguments:
Example:
// Only linkify a term once per post.
add_filter( 'c2c_linkify_text_replace_once', '__return_true' );
c2c_linkify_text_open_new_window (filter)
The ‘c2c_linkify_text_open_new_window’ hook allows you to customize or override the setting indicating if links should open in a new window.
Arguments:
Example:
// Make links open in a new window.
add_filter( 'c2c_linkify_text_open_new_window', '__return_true' );
c2c_linkify_text_linked_text (filter)
The ‘c2c_linkify_text_linked_text’ hook allows you to customize or override the replacement link markup for a given string. Return the value of $old_text to effectively prevent the given text linkification.
Arguments:
Example:
/**
* Disable linkification of links for posts that have the 'disable_linkify_text'
* custom field defined.
*
* @param array $display_link The associative array of attributes to be used for the link.
* @param string $old_text The text being replaced/linkified.
* @param string $link_for_text The URL that $old_text is to be linked to.
* @param string $text_to_link The full array of text and the URLs they should link to.
* @return string
*/
function selectively_disable_text_linkification( $display_link, $old_text, $link_for_text, $text_to_link ) {
if ( get_metadata( 'post', get_the_ID(), 'disable_linkify_text', true ) ) {
$display_link = $old_text;
}
return $display_link;
}
add_filter( 'c2c_linkify_text_linked_text', 'selectively_disable_text_linkification', 10, 4 );
c2c_linkify_text_link_attrs (filter)
The ‘c2c_linkify_text_link_attrs’ hook allows you to add or customize attributes for the link.
Arguments:
Example:
/**
* Force links created by Linkify Text plugin to open in a new tab.
*
* @param array $attrs The associative array of attributes to be used for the link.
* @param string $old_text The text being replaced/linkified.
* @param string $link The URL that $old_text is to be linked to.
* @return array
*/
function my_linkify_text_attrs( $attrs, $old_text, $link ) {
$attrs['target'] = '_blank';
return $attrs;
}
add_filter( 'c2c_linkify_text_link_attrs', 'my_linkify_text_attrs', 10, 3 );