How to allow data:image attribute in src tag during post insert?

Thanks to naththedeveloper from StackOverflow. His answer worked for me.

Well, this was a nightmare to find, but I think I’ve resolved it after digging through the WordPress code which hooks in through wp_insert_post. Please add this to your functions.php file and check it works:

add_filter('kses_allowed_protocols', function ($protocols) {
    $protocols[] = 'data';

    return $protocols;
});

Essentially internally in WordPress, there’s a filter that checks the protocol of any URL’s in the content and strips any which it doesn’t like. By default, the supported list doesn’t support the data protocol. The above function just adds it to the list of supported protocols.

This filter does not run if you’re an administrator, which is probably why you’re seeing this issue only when logged out.

Thank you for your research.

Leave a Comment