Wordpress 3 - Remove Links from Posts via function

2019-06-01 04:05发布

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!

标签: php wordpress
2条回答
The star\"
2楼-- · 2019-06-01 04:18

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.

查看更多
三岁会撩人
3楼-- · 2019-06-01 04:26

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.

查看更多
登录 后发表回答