英文:
WordPress 6 Block Custom HTML formats and inserts <br /> and removes some <p> and add some <p>
问题
使用最新版本的WordPress(6.2.2),我的问题很简单。
我希望自定义HTML块不要对我的内容进行任何更改。是的,我知道自定义HTML可能不适用于JS,但是它甚至会忽略JS中的<br>
,还会添加和删除常规HTML中的一些<p>
。
以下是一个示例;它会从"container"中删除<p>
,然后在JS中添加<br />
,然后在"modalid"中添加一些<p>
,这到底是怎么回事?
我不认为要求WP提供某种动作/过滤器来让系统保存为原始格式是一个很大的要求。
我尝试过;
remove_filter('the_content', 'wpautop');
remove_filter( 'the_excerpt', 'wpautop' );
但没有任何变化。我认为区块模板没有经过这个过滤器。
将这个:
<footer>
<div id="modalid" class="modal">
<center>
<h5>content</h5>
content content content content content content content content content content content content content contentinformation by contentcontentcontentcontentcontentcontent <br> <br>
For more information about our privacy practices, please review our <a href="link">Privacy Policy</a>.
</center>
</div>
<div class="container">
<p>
<a class="footer-link" href="link">bla</a> |
<a class="footer-link" href="link">bla</a>
| <a class="footer-link" rel="modal:open" href="#modalid" target="_blank">Please Sell My Information</a> <br>
&copy; 2014 - 2023 BLA. All rights reserved.
</p>
</div>
</footer>
<p>
<script>
$('.bla').bla({
dots: false,
infinite: true,
});
</script>
</p>
转换成这个:
<footer>
<div id="modalid" class="modal">
<center>
<h5>content</h5>
<p>content content content content content content content content content content content content content contentinformation by contentcontentcontentcontentcontentcontent <br> <br></p>
<p>For more information about our privacy practices, please review our <a href="link">Privacy Policy</a>.</p><br />
</center>
</div>
<div class="container">
<a class="footer-link" href="link">bla</a> |
<a class="footer-link" href="link">bla</a>
| <a class="footer-link" rel="modal:open" href="#modalid" target="_blank">Please Sell My Information</a> <br>
&copy; 2014 - 2023 BLA. All rights reserved.
</div>
</footer>
<p>
<script><br />
$('.bla').bla({<br />
dots: false,<br />
infinite: true,<br />
});<br />
</script>
</p>
英文:
Using the latest the greatest WordPress (6.2.2), my problem is simple.
I want Block Custom HTML to not touch my content at all. Yes I know Custom HTML may not meant for JS but ignoring the <br>
s in JS, it even adds and removes some of the <p>
from regular HTML.
Here's an example below; it removes the <p>
from "container" then adds <br />
to JS then adds some <p>
in "modalid", just WTH??
I don't think it's a big ask for WP to provide some kinda of action/filter to let the system save as raw.
I tried;
remove_filter('the_content', 'wpautop');
remove_filter( 'the_excerpt', 'wpautop' );
and nothing changes. I do not think Block templates are going thru this filter.
Turning this;
<footer>
<div id="modalid" class="modal">
<center>
<h5>content</h5>
content content content content content content content content content content content content content contentinformation by contentcontentcontentcontentcontentcontent <br> <br>
For more information about our privacy practices, please review our <a href="link">Privacy Policy</a>.
</center>
</div>
<div class="container">
<p>
<a class="footer-link" href="link">bla</a> |
<a class="footer-link" href="link">bla</a>
| <a class="footer-link" rel="modal:open" href="#modalid" target="_blank">Please Sell My Information</a> <br>
&copy; 2014 - 2023 BLA. All rights reserved.
</p>
</div>
</footer>
<p>
<script>
$('.bla').bla({
dots: false,
infinite: true,
});
</script>
</p>
into this
<footer>
<div id="modalid" class="modal">
<center>
<h5>content</h5>
<p>content content content content content content content content content content content content content contentinformation by contentcontentcontentcontentcontentcontent <br> <br></p>
<p>For more information about our privacy practices, please review our <a href="link">Privacy Policy</a>.</p><br />
</center>
</div>
<div class="container">
<a class="footer-link" href="link">bla</a> |
<a class="footer-link" href="link">bla</a>
| <a class="footer-link" rel="modal:open" href="#modalid" target="_blank">Please Sell My Information</a> <br>
&copy; 2014 - 2023 BLA. All rights reserved.
</div>
</footer>
<p>
<script><br />
$('.bla').bla({<br />
dots: false,<br />
infinite: true,<br />
});<br />
</script>
</p>
答案1
得分: 1
我可以复制在WP 6.2.2中描述的问题,但似乎只在使用“自定义HTML”块在“站点编辑器”的“模板部分”中时发生,而在编辑帖子/页面时却不会发生。
如果您将相同的代码添加到帖子/页面中并发布/预览/编辑它,包括JS代码的代码保持不变。如果您将工作的代码块保存为可重复使用的块并将其插入到站点编辑器中的模板部分中,问题将出现在编辑器内。
我的观察是它实际上没有改变底层代码:如果您发布模板,HTML源代码会如预期地呈现。问题似乎纯粹出现在站点编辑器中,当它将HTML安全地呈现为HTML时。
呈现的HTML包括如预期的代码,没有错误的<p>
或<br />
出现。
如果您删除<script>...<script>
代码,站点编辑器中的呈现问题将得到解决。
理想情况下,即使只是“几行代码”,您也应该分别加载JS代码,对于需要前端JS的自定义HTML代码,我会注册核心“自定义HTML”的块变体以启用在使用块变体时加载JS。
英文:
I can replicate the issue as described in WP 6.2.2, though it only seems to occur when using Custom HTML
block in a Template Part
in the Site Editor
- not when editing a post/page.
If you add the same code in a post/page and publish/preview/edit it, the code including the JS code remains the same. If you save the working code block as a reusable block and insert it into a Template Part in Site Editor, the issue appears inside the Editor.
My observation is that it's not actually changing the underlying code: if you publish the template, the HTML source is rendered as expected. The issue appears to purely be within the Site Editor when it renders HTML safely as HTML.
The rendered HTML includes the code as expected, with no erroneous <p>
or <br />
present.
If you remove the <script>...<script>
code, the rendering issue in the Site Editor is resolved.
Ideally, you should enqueue the JS code separately, even if it is "just a few lines". For custom HTML code that requires front-end JS, I would register a block variation of the core Custom HTML
to enable enqueuing the JS for when the block variation is used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论