Check Hyperlinks on post save

Best workaround to build a script in Wordpress to change hyperlink structure on post save.

Scenario: an Author creates posts with hyperlinks <a href=""></a> and saves, on save would be controlled if meets the hyperlinks given conditions to be redirected or not. If the urls has to be redirected than should change the structure of it <a href="goto.html?url="></a> and than push the post in database. What I want to avoid is to not force the Author to give attention on link creation.


You can simply add an action on save_post hook, and parse content to modify links (using a dom parser, or regexp).

But imo it is not a good idea to modify post content for this, you should modify links only when displaying post (with a the_content filter).


For example :

add_filter('the_content', 'my_content');
function my_content($content){
  return $content;

You can just sanitize the href="" values.

preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="goto.html?url=$2;"$3>', $postcontent);
Tags: PHP / Wordpress / Plugins

Similar questions

Favorite websites with shortened hyperlinks
Suppose in the site there are frequent links to another website, always tree same, say Wikipedia. Is there a way to write links to Wikipedia in a shortened way, and tell wordpress to translate it? Example: something like [wiki]/geography shorthand for
Show hyperlinks only when user is loggedin
I am a newbie in wordpress.My problem is that I want to make an option for the admin in my theme, so that when admin creates a post and insert a hyperlink on the post there should be an option to make the hyperlink invisible to the users who are not logged in. How can it possible please help me.
Migrating content from an old solution. How to easily change hardcoded hyperlinks in legacy content
I'm not entirely sure where I can even ask this question as it pertains to both HTML and WP. Anyways, our old intranet solution basically held a bunch of .htm documents that we made in Word and then turned into .htm. We have switched to a WP solution and have begun uploading these .htm docs as media as it is the easiest solution for the 1700 docs w...
Absolute Hyperlinks in Wordpress Website Will Not Work In the Local Server
3/26: I created the Wordpress website on a live server. I downloaded a copy to my XAMPP server. I exported the database and created a local one. I modified the wp-config.php file to connect to it. That all works. However, I am not able to use any of the links in the navbar and otherwise to open any of the pages and posts. All those hyperlinks have ...
Replace Text with hyperlinks
I would like to replace few words in the content of the posts. For example. Original: Excepted String: I have tried but there are few scenario where i have faced issues. When that word has predefined link along with a word. Like So my function exploded it in tow words like == My Solution ====
removing my underline from hyperlinks
So I am new to Wordpress, on my website I recently opened the source code and am learning how to customize even more. One thing I am stuck on is how to remove underlines from hyperlink, I tried the text decoration none thing but it did not work, this is the code, what and where do I typset into it to remove underlines? Thanks! also was not sure wha...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.