1

how can I allow unregistered users to post embeded images from other sites in comments?

Right now the wordpress strips any image html tag for unregistered users.

Thanks.

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Apr 11 at 17:00

1 Answer 1

0

To allow unregistered users to post embedded images from other sites in comments, you will need to modify wp comment handling to permit certain HTML tags for unregistered users. By default, wp sanitizes HTML input from unregistered users for security reasons.

You need to modify the list of allowed HTML tags in comments for unregistered users. You can use preprocess_comment wp filters

add code your functions.php file

// Allow unregistered users to post embedded images in comments
function allow_unregistered_users_to_post_images($comment_data) {
    // Check if the user is unregistered
    if (!is_user_logged_in()) {
        // Define the allowed HTML tags for unregistered users
        $allowed_tags = array(
            'a' => array(
                'href' => array(),
                'title' => array()
            ),
            'img' => array(
                'src' => array(),
                'alt' => array(),
                'title' => array(),
                'width' => array(),
                'height' => array()
            ),
            'br' => array(),
            'em' => array(),
            'strong' => array()
            // Add more tags if needed
        );

        // Set the allowed HTML tags for unregistered users
        $comment_data['comment_content'] = wp_kses($comment_data['comment_content'], $allowed_tags);
    }

    return $comment_data;
}
add_filter('preprocess_comment', 'allow_unregistered_users_to_post_images');

above code allow unregistered users to post embedded images <img> tags in comments. Adjust the $allowed_tags array as needed to permit other HTML tags or attributes

1
  • That looks like it'll perform an extra round of tag stripping but I can't see how it stops the existing code from removing those tags.
    – Rup
    Apr 8 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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