如何只允许管理员或登录用户在评论中发布链接?

时间:2012-09-24 作者:Lars

我知道如何在评论中禁用HTML,但我希望只有管理员才能发布链接,或者只有登录用户(如果需要)才能发布链接。这可能吗?

我现在using this plugin.

我写了插件的作者,他说:

“该函数的问题是,它通过使用筛选器应用于所有注释。我不确定是否可以向其添加条件。相反,您必须创建一个新函数:http://www.pastebin.com/q68qkKFX

然后,当你回应你的评论时,你必须这样做:

echo remove_comment_links($commentID, $commentTEXT); 
但使用正确的变量($commentID和$commentTEXT不存在)。“”

我使用了:

<?php $comment_text = get_comment_text(); 

$comment_id = get_comment_id(); 

echo remove_comment_links($comment_id, $comment_text); ?>
但它从所有用户中删除了所有HTML。

2 个回复
SO网友:kaiser

OP提供的其他信息似乎OP“忘记”告诉他,他使用了以下插件:

<?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\');

修改后的插件

要使其工作,我们必须稍微修改一下:

<?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 
    );
}

SO网友:kaiser

我不确定,但从简单的角度看,似乎只有global $allowedtags; 为此,遗憾的是没有API(除了allowed_tags() 以简单地显示它们)。

您可以扩展它,但从核心来看,它应该已经是可能的了。确保关闭所有插件(包括垃圾邮件)(!!)和缓存插件),然后测试以下插件。

<?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\' );

结束