How do I remove all links from all my posts at once?

admin2025-01-07  6

I found this code but I don't understand it.

add_filter( 'the_content', 'misha_remove_all_a' );
function misha_remove_all_a( $content ){
    return preg_replace('#<a.*?>(.*?)</a>#is', '\1', $content);
}

This is the link

My website address is this

I found this code but I don't understand it.

add_filter( 'the_content', 'misha_remove_all_a' );
function misha_remove_all_a( $content ){
    return preg_replace('#<a.*?>(.*?)</a>#is', '\1', $content);
}

This is the link

My website address is this

Share Improve this question edited Mar 3, 2020 at 12:12 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Mar 3, 2020 at 11:51 GideorlahGideorlah 11 bronze badge 2
  • Could you be a little more specific about your question? Not understanding it doesn't tell us what part isn't making sense. Do you need help figuring out where this goes? Or are you concerned about what it does? – WebElaine Commented Mar 3, 2020 at 14:01
  • What I mean is where do I place the link (I want to remove from all my posts) in that code. – Gideorlah Commented Mar 5, 2020 at 9:07
Add a comment  | 

1 Answer 1

Reset to default 0

This code is a filter, as shown by the initial add_filter call. This type of code can either be placed in a custom plugin, or a child theme or custom theme's functions.php file. (You don't want to place it in an existing functions.php file because whenever you update the theme, it will get lost.)

It will automatically run on the front-end (public) view of every post, page, and custom post type. That's because the filter is added to the_content, which takes the content from the database and renders it out as HTML on the front end.

If you'd like to make this a plugin, you just need an opening PHP tag and a comment to let WordPress recognize it as a plugin:

<?php
/* Plugin Name: Strip Links from Content */
add_filter( 'the_content', 'misha_remove_all_a' );
function misha_remove_all_a( $content ){
    return preg_replace('#<a.*?>(.*?)</a>#is', '\1', $content);
}
?>

Save it as strip-links.php, upload it to /wp-content/plugins/strip-links/, and you can then activate the plugin from wp-admin.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255860a315.html

最新回复(0)