Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

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 
    );
}
share|improve this answer
    
my sincerest apologies in neglecting to mention the plugin. I was interested to see an alternate approach but in hindsight I should have disclosed the entire situation. Again, my apologies and I'll be more verbose in with future questions. I did try this new modified plugin and while it throws no errors, the links are active for all users, not just admins. And yes, I have turned off all other comment plugins while testing this one. :) –  Lars Sep 25 '12 at 17:06
    
Ad not mentioning it) You're a new user. Just read the FAQs and everything's alright: Ad the question) You'll have to test this with new comments. It replaces comments on the fly afaik. –  kaiser Sep 25 '12 at 22:43
    
Please, move this all to as edit to the question and remove the comments. Also: We're already a "do this job" for me point... –  kaiser Sep 26 '12 at 17:22
    
No problem, I'm just a bit in a tizzy since this is rather crucial and I've exhausted my mind in searching. The function, as you pasted, unfortunately doesn't strip the tags even off new comments. I updated the post above with the latest. And yes, we are, and I'm open to that if someone wants to let me know what they think they can do it in. It seems like a plugin others would want, as well. –  Lars Sep 26 '12 at 17:33
    
@Lars You can contact me through my homepage - see profile. –  kaiser Sep 26 '12 at 20:36

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' );
share|improve this answer
    
Thanks for responding so quickly @ kaiser! Upon testing this plugin, I get the following: Warning: in_array() expects parameter 2 to be array, null given in admin-links.php on line 11 and Warning: array_merge() [function.array-merge]: Argument #1 is not an array in admin-links.php on line 17 –  Lars Sep 24 '12 at 23:32
    
Try again - added check to not trigger inside admin. –  kaiser Sep 24 '12 at 23:43
    
I current use this plugin right now: stefannilsson.com/remove-links-in-comments Which strips the anchor tags from comments. Is there any way to wrap this in some kind of conditional that would tell it to not strip them if the comment is from an admin or logged in user? –  Lars Sep 24 '12 at 23:45
    
Unfortunately same thing... –  Lars Sep 24 '12 at 23:51
    
First: I moved it to another hook. Second: Are you using this plugin all the time? If so: WHY didn't you tell in the question? –  kaiser Sep 25 '12 at 0:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.