Automatically add links to words or phrases in your posts.
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 );
linkify-text.zip
inside the plugins directory for your site (typically wp-content/plugins/
)Settings
-> Linkify Text
admin options page and define text and the URLs they should point tolinkify-text.zip
inside the plugins directory for your site (typically wp-content/plugins/
)Settings
-> Linkify Text
admin options page and define text and the URLs they should point toNo. The plugin filters post content on-the-fly.
Yes, if they include terms that you have defined to be linkified.
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.
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).
Already linked text will not get linked again by this plugin (regardless of what the link may be).
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).
Yes. You can reference another term by specifying its link as another term in the list prepended with a colon (‘:’). For instance:
WP => https://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.)
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' );
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.
Yes, with a bit of code. You can define the title attribute text in your replacement string, like so:
WP => https://wordpress.org || This is the link title
Now the code:
/** * 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_for_text The URL that $old_text is to be linked to. * @return array */ function add_title_attribute_to_linkified_text( $attrs, $old_text, $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( $url, $title ) = explode( $separator, $link_for_text, 2 ); // Set the attributes ('href' must be overridden to be a proper URL). $attrs['href'] = $url; $attrs['title'] = $title; } return $attrs; } add_filter( 'c2c_linkify_text_link_attrs', 'add_title_attribute_to_linkified_text', 10, 3 );
Yes, with some custom code making use of the ‘c2c_linkify_text_linked_text’ filter. The code should determine if the given text linkification should be disabled, and if so, return the second argument sent via the filter. See the docs for the ‘c2c_linkify_text_linked_text’ filter for an example of how a custom field could be used to disable all text linkifications on a per-post basis. No doubt your particular situation will require custom logic to determine when to disable linkification.
Yes.
mb_*
functions aren’t used when not availableHighlights:
Details:
* New: Add setting to set if links should open in a new window/tab
* New: Add filter ‘c2c_linkify_text_link_attrs’ for adding attributes to links
* New: Add support for finding linkable text that may span more than one line or consist of internal spaces
* Fix: Improve handling of removing links within links
* Change: Improve performance by checking for substring match for phrase to linkify before doing much work
* Change: Update plugin framework to 048
* 048:
* When resetting options, delete the option rather than setting it with default values
* Prevent double “Settings reset” admin notice upon settings reset
* 047:
* Don’t save default setting values to database on install
* Change “Cheatin’, huh?” error messages to “Something went wrong.”, consistent with WP core
* Note compatibility through WP 4.9+
* Drop compatibility with version of WP older than 4.7
* 046:
* Fix reset_options()
to reference instance variable $options
* Note compatibility through WP 4.7+
* Update copyright date (2017)
* 045:
* Ensure reset_options()
resets values saved in the database
* 044:
* Add reset_caches()
to clear caches and memoized data. Use it in reset_options()
and verify_config()
* Add verify_options()
with logic extracted from verify_config()
for initializing default option attributes
* Add add_option()
to add a new option to the plugin’s configuration
* Add filter ‘sanitized_option_names’ to allow modifying the list of whitelisted option names
* Change: Refactor get_option_names()
* 043:
* Disregard invalid lines supplied as part of hash option value
* Change: Bail early if filtering disables linking of the given text
* Change: Prevent PHP warnings by ensuring array elements exist before use
* Change: Cast return values of hooks to expected data types
* Change: Improve setting page help text
* New: Add README.md
* New: Add GitHub link to readme
* Change: Store setting name in constant
* Unit tests:
* Change: Improve test initialization
* Change: Improve tests for settings handling
* Change: Default WP_TESTS_DIR
to /tmp/wordpress-tests-lib
rather than erroring out if not defined via environment variable
* Change: Enable more error output for unit tests
* New: Add more tests
* New: Add header comments to bootstrap
* Change: Note compatibility through WP 4.9+
* Change: Drop compatibility with version of WP older than 4.7.
* Change: Tweak plugin description
* Change: Rename readme.txt section from ‘Filters’ to ‘Hooks’
* Change: Modify formatting of hook name in readme to prevent being uppercased when shown in the Plugin Directory
* Change: Update installation instruction to prefer built-in installer over .zip file
* Change: Update copyright date (2018)
c2c_plugin_version()
.empty()
.