How to Remove Links in Posts with WordPress

Update: This functionality no longer exists with the latest versions of WordPress and Gutenberg. This is because Gutenberg no longer uses the “post_updated_messages” filter. If you want this code to work, I suggest you install the WordPress Classic Editor plugin.

Imagine you have an article submission site, where readers can generate their own content. But you want to make sure that it’s not abused with spam links. Let’s say you need to disallow all links in your content and not allow users to submit them at all. Obviously every situation will be different. But let’s look at a single scenario where we display an error whenever a user tries to submit content that contains a link.

Warn Users about Submitting Content with Links

Open up your functions.php file or plugin for custom PHP code and all the following. If you don’t know how to add code snippets in WordPress, see this tutorial.

function show_editor_message($messages) {
    global $post;
    $post_title = $post->post_content;
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    if(preg_match($reg_exUrl, $post->post_content)) {
        $error_message = 'Post contains links. Please Remove them first';
        add_settings_error('post_has_links', '', $error_message, 'error');
        settings_errors( 'post_has_links' );
        $post->post_status = 'draft';
    return $messages;

add_action('post_updated_messages', 'show_editor_message');

This piece of code with throw up a warning whenever a user tries to submit a post which contains a link. Here’s a screenshot:

Remove links in posts
Remove links in posts

The post will be automatically sent back to “draft” status regardless of whether they tried to save, or publish it. This way, instead of removing links or disabling them, we give the user an opportunity to restructure their content in a natural way.

You can add several variations to this. For example, you can add the following line to the beginning of the function to allow publishing/saving by admins:

if ( current_user_can( 'manage_options' ) )
    return $messages;

This can allow you to override the default behavior. While regular users can’t submit posts with links, you can log in and do it on their behalf on a case by case basis. The nature of this solution is such that the requirements will be different each time. Perhaps you want to allow internal links, and leave out external ones. In that case, the regular expression we use for matching will change.

But this should work nicely. With the second part of the code containing the check for admins, you have the flexibility to block all links except those you want to expressly allow.

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!


  1. What if I want them to be able to publish their posts but strip the links without the warning?


  2. You do not have an automatic code


  3. Please provide the function to remove links automatically when a user submits the post


Speak Your Mind