admin管理员组文章数量:1328037
I'm inserting a post using wp_post_insert()
. And my post's content looks like this:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAN4AAAB6CA { ... }
but on the insert process, Wordpress removes
the data attribute. So above code becomes this:
<img src="image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAN4AAAB6CA { ... }
I've tried something like this but no luck:
function my_filter_allowed_html($allowed, $context){
if (is_array($context)) {
return $allowed;
}
if ($context === 'post') {
$allowed['img']['data'] = true;
$allowed['src']['data'] = true;
}
return $allowed;
}
add_filter('wp_kses_allowed_html', 'my_filter_allowed_html', 10, 2);
How can I avoid this?
I'm inserting a post using wp_post_insert()
. And my post's content looks like this:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAN4AAAB6CA { ... }
but on the insert process, Wordpress removes
the data attribute. So above code becomes this:
<img src="image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAN4AAAB6CA { ... }
I've tried something like this but no luck:
function my_filter_allowed_html($allowed, $context){
if (is_array($context)) {
return $allowed;
}
if ($context === 'post') {
$allowed['img']['data'] = true;
$allowed['src']['data'] = true;
}
return $allowed;
}
add_filter('wp_kses_allowed_html', 'my_filter_allowed_html', 10, 2);
How can I avoid this?
Share Improve this question edited Nov 29, 2019 at 12:06 Earth asked Nov 28, 2019 at 15:35 EarthEarth 1811 gold badge1 silver badge8 bronze badges1 Answer
Reset to default 5Thanks 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 yourfunctions.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.
本文标签: wp ksesHow to allow dataimage attribute in src tag during post insert
版权声明:本文标题:wp kses - How to allow data:image attribute in src tag during post insert? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742239562a2438708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论