Linkify Text

Author:Scott Reilly
Version:1.7
First released:2011-12-05
Last update:2015-02-21
Compatibility:WP 3.6 – 4.1.7
Download:[ zip ]
Description:

Automatically hyperlink words or phrases in your posts.

Extended Description

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:

  • Text linkification can be enabled for comments (it isn’t by default)
  • Text linkification can be made case sensitive (it isn’t by default)
  • Text linkification can be limited to doing only one linkification per term, per post (by default, all occurrences of a term are linkified)

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.

Links: Plugin Homepage | Plugin Directory Page | Author Homepage

Find out more at the plugin’s WordPress Plugin Repository page.

Screenshots

Click to see full-size image.

  1. linkify-text screenshot 1

    A screenshot of the admin options page for the plugin, where you define the text and their related links, as well as customize various settings.


Installation

  1. Unzip linkify-text.zip inside the /wp-content/plugins/ directory (or install via the built-in WordPress plugin installer)
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. (optional) Go to the Settings -> Linkify Text admin options page and define text and the URLs they should point to

Filters

The plugin exposes five filters for hooking. Typically, the code to utilize these hooks would go inside your active theme’s functions.php file. Bear in mind that all 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:

  • $hooks (array): Array of hooks that will be text linkified.

Example:

// Enable text linkification for custom fields
add_filter( 'c2c_linkify_text_filters', 'more_text_replacements' );
function more_text_replacements( $filters ) {
    $filters[] = 'the_meta'; // Here you could put in the name of any filter you want
    return $filters;
}

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:

  • $state (bool): Either true or false indicating if text linkification is enabled for comments. The default value will be the value set via the plugin’s settings page.

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:

  • $linkify_text_array (array): Array of text and their associated links. The default value will be the value set via the plugin’s settings page.

Example:

// Add more text to be linked
add_filter( 'c2c_linkify_text', 'my_text_linkifications' );
function my_text_linkifications( $replacements ) {
    // Add text link
    $replacements['Matt Mullenweg'] => 'http://ma.tt';
    // Unset a text link that we never want defined
    if ( isset( $replacements['WordPress'] ) )
        unset( $replacements['WordPress'] );
    // Important!
    return $replacements;
}

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:

  • $state (bool): Either true or false indicating if text matching is case sensitive. The default value will be the value set via the plugin’s settings page.

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:

  • $state (bool): Either true or false indicating if text linkification is to only occur once per term. The default value will be the value set via the plugin’s settings page.

Example:

// Only linkify a term once per post.
add_filter( 'c2c_linkify_text_replace_once', '__return_true' );

Frequently Asked Questions

Q. Does this plugin modify the post content in the database?
A. No. The plugin filters post content on-the-fly.

Q. Will this work for posts I wrote prior to installing this plugin?
A. Yes, if they include terms that you have defined to be linkified.

Q. What post fields get handled by this plugin?
A. By default, the plugin filters the post content, post excerpt fields, widget text, and optionally comments and comment excerpts. You can use the ‘c2c_linkify_text_filters’ filter to modify that behavior (see Filters section). There is a setting you can change to make text linkifications apply to comments as well.

Q. Is the plugin case sensitive?
A. By default, yes. There is a setting you can change to make it case insensitive. Or if you are a coder, you can use the ‘c2c_linkify_text_case_sensitive’ filter (see Filters section).

Q. What if the word or phrase is already linked in a post?
A. Already linked text will not get linked again by this plugin (regardless of what the link may be).

Q. Will all instances of a given term be linked in a single post?
A. By default, yes. There is a setting you can change so that only the first occurrence of the term in the post gets linked. Or if you are a coder, you can use the ‘c2c_linkify_text_replace_once’ filter (see Filters section).

Q. Is there an efficient way to link multiple terms to the same link without repeating the link in the settings field (which can be tedious and prone to errors)?
A. Yes. You can reference another term by specifying its link as another term in the list prepended with a colon (‘:’). For instance:

WP => wordpress.org,
WordPress => :WP
dotorg => :WP

Given the above terms to link, all terms would link to ‘https://wordpress.org’. The latter two all reference the link used for the term “WP”.

NOTE: The referenced term must have an actual link defined and not be a reference to another term. (Basically, nested references are not currently supported.)

Q. How can I get text linkification to apply for custom fields (or something not linkified by default)?
A. You can add to the list of filters that get text linkified using something like this (added to your theme’s functions.php file, for instance):

