Hello guys,

can anybody advice me how to remove or limit adding links inside posts of a wordpress blog? any php plugin available for this? I have a blog where many contributors are placing too many links with their posts. How can I solve this issue?

D!

Dani AI

Generated

Brief follow-up to 's question (plain text vs removal) and 's admin exception: the safest pattern is to change output rather than stored content, and to base the change on the post author capability. That keeps administrator (or other trusted) posts with live links while showing contributors' links as plain text.

The non-destructive solution is a the_content filter that runs after WP's auto-linking and converts <a> tags to plain text (for example keeping the link text and appending the URL in parentheses). Add this to a theme functions.php or a small plugin:

add_filter( 'the_content', 'dw_plain_links_for_non_privileged_authors', 100 );

function dw_plain_links_for_non_privileged_authors( $content ) {
    global $post;
    if ( empty( $post ) ) return $content;
    if ( user_can( $post->post_author, 'unfiltered_html' ) ) return $content;

    return preg_replace_callback(
        '#<a\s+[^>]*href=(["\'])(.*?)\1[^>]*>(.*?)</a>#is',
        'dw_anchor_to_plain_text_callback',
        $content
    );
}

function dw_anchor_to_plain_text_callback( $m ) {
    $href = $m[2];
    $text = wp_strip_all_tags( $m[3] );
    if ( empty( $text ) ) $text = $href;
    return $text . ' (' . esc_url( $href ) . ')';
}

An alternative is sanitizing on save (save_post) so links never get stored; that is destructive and harder to revert, so only use it after backups and tests. Regex-based replacement is simple and fast but not perfect for malformed HTML—use DOMDocument for more robust parsing on complex content. Always test on a staging copy before applying to production.

Recommended Answers

All 2 Replies

So do you just want to have the links parsed as plain text, or do you want them removed all together?

Either is possible, just depends on exactly what you want to achieve.

Yes, I just want to show it as plain text.. But when the administrator publish posts there can be links.. I don't know whether this will be possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.