Is there a hook / filter to process the link being added into a WordPress post ?
My aim is to pre-process the link inserted in a post using the following button and shorten it using a third party API like bit.ly
I want to do this for both internal / external links.
One solution that I an think of is to add an extra button to my editor that does this but I would prefer a hook / filter that does the job, that way it would be more clean and I will convert that into a custom plugin for my website (there by allowing my WordPress to be up-gradable).
I went through the WordPress docs and skimmed through the following hooks / filters which were of no use to me
Update 1: As far as I know, external URLs are inserted into post content by TinyMCE editor link plugin, PHP does nothing.
In WordPress, there're two plugins which located at
wp-includes/js/wplink.js
andwp-includes/js/tinymce/plugins/wplink/plugin.js
. Note that if you're not inSCRIPT_DEBUG
mode, they have.min
suffix.wp-includes/js/wplink.js handles this dialog:
To filter URLs inserted via this dialog box, we must override
wpLink.getAttrs
method. For example, to addso39115564
string to every URLs:You should take a look at the
wp-includes/js/wplink.js
for more info. It's too long to explain in detail here.And let say the above script is
mylink.js
, here is how we should enqueue it:wp-includes/js/tinymce/plugins/wplink/plugin.js handles this dialog:
This time, we also have to override
setURL
method oftinymce.ui.WPLinkPreview
. But, it's almost impossible, unless you deregister this script and register a modified version. Then manage that script yourself with unpredictable changes from WordPress.Now, choose it wisely! Shorten any external URLs before pasting them into your posts or mess up with WordPress TinyMCE plugins or use dialog box of
wp-includes/js/wplink.js
plugin.Yes! WordPress inserts inline links via
wp_link_ajax
action which is executed by wp_ajax_wp_link_ajax() function.As you can see in source code of that function,
$results
is retrieved by_WP_Editors::wp_link_query
. Check out this method, you will meet wp_link_query filter. This filter accept two arguments:$results
and$query
.$results
will be what we need to filter.For example, we need to append
so39115564
to the queried link:Now, you should know how to do it. Make sure to take a look at _WP_Editors::wp_link_query to filter the
$results
more efficient.I am not perfect in TinyMCE because i didn't used TinyMCE in several project but i have experience with javascript so i found a some TinyMCE event api to use you can make a short url. I checked in WP latest version and TinyMCE version 4.x. You can also find for right api to trigger your custom callback.
Before tinymce initialize in wordpress editor register setup function using wordpress hook tiny_mce_before_init.
After add javascript callback in admin footer. I have two methods for this solution.
#1 method
#2 method
Best of luck :)
Have you considered not processing the link until you save the post? There is an action in the wordpress plugin api called "save_post" that's triggered on save.
Using "save_post" you could parse the content of the post and replace links using a url shortener then.
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
There might be plugins available for this , but here is the default logic behind.
To achieve the above thing you will have to do following things :
save_post
hook to get the post content after saving.wp_update_post
we are removing the hook to avoid infinite loop.https://wordpress.org/plugins/url-shortener/
https://wordpress.org/plugins/shortnit/