How I can remove all internal or external links on my posts at once? just remove link and leave anchor text.
3 Answers
Not sure about the database but you can do it easily with the_content
filter, just add the code below to your current (child) theme functions.php
file:
add_filter( 'the_content', 'misha_remove_all_a' );
function misha_remove_all_a( $content ){
return preg_replace('#<a.*?>(.*?)</a>#is', '\1', $content);
}
-
1Obligatory Comment: avoid manipulating HTML with regexps. That one will break on
<a title="and>">
. Just because you wouldn't doesn't mean others won't write wonky stuff in their documents.– janhCommented Oct 14, 2017 at 17:29 -
Agreed. I also trying to avoid such stuff, but sometimes you may need fast and easy solution. Commented Oct 14, 2017 at 17:42
-
Totally, I'm guilty of that as well. But you have to try to warn those that haven't yet stepped onto this dangerous path ;)– janhCommented Oct 14, 2017 at 17:47
-
-
thanks, but will not remove it from db, this is just a temporary solution.– JlilCommented Oct 14, 2017 at 20:15
Target your links with some CSS. In default twentyseventeen theme it's
.entry-content a {
pointer-events: none;
cursor: default;
}
add_filter('the_content', 'removelink_content',1);
function removelink_content($content = '')
{
preg_match_all("#<a(.*?)>(.*?)</a>#i",$content, $matches);
$num = count($matches[0]);for($i = 0;$i < $num;$i++){
$content = str_replace($matches[0][$i] , $matches[2][$i] , $content);
}
return $content;
}