How to allow only Admins or Logged In Users to post links in comments?

I know how to disable HTML in comments, but I’d like the ability to only have Admins be able to post links, or only Logged In users if need be, to have the ability to post links. Is this possible?

I am currently using this plugin.

I wrote the author of the plugin and he had this to say:

“The problem with the function is that it applies to all comments by using a filter. I’m not sure if it’s possible to add a condition to it. Instead, you’d have to create a new function: http://www.pastebin.com/q68qkKFX

Then, when you echo out your comment, you’d have to do something like this:

echo remove_comment_links($commentID, $commentTEXT); 

But with the right variables ($commentID and $commentTEXT doesn’t exist). “

I used:

<?php $comment_text = get_comment_text(); 

$comment_id = get_comment_id(); 

echo remove_comment_links($comment_id, $comment_text); ?>

But it strips out all HTML from all users, instead.

2 Answers
2

Additional info by the OP

It seems that the OP “forgot” to tell, that he uses the following plugin:

<?php
/*
Plugin Name: Remove Links in Comments
Plugin URI: http://www.stefannilsson.com/remove-links-in-comments/
Description: Deactivate hyperlinks in comments.
*/

function remove_links($string = '') {
    $link_pattern = "/<a[^>]*>(.*)<\/a>/iU";
    $string = preg_replace($link_pattern, "$1", $string);

    return $string;
}
add_filter('comment_text', 'remove_links');

The modified plugin

To make this work, we have to modify it slightly:

<?php
! defined( 'ABSPATH' ) AND exit;
/* Plugin Name: (#66166) »kaiser« Extend allowed HTML Tags for Admin <strong>only</strong> */

function wpse66166_extend_allowed_tags( $post_id )
{
    global $allowed_tags;
    // For Admin users only
    if ( 
        ! current_user_can( 'manage_options' ) 
        OR is_admin()
    )
        return add_filter( 'comment_text', 'wpse66166_remove_links' );

    if ( ! is_array( $allowed_tags ) )
        return;

    return $GLOBALS['allowed_tags'] = array_merge(
         $allowed_tags
        ,array( 'a' => array( 'href' => 'title' ) )
    );
}
add_action( 'comment_form', 'wpse66166_extend_allowed_tags' );

function wpse66166_remove_links( $comment )
{
    return preg_replace(
         "/<a[^>]*>(.*)<\/a>/iU"
         "$1"
        ,$comment 
    );
}

Leave a Comment