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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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 
        );
    }
    
  2. I’m not sure, but from a brief look it seems like that there’s only the global $allowedtags; for this – sadly no API (aside from allowed_tags() to simply display them).

    You can extend it, but from a look at core it should already be possible. Make sure that you turn off all plugins (incl. Spam(!!) and Caching plugins), before testing the following plugin.

    <?php
    ! defined( 'ABSPATH' ) AND exit;
    /* Plugin Name: (#66166) »kaiser« Extend allowed HTML Tags for Admin */
    
    function wpse66166_extend_allowed_tags( $post_id )
    {
        global $allowed_tags;
        // For Admin users only
        if ( 
            ! current_user_can( 'manage_options' ) 
            OR is_admin()
        )
            return;
    
        if ( in_array( 'a', $allowed_tags ) )
            return print "Posting links is already possible for admins."
    
        return $GLOBALS['allowed_tags'] = array_merge(
             $allowed_tags
            ,array( 'a' => array( 'href' => 'title' ) )
        );
    }
    add_action( 'comment_form', 'wpse66166_extend_allowed_tags' );