/**
 * Enable text linkification for custom fields.
 *
 * @param array $filters Array of filters that the plugin should hook.
 * @return array
 */
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' );

Q. Can I only have text linkification take place for only a part of a post (such as text inside certain tags, or except for text in certain tags)?
A. No. The plugin applies fully to the post content. With some non-trivial coding the plugin could be utilized to affect only targeted parts of a post’s content, but it’s not something that will be built into the plugin.

Q. Can I change how the link gets created because I want to add a ‘title’ attribute to the link?
A. Yes, with a bit of code. You can define the title attribute text in your replacement string, like so:

WP => wordpress.org || This is the link title

Now the code:

function add_title_attribute_to_linkified_text( $display_link, $text_to_link, $link_for_text  ) {
    // The string that you chose to separate the link URL and the title attribute text.
    $separator = ' || ';

    // Only change the linked text if a title has been defined
    if ( false !== strpos( $link_for_text, $separator ) ) {
        // Get the link and title that was defined for the text to be linked.
        list( $link, $title ) = explode( $separator, $link_for_text );

        // Make the link the way you want.
        $display_link = '<a href="' . esc_url( $link ) . '" title="' . $title . '">' . $text_to_link . '</a>';
    }

    return $display_link;
}
add_filter( 'c2c_linkify_text_linked_text', 'add_title_attribute_to_linkified_text', 10, 3 );

Q. Does this plugin include unit tests?
A. Yes.


Release Log

1.7 (2015-02-20)

  • Improve support of ‘&’ in text to be linked by recognizing its encoded alternatives (‘&’, ‘&’) as equivalents
  • Prevent linkification of text if the provided link doesn’t look anything like a link
  • Change regex delimiter from ‘|’ to ‘~’
  • Minor refactoring of multibyte handling
  • Add to and improve unit tests
  • Add help text under primary textarea mentioning the term referencing feature
  • Minor documentation changes throughout

1.6 (2015-02-12)

  • Prevent text replacements from taking place within shortcode attributes or content. props @rbonk
  • Support linkifying multibyte strings. NOTE: Multibyte strings don’t honor limiting their replacement within a piece of text to once
  • Use preg_quote() to escape user input used in regex
  • Update plugin framework to 039
  • Add check to prevent execution of code if file is directly accessed
  • Minor plugin header reformatting
  • Minor code reformatting (spacing, bracing)
  • Change documentation links to wp.org to be https
  • Add an FAQ question
  • Note compatibility through WP 4.1+
  • Update copyright date (2015)
  • Add an FAQ question
  • Add more unit tests
  • Add plugin icon
  • Regenerate .pot

1.5 (2014-01-04)

  • Add setting to allow limiting linkification to once per term per text
  • Add filter ‘c2c_linkify_text_replace_once’
  • Add ability for a term to use another term’s link
  • Change to just-in-time (rather than on init) determination if comments should be filtered
  • Add linkify_comment_text()
  • Add get_instance() static method for returning/creating singleton instance
  • Made static variable ‘instance’ private
  • Validate post is either int or string before handling
  • Add unit tests
  • Omit output of empty ‘title’ attribute for links
  • Update plugin framework to 037
  • Use explicit path for require_once()
  • For options_page_description(), match method signature of parent class
  • Discontinue use of explicit pass-by-reference for objects
  • Code tweaks (spacing, bracing, rearranging)
  • Documentation enhancements, additions, and tweaks
  • Note compatibility through WP 3.8+
  • Drop compatibility with version of WP older than 3.6
  • Update copyright date (2014)
  • Regenerate .pot
  • Change donate link
  • Add assets directory to plugin repository checkout
  • Add banner
  • Add screenshot

1.0.1 (unreleased)

  • Re-license as GPLv2 or later (from X11)
  • Add ‘License’ and ‘License URI’ header tags to readme.txt and plugin file
  • Add ‘Upgrade Notice’ section to readme.txt
  • Remove ending PHP close tag
  • Note compatibility through WP 3.4+
  • Update copyright date (2012)

1.0

  • Initial release

Copyright & Disclaimer

Copyright © 2011-2015 by Scott Reilly (aka coffee2code)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


Discussion / Support

Have any questions, comments, or suggestions? Please provide them via the plugin’s WordPress.org support forum. I’ll do my best to reply in a timely fashion and help as best I can.

Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.

Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.