I have a theme that enables users to comment a post, and also to add a link inside a custom field. I've tried a few plugins to try to force a nofollow attribute, but none seem to affected the links that are inside the custom field.

I'm also looking for the ability to add exceptions for the nofollow attribute, such as domains that I want all the links to follow instead.

I suppose this has to be done manually. Anyone have any ideas?

Snippet of the actual custom field:

    <div class="contentText">
    <p class="comment"><?php comment_text(); ?></p>
    <?php if (get_comment_meta($comment->comment_ID, 'comment_source_link', true) != NULL) { ?>
    <p class="sourceLink"><span><?php echo $t_link_text;?>:</span>
<a href="<?php echo get_comment_meta($comment->comment_ID, 'comment_source_link', true) ?>" target="_blank">
<?php echo get_comment_meta($comment->comment_ID, 'comment_source_link', true) ?></a></p>
     <?php } ?>
         <?php $comment_parent_id = $comment->comment_post_ID; 
                        $question = get_post($comment_parent_id);
                    global $current_user;
                        get_currentuserinfo();  ?>

Snippet of the function:

// Add New comment
function post_new_comment($comment_content, $source_link){
    include ( ABSPATH . 'wp-load.php' );
    include (TEMPLATEPATH . '/translations.php');

    $comment_content_brs = nl2br($comment_content);

    // Allow a few HTML tags to be used
    $comment_content_tag_checked = wp_kses( $comment_content_brs, array(  
        'b' => array(),  
        'a' => array('href' => array(), 'target' => array()),
        'i' => array(),
        'u' => array(), 
        'p' => array(),
        'br' => array()
    ) );  
    $comment_content_stripped = balanceTags($comment_content_tag_checked,true);

    //Strip any tags then may have been put into the array
    //$comment_content_stripped = strip_tags($comment_content);
    $comment_source_stripped = strip_tags($source_link);

    // Get the topic ID
    global $wp_query;
    $topic_id = $wp_query->post->ID;
    $topic_author_id = $wp_query->post->post_author;

    // Get the Current User Info
    $user = wp_get_current_user();

    global $wpdb;
    $gather_comments = "SELECT * FROM ".$wpdb->prefix."comments WHERE comment_post_ID = '" . $topic_id . "' ORDER BY comment_date";
    $user_comments = $wpdb->get_results($gather_comments);

    // Validate the Form Data
    if(isEmptyString($comment_content_stripped)) return new WP_Error('forgot_comment', $t_message_forgot_comment_text);

    // Validate - Check to see if user already posted this exact comment for this topic
    foreach ($user_comments as $user_comment) {
        if ($user_comment->comment_author == $user->user_login ) {
            if ($user_comment->comment_content == $comment_content_stripped) {
                return new WP_Error('duplicate_user_comment_on_topic', $t_message_already_commented_topic);

    // Define the comment Data
    $comment_author = $user->user_login;
    $comment_author_email = $user->user_email;
    $comment_post_ID = $topic_id;
    $comment_parent = 0;
    $comment_content = $comment_content_stripped;
    $user_ID = $user->ID; 

    // Build the comment Array
    $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_content', 'comment_parent', 'user_ID');
    // Insert the comment into the DB
    $comment_id = wp_new_comment( $commentdata );   

    if ($comment_source_stripped != 'http://' || $comment_source_stripped == NULL) {
        update_comment_meta($comment_id, 'comment_source_link', $comment_source_stripped);
    update_comment_meta($comment_id, 'has_been_best_comment_before', 'no'); 
  • 1
    You got to provide more information than this. You said your comment form have a field to insert URL in custom field. How are you doing that? With a plugin or theme function. Add that code here in your question. – Robert hue Oct 2 '14 at 8:46
  • Hey Robert. I've added some code. Any ideas? – dkl Oct 3 '14 at 10:21
  • What domains do you want to exclude? – jetlej Oct 3 '14 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.