admin管理员组文章数量:1025278
When I paste a table into the WordPress editor, I always get widths like <td width="232">
.
I want to remove all widths when tables are added to the editor.
If it was regex, I would write something like: width="([0-9]+)"
.
How do I go about accomplishing this?
When I paste a table into the WordPress editor, I always get widths like <td width="232">
.
I want to remove all widths when tables are added to the editor.
If it was regex, I would write something like: width="([0-9]+)"
.
How do I go about accomplishing this?
Share Improve this question edited May 7, 2019 at 19:23 MikeNGarrett 2,6711 gold badge20 silver badges29 bronze badges asked May 7, 2019 at 16:06 KrystleKrystle 232 bronze badges 1- Working to get your question reopened since this is a relevant WordPress question. – MikeNGarrett Commented May 7, 2019 at 18:03
1 Answer
Reset to default 1I would approach this by modifying the attributes allowed in the table, tr, and td markup from wp_kses
. wp_kses
is a function that runs on the content to filter out unwanted tags and attributes. It stands for KSES Strips Evil Scripts, but it does much more than that.
It's a large and sometimes convoluted function, but it's very useful if you want to extend or modify what it filters out.
Keep in mind, this only applies to users without the unfiltered_html
capability.
The wp_kses_allowed_html
filter allows you to modify the allowed tags and attributes. The array structure is $allowedtags['tag_name']['attribute']
, so you would look for $allowedtags['table']['width']
, for example.
function my_modify_tags( $tags, $context ) {
if ( 'post' !== $context ) {
return $tags;
}
if ( isset( $tags['table']['width'] ) ) {
unset( $tags['table']['width'] );
}
if ( isset( $tags['td']['width'] ) ) {
unset( $tags['td']['width'] );
}
if ( isset( $tags['th']['width'] ) ) {
unset( $tags['th']['width'] );
}
return $tags;
}
add_filter( 'wp_kses_allowed_html', 'my_modify_tags', 1, 2 );
The $context
here allows for this to apply to specific instances where you want to modify the tags. By targeting post
, you can have this filtered list of allowed tags only apply to the post content.
When I paste a table into the WordPress editor, I always get widths like <td width="232">
.
I want to remove all widths when tables are added to the editor.
If it was regex, I would write something like: width="([0-9]+)"
.
How do I go about accomplishing this?
When I paste a table into the WordPress editor, I always get widths like <td width="232">
.
I want to remove all widths when tables are added to the editor.
If it was regex, I would write something like: width="([0-9]+)"
.
How do I go about accomplishing this?
Share Improve this question edited May 7, 2019 at 19:23 MikeNGarrett 2,6711 gold badge20 silver badges29 bronze badges asked May 7, 2019 at 16:06 KrystleKrystle 232 bronze badges 1- Working to get your question reopened since this is a relevant WordPress question. – MikeNGarrett Commented May 7, 2019 at 18:03
1 Answer
Reset to default 1I would approach this by modifying the attributes allowed in the table, tr, and td markup from wp_kses
. wp_kses
is a function that runs on the content to filter out unwanted tags and attributes. It stands for KSES Strips Evil Scripts, but it does much more than that.
It's a large and sometimes convoluted function, but it's very useful if you want to extend or modify what it filters out.
Keep in mind, this only applies to users without the unfiltered_html
capability.
The wp_kses_allowed_html
filter allows you to modify the allowed tags and attributes. The array structure is $allowedtags['tag_name']['attribute']
, so you would look for $allowedtags['table']['width']
, for example.
function my_modify_tags( $tags, $context ) {
if ( 'post' !== $context ) {
return $tags;
}
if ( isset( $tags['table']['width'] ) ) {
unset( $tags['table']['width'] );
}
if ( isset( $tags['td']['width'] ) ) {
unset( $tags['td']['width'] );
}
if ( isset( $tags['th']['width'] ) ) {
unset( $tags['th']['width'] );
}
return $tags;
}
add_filter( 'wp_kses_allowed_html', 'my_modify_tags', 1, 2 );
The $context
here allows for this to apply to specific instances where you want to modify the tags. By targeting post
, you can have this filtered list of allowed tags only apply to the post content.
本文标签: regexRemove all table widths from editor content
版权声明:本文标题:regex - Remove all table widths from editor content 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745515158a2154004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论