简体   繁体   中英

Wordpress 3 - Remove Links from Posts via functions.php

Is there a way that I can remove links in posts via my functions.php file. Basically I don't want anyone to be able to go outside of the blog posts that are viewed. I have hundreds of posts so I obviously can't go through all of them and remove them manually. Or could I use javascript?

Thanks so much.


Updated: The jQuery below is great. Does anyone know if there is a way I can do it thru php in my functions.php file? If, for whatever ridiculous reason, someone has JS disabled is why I ask.

Thanks!

You could use JavaScript, but you're not going to be able to stop people leaving if they want to.

Something like this may work, although I haven't tested and it was written off-hand:

<script>
$('#content a').each(function() {
    $(this).replaceWith($(this).text());
});
</script>

With the jQuery library, this should replace all <a> tags with what was in between them.

So <a href="http://www.google.co.uk/">Google</a> should become just Google .

You can strip out the links on the fly using a regular expression -

$post_content = get_the_content();
$post_content = preg_replace( "|<a *href=\"(.*)\">(.*)</a>|", "\\2", $post_content );
echo $post_content

This would need to go in your theme wherever you print the_content. Untested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM